Click here to Skip to main content
14,773,285 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
''' <summary>
''' Merge multiple pdf files into a single pdf
''' </summary>
''' <param name="pdfFiles">string array containing full paths to the pdf files to be merged</param>
''' <param name="outputPath">full path to the merged output pdf</param>
''' <param name="authorName">Author's name.</param>
''' <param name="creatorName">Creator's name</param>
''' <param name="subject">Subject field</param>
''' <param name="title">Title field</param>
''' <param name="keywords">keywords field</param>
''' <returns>True if the merging is successful, False otherwise.</returns>
''' <remarks>All optional paramters are used for the output pdf metadata.
''' You can see a pdf metada by going to the PDF tab of the file's Property window.</remarks>
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     'total input pdf file count
    Dim f As Integer = 0            'pointer to current input pdf file
    Dim fileName As String = String.Empty   'current input pdf filename
    Dim reader As iTextSharp.text.pdf.PdfReader = Nothing
    Dim pageCount As Integer = 0    'cureent input pdf page count
    Dim pdfDoc As iTextSharp.text.Document = Nothing    'the output pdf document
    Dim writer As iTextSharp.text.pdf.PdfWriter = Nothing
    Dim cb As iTextSharp.text.pdf.PdfContentByte = Nothing
    'Declare a variable to hold the imported pages
    Dim page As iTextSharp.text.pdf.PdfImportedPage = Nothing
    Dim rotation As Integer = 0
    'Declare a font to used for the bookmarks
    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
            'Open the 1st pad using PdfReader object
            fileName = pdfFiles(f)
            reader = New iTextSharp.text.pdf.PdfReader(fileName)
            'Get page count
            pageCount = reader.NumberOfPages
            pageCount = frmMerger.GetNumberOfPdfPages(fileName)

            'Instantiate an new instance of pdf document and set its margins. This will be the output pdf.
            'NOTE: bookmarks will be added at the 1st page of very original pdf file using its filename. The location
            'of this bookmark will be placed at the upper left hand corner of the document. So you'll need to adjust
            'the margin left and margin top values such that the bookmark won't overlay on the merged pdf page. The
            'unit used is "points" (72 points = 1 inch), thus in this example, the bookmarks' location is at 1/4 inch from
            'left and 1/4 inch from top of the page.
            pdfDoc = New iTextSharp.text.Document(reader.GetPageSizeWithRotation(1), 18, 18, 18, 18)
            'Instantiate a PdfWriter that listens to the pdf document
            writer = iTextSharp.text.pdf.PdfWriter.GetInstance(pdfDoc, New System.IO.FileStream(outputPath, IO.FileMode.Create))
            'Set metadata and open the document
            With pdfDoc
                .AddAuthor(authorName)
                .AddCreationDate()
                .AddCreator(creatorName)
                .AddProducer()
                .AddSubject(subject)
                .AddTitle(title)
                .AddKeywords(keywords)
                .Open()
            End With
            'Instantiate a PdfContentByte object
            cb = writer.DirectContent
            'Now loop thru the input pdfs

            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

                '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

                    'REMOVE FILENAME HEADER
                    'If i = 1 Then
                    '    'First create a paragraph using the filename as the heading
                    '    Dim para As New iTextSharp.text.Paragraph(IO.Path.GetFileName(fileName).ToUpper(), bookmarkFont)
                    '    'Then create a chapter from the above paragraph
                    '    Dim chpter As New iTextSharp.text.Chapter(para, f + 1)
                    '    'Finally add the chapter to the document
                    '    pdfDoc.Add(chpter)
                    'End If
                    '----------------
                    '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
                    pageCount = frmMerger.GetNumberOfPdfPages(fileName)
                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
        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
Posted
Updated 23-Dec-20 0:33am

1 solution

A primeira função ajuda muito , porém muito erro de logica.

Segue com a correção:
Public 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 'total input pdf file count
Dim f As Integer = 0 'pointer to current input pdf file
Dim fileName As String = String.Empty 'current input pdf filename
Dim reader As iTextSharp.text.pdf.PdfReader = Nothing
Dim pageCount As Integer = 0 'cureent input pdf page count
Dim pdfDoc As iTextSharp.text.Document = Nothing 'the output pdf document
Dim writer As iTextSharp.text.pdf.PdfWriter = Nothing
Dim cb As iTextSharp.text.pdf.PdfContentByte = Nothing
'Declare a variable to hold the imported pages
Dim page As iTextSharp.text.pdf.PdfImportedPage = Nothing
Dim rotation As Integer = 0
'Declare a font to used for the bookmarks
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
'Open the 1st pad using PdfReader object
fileName = pdfFiles(f)
reader = New iTextSharp.text.pdf.PdfReader(fileName)
'Get page count
pageCount = reader.NumberOfPages


'Instantiate an new instance of pdf document and set its margins. This will be the output pdf.
'NOTE: bookmarks will be added at the 1st page of very original pdf file using its filename. The location
'of this bookmark will be placed at the upper left hand corner of the document. So you'll need to adjust
'the margin left and margin top values such that the bookmark won't overlay on the merged pdf page. The
'unit used is "points" (72 points = 1 inch), thus in this example, the bookmarks' location is at 1/4 inch from
'left and 1/4 inch from top of the page.
pdfDoc = New iTextSharp.text.Document(reader.GetPageSizeWithRotation(1), 18, 18, 18, 18)
'Instantiate a PdfWriter that listens to the pdf document
writer = iTextSharp.text.pdf.PdfWriter.GetInstance(pdfDoc, New System.IO.FileStream(outputPath, IO.FileMode.Create))
'Set metadata and open the document
With pdfDoc
.AddAuthor(authorName)
.AddCreationDate()
.AddCreator(creatorName)
.AddProducer()
.AddSubject(subject)
.AddTitle(title)
.AddKeywords(keywords)
.Open()
End With
'Instantiate a PdfContentByte object
cb = writer.DirectContent
'Now loop thru the input pdfs



While f < pdfCount
Application.DoEvents()

'Declare a page counter variable

'Increment f and read the next input pdf file

'If IsNothing(fileName) Then
fileName = pdfFiles(f)
If fileName <> "" Then
If f < pdfCount Then

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

End If




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

'REMOVE FILENAME HEADER
'If i = 1 Then
' 'First create a paragraph using the filename as the heading
' Dim para As New iTextSharp.text.Paragraph(IO.Path.GetFileName(fileName).ToUpper(), bookmarkFont)
' 'Then create a chapter from the above paragraph
' Dim chpter As New iTextSharp.text.Chapter(para, f + 1)
' 'Finally add the chapter to the document
' pdfDoc.Add(chpter)
'End If
'----------------
'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
End If
f += 1

'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
' Throw New Exception(ex.Message)
'End Try
Return result
End Function
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900