【问题标题】:Converting multiple Images to pdf using pdfsharp使用 pdfsharp 将多个图像转换为 pdf
【发布时间】:2011-12-12 18:26:51
【问题描述】:

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

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

在将bulk images 转换为single 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

【问题讨论】:

    标签: vb.net image pdfsharp


    【解决方案1】:

    问题是您在每次循环时都创建了一个新的 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-27
      • 1970-01-01
      • 1970-01-01
      • 2015-12-19
      • 2016-05-14
      相关资源
      最近更新 更多