2013-04-25 240 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