0

Getting into very strange behavior

I have a base PDF that has multiple invoices (59) - 66 pages. Some of the invoices span 2 pages. It opens just find without any issue.

I have 59 individual pdfs (for each invoice) that has additional info about each invoice. All open just fine without any issue.

I need to merge the individual invoice into base PDF on the next page. Lets say, if invoice 103 on page 3, then individual one will get merged on page 4. So current page 4 will become page 5 in the base PDF .

We are using itextsharp for getting page no and then merging.

Now, after merge the when i open PDF it says it has an error. And at this time when i look through the PDF, the original base PDF pages are all white and the individual appends are there.

However, when i pick top 48 invoices for merging, either in ascending or descending order, then the PDF is just fine. So can't pick one individual invoice that has issue.

Any idea as to what is happening here? Is it due to number of pages being increased?

code to pick page no:

Public Shared Function FindPageNo(ByVal sourceFiles As String, ByVal startpage As Integer, ByVal invno As String) As Integer
        Dim i As Integer
        Dim str1 As String
        Dim pos1 As Integer, pos2 As Integer
        Dim bgReader As PdfReader
        Dim pagen As Integer

        If File.Exists(sourceFiles) Then
            bgReader = New PdfReader(sourceFiles)

            If startpage > bgReader.NumberOfPages Then
                FindPageNo = -1  'error. invalid
                bgReader.Close()
            End If

            pagen = 0
            For i = startpage To bgReader.NumberOfPages
                str1 = PdfTextExtractor.GetTextFromPage(bgReader, i)
                pos1 = str1.IndexOf("Invoice No:")
                pos2 = str1.IndexOf("Phone:")
                If pos2 > pos1 Then
                    If str1.Substring(pos1 + 11, pos2 - pos1 - 11).Trim.Equals(invno) = True Then
                        pagen = i  'found the page
                        'bgReader.Close()
                        'Exit Function
                    Else
                        If pagen <> 0 Then
                            'we found the page no. so no need to go further.
                            'exit now
                            FindPageNo = pagen  'last page found
                            bgReader.Close()
                            Exit Function
                        End If
                    End If
                End If
            Next i
            bgReader.Close()
        End If
        If pagen <> 0 Then
            FindPageNo = pagen  'last page found
        Else
            FindPageNo = 0  'not found
        End If

    End Function

Combine PDF files

Public Shared Sub CombinePDFFiles(ByVal OrignalFile As String, ByVal InsertFile As String, ByVal insertPage As Integer, ByVal SourceFile As String)

        Dim OrigDirectory As String = System.IO.Path.GetDirectoryName(OrignalFile)
        Dim SourceFileOnly As String = System.IO.Path.GetFileName(SourceFile)
        Dim reader As PdfReader = New PdfReader(OrignalFile)
        Dim reader1 As PdfReader = New PdfReader(InsertFile)
        Dim n As Integer = reader.NumberOfPages
        Dim m As Integer = reader1.NumberOfPages
        reader.Close()
        reader1.Close()
        'Dim fil As New System.IO.FileStream(OrigDirectory & "\" & SourceFile, FileMode.Create)
        Dim fil As New System.IO.FileStream(SourceFile, FileMode.Create)
        Dim pdfDoc As Document = New Document(iTextSharp.text.PageSize.A4)
        Dim pdfWriter As PdfWriter = pdfWriter.GetInstance(pdfDoc, fil)

        pdfDoc.Open()

        Dim f As Integer = 1
        Dim g As Integer = 1


        Dim bgReader As PdfReader
        bgReader = New PdfReader(OrignalFile)
        Dim bg As PdfImportedPage
        Dim bgReader1 As PdfReader
        bgReader1 = New PdfReader(InsertFile)
        Dim bg1 As PdfImportedPage
        While f <= n
            If (f = insertPage) Then
                While g <= m
                    bg1 = pdfWriter.GetImportedPage(bgReader1, g)
                    pdfWriter.DirectContentUnder.AddTemplate(bg1, 0, 0)
                    pdfDoc.NewPage()
                    g = g + 1
                End While
            End If

            bg = pdfWriter.GetImportedPage(bgReader, f)
            '' add the template beneath content
            pdfWriter.DirectContentUnder.AddTemplate(bg, 0, 0)
            pdfDoc.NewPage()
            f = f + 1
        End While

        'if append at last page
        If (f = insertPage) Then
            While g <= m
                bg1 = pdfWriter.GetImportedPage(bgReader1, g)
                pdfWriter.DirectContentUnder.AddTemplate(bg1, 0, 0)
                pdfDoc.NewPage()
                g = g + 1
            End While
        End If

        pdfDoc.Close()
        bgReader.Close()
        bgReader1.Close()
        fil.Close()
        pdfWriter.Close()

    End Sub

Merging code that uses above two functions

For Each dr As DataRow In myTable.Rows
                sInvoiceNo = dr("cINVNO")

                pageno = clsLabelToPDF.FindPageNo(vInvoiceFile, 1, sInvoiceNo)

                If pageno <> 0 Then
                    Dim InvFileExt As String = ".PDF"
                    If My.Computer.FileSystem.FileExists(TempDir & sInvoiceNo & InvFileExt) = True Then
                        My.Computer.FileSystem.CopyFile(vInvoiceFile, TempDir & "\temp" & InvFileExt, True)
                        clsLabelToPDF.CombinePDFFiles(TempDir & "temp" & InvFileExt, TempDir & sInvoiceNo & InvFileExt, pageno + 1, vInvoiceFile)
                    End If

                End If
            Next
learning...
  • 3,104
  • 10
  • 58
  • 96
  • Do yourself a favor and throw away your merging code. Go to the official web site, read the official documentation, and use `PdfCopy`. If you read frustration between the lines of this comment, you are right. I have explained hundreds of times that using `PdfWriter` and `AddTemplate()` is **completely wrong**. Why are you using this **completely wrong approach**? If someone inspired you, I'd really like to slap that person in the face. (I am the original developer of iText.) – Bruno Lowagie Aug 29 '17 at 18:07
  • i haven't written this, done by someone years ago... i am just debugging it and trying to figure out what is wrong with it. – learning... Aug 29 '17 at 18:32
  • I guess that developer didn't understand the "HOW **NOT** TO" in the example [How not to merge documents](http://developers.itextpdf.com/examples/merging-pdf-documents-itext5/how-not-merge-documents) and didn't read the entry on [How to merge documents correctly?](http://developers.itextpdf.com/question/how-merge-documents-correctly) – Bruno Lowagie Aug 30 '17 at 06:33
  • When down voting, please justify by providing a comment or don't bother doing a mouse click. This is a valid question, and the author of the utility has given me a pointer that has helped me re-write the functionality. @BrunoLowagie please put your comment as an answer so that i can accept it. Thanks a lot! – learning... Sep 03 '17 at 07:12
  • I didn't down-vote, but I have set the score back to 0 by casting an up-vote. Rather than adding the answer, I would close it as a duplicate of your follow-up question. In your answer to your question, you apply the knowledge I referred to in my comment. – Bruno Lowagie Sep 03 '17 at 11:05

0 Answers0