问题描述
我正在尝试使用 pdfsharp库将多个图像转换为pdf.
I am trying to convert multiple images to pdf using pdfsharp library.
我能够转换单个图像,并且效果很好.
I am able to convert single image and it works pretty well.
在将bulk images
转换为single pdf
时,我面临的问题是它将所有图像都进行转换,但是在转换后如果我检查它仅显示最后一个图像,因为它没有追加到现有图像上,覆盖上一张图片.
And while converting bulk images
to single pdf
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.
那我该如何纠正呢?
我第一次使用pdf库并向我指出任何错误时将提供任何帮助,我将很高兴知道更多有关此的信息,但是如果您指出我没有任何帮助,我将不为所动我犯的错误.
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.
这是我的代码:
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
推荐答案
问题是您在循环中每次通过都会创建一个新的PDF文档.您需要将其移出循环.另外,您引用的是第0页,而不是第pix
页.这是我的解决方法:
The problem is that you are creating a new PDF document on each pass through the loop. You need to move this outside the loop. Also, you are referencing page 0, not page pix
. Here is how I would fix it:
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
这篇关于使用pdfsharp将多个图像转换为pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!