0

我正在尝试使用 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()
4

1 回答 1

0

这个问题已经在 StackOverflow 上得到了一遍又一遍的回答。令人惊讶的是,没有人投票将其作为重复项关闭。

无论如何:正如我之前多次回答的那样,使用PdfWriter/PdfImportedPage合并文档是不好的做法。请阅读我写的关于 iText 的书的第 6 章,你会发现无论是谁提供你复制的代码示例都是错误的。您应该使用PdfCopy来合并文件,而不是PdfWriter!

例如,阅读以下 StackOverflow 答案:

如何在itextSharp(dll)中保留原始旋转页面

如何合并多个pdf文件(在运行时生成)?

Itext pdf合并:pdf(文本被截断)页面外的文档溢出并且不显示

等等...

如果您接受这个答案的速度足够快,那么您可能会很幸运,在发布已经回答的问题之前不会因为没有搜索档案而被否决。

于 2013-04-25T17:36:16.363 回答