使用pdfsharp将多个图像转换为pdf

4

我正在尝试使用pdfsharp库将多个图像转换为pdf。

我能够转换单个图像并且效果很好。

在将批量图像转换为单个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

保存PDF:
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
1个回答

9
问题在于你在循环的每一遍都创建了一个新的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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接