2013-04-25 55 views
0

我想使用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() 

回答

0

這個問題已經被回答#一遍又一遍。令人驚訝的是,沒有人投票決定將其作爲一個副本來關閉它。

在任何情況下:因爲我以前已經回答了很多次,所以使用PdfWriter/PdfImportedPage合併文檔是不好的做法。請閱讀我撰寫的關於iText的書的chapter 6,並且您會發現,向您提供您複製的代碼示例的人都是錯誤的。您應該使用PdfCopy合併文件,而不是PdfWriter

有關示例,請閱讀以下StackOverflow的答案:

How to keep original rotate page in itextSharp (dll)

How to merge multiple pdf files (generated in run time)?

Itext pdf Merge : Document overflow outside pdf (Text truncated) page and not displaying

等等...

如果你在接受你足夠快這個答案,你可能會很幸運,不會因爲不搜索而被低估在發佈一個已經回答的問題之前提交檔案。

+0

我搜索了檔案,發現了類似的答案。我承認並道歉,我不願意偏離我得到的榜樣。我只花了大約半天的時間,不知道itextsharp是以編程方式成功合併pdf的。我仍然是初學者,所以這對我來說很重要。我會研究你的解決方案。 – 2013-04-25 20:52:12