2011-12-12 57 views
3

我想用pdfsharp library將多個圖像轉換爲pdf。使用pdfsharp將多個圖像轉換爲pdf

我能夠轉換單個圖像,它工作得很好。

雖然轉換bulk imagessingle pdf我面臨的問題,它需要所有的圖像,並把它們轉換,但轉換後如果我檢查它顯示我只有最後的圖像,因爲它是不附加到現有的圖像,並覆蓋舊的圖片。

那我該如何糾正呢?

任何幫助將不勝感激,因爲我第一次使用pdf庫並指出我如果我犯了任何錯誤。而且我會被帶走了解更多關於這個,我不覺得雖然如果你指出我解決我犯的錯誤。

這裏是我的代碼:

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 

背景工人:

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 

轉換按鈕:

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 

保存全文:

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() <> System.Windows.Forms.DialogResult.OK Then 
      Return 
     End If 
     destFile = sfdDestFile.FileName 
End Sub 

回答

7

問題是,您正在通過循環的每次傳遞中創建一個新的PDF文檔。你需要在循環之外移動它。此外,您正在參考頁面0,而不是頁面pix。這裏是我將如何解決它:

Private Sub bw_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles bw.DoWork 
    Dim doc As New PdfDocument() 

    For pix As Integer = 0 To CheckedListBox1.CheckedItems.Count - 1 
     Try 
      Dim source As String = CheckedListBox1.Items(pix).ToString() 
      Dim oPage As New PDFPage() 

      doc.Pages.Add(oPage) 
      Dim xgr As XGraphics = XGraphics.FromPdfPage(oPage) 
      Dim img As XImage = XImage.FromFile(source) 

      xgr.DrawImage(img, 0, 0) 
      success = True 
     Catch ex As Exception 
      MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) 
     End Try 
    Next 

    Dim destinaton As String = (TryCast(e.Argument, String()))(1) 
    doc.Save(destinaton) 
    doc.Close() 
End Sub 
+0

感謝您指出我,它工作得很好。 – coder