I found a source code for merging pdf files, I don't know if you know about this module
PdfManipulation2.vb. This is the code form merging pdf files
Public Shared Function MergePdfFiles(ByVal pdfFiles() As String, ByVal outputPath As String, _
Optional ByVal authorName As String = "", _
Optional ByVal creatorName As String = "", _
Optional ByVal subject As String = "", _
Optional ByVal title As String = "", _
Optional ByVal keywords As String = "") As Boolean
Dim result As Boolean = False
Dim pdfCount As Integer = 0
Dim f As Integer = 0
Dim fileName As String = String.Empty
Dim reader As iTextSharp.text.pdf.PdfReader = Nothing
Dim pageCount As Integer = 0
Dim pdfDoc As iTextSharp.text.Document = Nothing
Dim writer As iTextSharp.text.pdf.PdfWriter = Nothing
Dim cb As iTextSharp.text.pdf.PdfContentByte = Nothing
Dim page As iTextSharp.text.pdf.PdfImportedPage = Nothing
Dim rotation As Integer = 0
Dim bookmarkFont As iTextSharp.text.Font = iTextSharp.text.FontFactory.GetFont(iTextSharp.text.FontFactory.HELVETICA, _
12, iTextSharp.text.Font.BOLD, iTextSharp.text.BaseColor.BLUE)
Try
pdfCount = pdfFiles.Length
If pdfCount > 1 Then
fileName = pdfFiles(f)
reader = New iTextSharp.text.pdf.PdfReader(fileName)
pageCount = reader.NumberOfPages
pageCount = frmMerger.GetNumberOfPdfPages(fileName)
pdfDoc = New iTextSharp.text.Document(reader.GetPageSizeWithRotation(1), 18, 18, 18, 18)
writer = iTextSharp.text.pdf.PdfWriter.GetInstance(pdfDoc, New System.IO.FileStream(outputPath, IO.FileMode.Create))
With pdfDoc
.AddAuthor(authorName)
.AddCreationDate()
.AddCreator(creatorName)
.AddProducer()
.AddSubject(subject)
.AddTitle(title)
.AddKeywords(keywords)
.Open()
End With
cb = writer.DirectContent
frmMerger.ProgressBar1.Value = 0
frmMerger.ProgressBar1.Maximum = pdfCount
While f < pdfCount
Application.DoEvents()
frmMerger.ProgressBar1.Value = frmMerger.ProgressBar1.Value + 1
frmMerger.lblStatus.Text = "Status: Merging " & New IO.FileInfo(fileName).Name
Dim i As Integer = 0
While i < pageCount
i += 1
pdfDoc.SetPageSize(reader.GetPageSizeWithRotation(i))
pdfDoc.NewPage()
page = writer.GetImportedPage(reader, i)
rotation = reader.GetPageRotation(i)
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
f += 1
If f < pdfCount Then
fileName = pdfFiles(f)
reader = New iTextSharp.text.pdf.PdfReader(fileName)
pageCount = reader.NumberOfPages
pageCount = frmMerger.GetNumberOfPdfPages(fileName)
End If
End While
pdfDoc.Close()
result = True
End If
Catch ex As Exception
Throw New Exception(ex.Message)
End Try
Return result
End Function
My problem is this
pageCount = reader.NumberOfPages
This code is not accurate, the actual page count in pdf is 20, the code returns 30.
Can someone tell what causes that?
To temporary solve the problem I use another code the get the Total Pages...
pageCount = frmMerger.GetNumberOfPdfPages(fileName)
Public Function GetNumberOfPdfPages(ByVal fileName As String) As Integer
Using sr As New StreamReader(File.OpenRead(fileName))
Dim regex As New System.Text.RegularExpressions.Regex("/Type\s*/Page[^s]")
Dim matches As System.Text.RegularExpressions.MatchCollection = regex.Matches(sr.ReadToEnd())
Return matches.Count
End Using
End Function