我想使用itextsharp合併pdf文件。
這個問題讓我在合併之前應用到單個文件的任何裁剪或旋轉都被忽略了。所有的原始文件被剪切和旋轉爲TIFF然後轉換爲PDF,現在最後我試圖合併它們。
我希望頁面大小與添加的內容匹配,並且我已經應用了任何輪換。itextsharp合併調整大小和不旋轉pdf
感謝您的幫助,
科爾賓日布魯因
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()
我搜索了檔案,發現了類似的答案。我承認並道歉,我不願意偏離我得到的榜樣。我只花了大約半天的時間,不知道itextsharp是以編程方式成功合併pdf的。我仍然是初學者,所以這對我來說很重要。我會研究你的解決方案。 – 2013-04-25 20:52:12