我正在尝试使用 itextsharp 合并 pdf 文件。
问题是我在合并之前对单个文件应用的任何裁剪或旋转都被忽略了。所有原始文件都被裁剪和旋转为 TIFF,然后转换为 pdf,现在我终于尝试合并它们。
我希望页面大小与添加的内容相匹配,并且我申请的任何旋转都可以通过。
感谢您的帮助,
Corbin de Bruin
Public Function MergePDFFiles(FileList As Dictionary(Of String, String), DeleteOldFile As Boolean) As Byte()
' Public Function MergePDFFiles(FileList As Dictionary(Of String, String), DeleteOldFile As Boolean) As MemoryStream()
Dim document As New Document()
Dim output As New MemoryStream()
Try
Dim writer As PdfWriter = PdfWriter.GetInstance(document, output)
writer.PageEvent = New PdfPageEvents()
document.Open()
Dim content As PdfContentByte = writer.DirectContent
' foreach
For Each FilePath As KeyValuePair(Of String, String) In FileList
If File.Exists(FilePath.Value) Then
Dim reader As New PdfReader(FilePath.Value)
Dim numberOfPages As Integer = reader.NumberOfPages
For currentPageIndex As Integer = 1 To numberOfPages
document.SetPageSize(reader.GetPageSizeWithRotation(currentPageIndex))
document.NewPage()
' you can see iTextSharp.tutorial.01 - 0403sample
If currentPageIndex.Equals(1) Then
Dim par As New Paragraph(FilePath.Key)
Debug.Print("FilePath.Key = " & FilePath.Key)
Dim bookmark As New Chapter(par, 0) With {.NumberDepth = 0}
document.Add(bookmark)
End If
Dim importedPage As PdfImportedPage = writer.GetImportedPage(reader, currentPageIndex)
Dim pageOrientation As Integer = reader.GetPageRotation(currentPageIndex)
If (pageOrientation = 90) OrElse (pageOrientation = 270) Then
content.AddTemplate(importedPage, 0, 1.0F, 1.0F, 0, 0, reader.GetPageSizeWithRotation(currentPageIndex).Height)
Else
content.AddTemplate(importedPage, 1.0F, 0, 0, 1.0F, 0, 0)
End If
Next
End If
Next
Catch exception As Exception
Debug.Print("Failure")
Finally
document.Close()
End Try
If DeleteOldFile Then
'Delete(FileList)
End If
Return output.GetBuffer()
End Function
End Try
If DeleteOldFile Then
'Delete(FileList)
End If
Return output.GetBuffer()