0

I'm trying to merge pdf files using itextsharp.
The problem 'm getting its that any cropping or rotating I've applied to the individual files before the merge is somehow ignored. All original files were cropped and rotated as TIFFs then converted to pdf and now finally I'm trying to merge them.
I'd like the page size to match the added content, and I any rotation I've applied to come through.'

Thank you for any help,
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()
j0k
  • 22,600
  • 28
  • 79
  • 90
Corbin de Bruin
  • 35
  • 1
  • 2
  • 8

1 Answers1

0

This question has been answered over and over again on StackOverflow. It's amazing that nobody voted to close it as a duplicate.

In any case: as I've answered many times before, it is bad practice to use PdfWriter/PdfImportedPage to merge document. Please read chapter 6 of the book I wrote about iText, and you'll discover that whoever provided you with the code sample you copied was all wrong. You should use PdfCopy to merge files, not PdfWriter!

For examples, read the following StackOverflow answers:

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

and so on...

If you're fast enough in accepting this answer, you'll probably be lucky enough not do get downvoted for not searching the archives before posting an already answered question.

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • I did search the archives, and found similar answers. I admit and apologize, I was unwilling to stray to far from the example I got. I only spent about half a day from no idea what itextsharp was to successfully merging pdfs programmatically. I'm still a beginner, so that was big for me. I'll look into your solution. – Corbin de Bruin Apr 25 '13 at 20:52