0

I have to merge multiple pdf's into one pdf. I'm using iTextSHarp 5.5.10.0 to accomplish this, and it work fine when I merge small files( 2 or more, with 20 pages each), and even work when I try to merge 2 files, one with 3000 pages and the second with 1300 pages, but when I try to merge 3 pdf's with 3000 pages each, I get an out of memory exception.

I don't know how to solve this. I'm using fileStream and not memoryStream.

I took the code from the answer to this question:

VB.Net Merge multiple pdfs into one and export

        Public Function MergePdfFiles(ByVal pdfFiles() As String, ByVal outputPath As String) As Boolean
        Dim result As Boolean = False
        Dim pdfCount As Integer = 0     'total input pdf file count
        Dim f As Integer = 0    'pointer to current input pdf file
        Dim fileName As String
        Dim reader As iTextSharp.text.pdf.PdfReader = Nothing
        Dim pageCount As Integer = 0
        Dim pdfDoc As iTextSharp.text.Document = Nothing    'the output pdf document
        Dim writer As PdfWriter = Nothing
        Dim cb As PdfContentByte = Nothing

        Dim page As PdfImportedPage = Nothing
        Dim rotation As Integer = 0

        Try
            pdfCount = pdfFiles.Length
            If pdfCount > 1 Then
                'Open the 1st item in the array PDFFiles
                fileName = pdfFiles(f)
                reader = New iTextSharp.text.pdf.PdfReader(fileName)
                'Get page count
                pageCount = reader.NumberOfPages

                pdfDoc = New iTextSharp.text.Document(reader.GetPageSizeWithRotation(1), 18, 18, 18, 18)

                writer = PdfWriter.GetInstance(pdfDoc, New FileStream(outputPath, FileMode.OpenOrCreate))


                With pdfDoc
                    .Open()
                End With
                'Instantiate a PdfContentByte object
                cb = writer.DirectContent
                'Now loop thru the input pdfs
                While f < pdfCount
                    'Declare a page counter variable
                    Dim i As Integer = 0
                    'Loop thru the current input pdf's pages starting at page 1
                    While i < pageCount
                        i += 1
                        'Get the input page size
                        pdfDoc.SetPageSize(reader.GetPageSizeWithRotation(i))
                        'Create a new page on the output document
                        pdfDoc.NewPage()
                        'If it is the 1st page, we add bookmarks to the page
                        'Now we get the imported page
                        page = writer.GetImportedPage(reader, i)
                        'Read the imported page's rotation
                        rotation = reader.GetPageRotation(i)
                        'Then add the imported page to the PdfContentByte object as a template based on the page's rotation
                        If rotation = 90 Then
                            cb.AddTemplate(page, 0, -1.0F, 1.0F, 0, 0, reader.GetPageSizeWithRotation(i).Height)
                        ElseIf rotation = 270 Then
                            cb.AddTemplate(page, 0, 1.0F, -1.0F, 0, reader.GetPageSizeWithRotation(i).Width + 60, -30)
                        Else
                            cb.AddTemplate(page, 1.0F, 0, 0, 1.0F, 0, 0)
                        End If
                    End While
                    'Increment f and read the next input pdf file
                    f += 1
                    If f < pdfCount Then
                        fileName = pdfFiles(f)
                        reader = New iTextSharp.text.pdf.PdfReader(fileName)
                        pageCount = reader.NumberOfPages
                    End If
                End While
                'When all done, we close the document so that the pdfwriter object can write it to the output file
                pdfDoc.Close()
                result = True
            End If
        Catch ex As Exception
            Return False
        End Try
        Return result
   End Function

I get the Exception from this line, in the end of the while, when trying to read the second file:

reader = New iTextSharp.text.pdf.PdfReader(fileName)

How can I solve this?

shlomi
  • 523
  • 3
  • 13
  • 26
  • 1
    Please throw away your code. You are merging the files incorrectly. You throw away all the interactivity: annotations, links, fields,... If they existed in the original document, they are all lost in the combined document because you aren't using the right class to merge the documents. Also: why aren't you using a more recent version of iText? – Bruno Lowagie Nov 29 '17 at 12:47
  • @BrunoLowagie This is the version that I have, wrote iText in google and download the first thing. Will 5.5.12 is the thing that make the different? Do you have an example of other code? what is the right class? – shlomi Nov 30 '17 at 09:02
  • Go to the official web site. The last version is iText 7, not 5. The 5.5.12 version you mention is a maintenance release. iText 5 is being discontinued, so please upgrade to the latest iText 7 version! – Bruno Lowagie Nov 30 '17 at 11:03
  • @BrunoLowagie Will it solve the memory problem? If the pdfReader is the same, I don't know how it helps.. Maybe you have a code that work and doing the merging in the correct way? All of this comments are not helping.. – shlomi Nov 30 '17 at 11:57
  • iText 7 is a complete rewrite of iText 5, so although there's still a `PdfReader` class, it's has been completely changed. – Bruno Lowagie Nov 30 '17 at 12:30
  • @BrunoLowagie Do you have a link to iText 7 dll? I can't find it.. – shlomi Nov 30 '17 at 12:57
  • @shlomi *"This is the version that I have, wrote iText in google and download the first thing."* - the solution you used indeed was from the accepted answer... but that answer was also downvoted. Thus, others appear to have had issues with it. in particular read Bruno's comment underneath it. – mkl Nov 30 '17 at 13:05
  • @shlomi Are you living in a place where access to https://itextpdf.com is blocked? We have a download hub here: https://developers.itextpdf.com/downloads Let me know if you can't access the official web site. – Bruno Lowagie Nov 30 '17 at 13:50

0 Answers0