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