title: Converting multiple Images to pdf using pdfsharp
body: <p>I am trying to convert multiple images to pdf using <a href="http://www.pdfsharp.net/MainPage.ashx" rel="nofollow">pdfsharp library</a>.</p>
  
  <p>I am able to convert single image and it works pretty well.</p>
  
  <p>And while converting <code>bulk images</code> to <code>single pdf</code> I am facing problem that it takes all the images and converts them but after conversion If I check it shows me only the last image as it is not appending to the existing image and it overwrites the previous image.</p>
  
  <p>So how do I rectify this?</p>
  
  <p>Any help will be appreciated as I am first time working with pdf library and point me out If I am doing any mistake.And I will be gald to know more about this and I don't feel though If you pointed me out the mistake I have done.</p>
  
  <p>Here is my code:</p>
  
  <pre><code> Private Sub btnAddFolder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddFolder.Click
              If Me.FolderBrowserDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
  
              Dim f As New DirectoryInfo(Me.FolderBrowserDialog1.SelectedPath)
              Dim fso As New System.Object
              For Each file As FileInfo In f.GetFiles
                  Select Case file.Extension.ToLower
                      Case ".jpg", ".bmp", ".gif", ".png"
                          Me.ThumbControl1.BackgroundImage = Nothing
                          Me.CheckedListBox1.Items.Add(file.FullName, CheckState.Checked)
                          Me.ThumbControl1.AddThumbnail(file.FullName)
                          Me.ThumbControl1.BackgroundImage = Nothing
                          Me.CheckedListBox1.SelectedIndex = 0
                  End Select
              Next
              End If
      End Sub
  </code></pre>
  
  <p>Background worker:</p>
  
  <pre><code> Private Sub bw_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles bw.DoWork
          For pix As Integer = 0 To CheckedListBox1.CheckedItems.Count - 1
              Try
                  Dim source As String = CheckedListBox1.Items(pix).ToString()
                  Dim destinaton As String = (TryCast(e.Argument, String()))(1)
  
                  Dim doc As New PdfDocument()
                  doc.Pages.Add(New PdfPage())
                  Dim xgr As XGraphics = XGraphics.FromPdfPage(doc.Pages(0))
                  Dim img As XImage = XImage.FromFile(source)
  
                  xgr.DrawImage(img, 0, 0)
                  doc.Save(destinaton)
                  doc.Close()
                  success = True
              Catch ex As Exception
                  MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
              End Try
          Next
      End Sub
  </code></pre>
  
  <p>Convert button:</p>
  
  <pre><code>  Private Sub btnConvert_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnConvert.Click
           bw.RunWorkerAsync(New String(1) {srcFile, destFile})
    End sub
  </code></pre>
  
  <p>Saving Pdf:</p>
  
  <pre><code>Private Sub btnSelectDest_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSelectDest.Click
          sfdDestFile.Filter = "PDF Files(*.pdf)|*.pdf"
          If sfdDestFile.ShowDialog() &lt;&gt; System.Windows.Forms.DialogResult.OK Then
              Return
          End If
          destFile = sfdDestFile.FileName
   End Sub
  </code></pre>