# VBForums CodeBank > CodeBank - Visual Basic .NET >  Merge Pdf Files and Add Bookmarks to It (Using iTextSharp)

## stanav

Hello all,
A while ago I posted a little demo project that merges pdf files and adds bookmarks to the merged output file using PDFBox (see this thread http://www.vbforums.com/showthread.php?t=462929 ). However, when I recently started to use iTextSharp for manipulating Pdfs, I found a better way of merging pdfs and adding bookmarks to it using iTextSharp. These are a few advantages I found:
1. iTextSharp.dll is small (only 2.3 MB vs PDFBox's 16MB)
2. iTextSharp is significantly faster for manipulating pdf documents
3. Can add bookmarks while merging using itextSharp (with PDFBox, I first had to merge the files to a temp location, then reopen it to add bookmarks, then save it to the final location, then delete the temp file... pheeewww, too much work!)
4. You can fill in from here  :Smilie: 
5. ???
6. ???

All in all, I highly recommend using iTextSharp over PDFBox.

And here it is, the pdf merging function that utilizes iTextSharp libray. Just remember to add a reference to itextsharp.dll in your project if you this function. (You can google for download locations of iTextSharp if you don't already have it)


vb Code:
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 = 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 PdfWriter = Nothing
        Dim cb As PdfContentByte = Nothing
        'Declare a variable to hold the imported pages
        Dim page As 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.Color.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 = PdfWriter.GetInstance(pdfDoc, New IO.FileStream(outputPath, IO.FileMode.Create))
                'Set metadata and open the document
                With pdfDoc
                    .AddAuthor("Your name here")
                    .AddCreationDate()
                    .AddCreator("Your program name here")
                    .AddSubject("Whatever subject you want to give it")
                    'Use the filename as the title... You can give it any title of course.
                    .AddTitle(IO.Path.GetFileNameWithoutExtension(outputPath))
                    'Add keywords, whatever keywords you want to attach to it
                    .AddKeywords("Report, Merged PDF, " & IO.Path.GetFileName(outputPath))
                    .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
                        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
                    End If
                End While
                'When all done, we close the documwent 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

----------


## PENNYSTOCK

Thanks for the code.  Works great.

----------


## mia6224

Hi,

I tried your code, it seems to run well but unfortunately, it doesn't create the output file.
Should the output pdf file exists before running the program?
Here is my code:
        Dim y(1) As String
        y(0) = MyPath & "Report1.pdf"
        y(1) = MyPath & "Report2.pdf"

        x = MergePdfFiles(y, MyPath & "me.pdf")

in the function, I have copied exactly your code.

Help

Mia

----------


## mia6224

Hi,

I tried your code, it seems to run well but unfortunately, it doesn't create the output file.
Should the output pdf file exists before running the program?
Here is my code:
Dim y(1) As String
y(0) = MyPath & "Report1.pdf"
y(1) = MyPath & "Report2.pdf"

x = MergePdfFiles(y, MyPath & "me.pdf")

in the function, I have copied exactly your code.

Help

Mia

----------


## Ampplee

Code worked great got me off the ground with a running start!!! Thank You.

----------


## angelag

Does .NET version of iTextSharp support converting html to pdf? if not, how can i convert html output of a text editor from browser, to xml that is compatible with iTextSharp?

----------


## stanav

As I just answered to a member earlier (maybe you?) that iTextSharp is not the best tool for html to pdf conversion. It can only do every simple web pages (pages with only plain text, no css, no tables, no images...) You need to use other tools that is designed for html to pdf conversion. Have a look at HtmlDoc or ICEBrowser. There are also free web services out there that you can use, but this approach obviously requires you have Internet connection whenever your application is running.

----------


## angelag

Thanks! application is going to run in online mode. please let me know a good web service to convert html to pdf.

----------


## cthai

hello -

Sorry I am an extreme newbie at this vb.net - but i copy your code and am curious where is the output?

----------


## stanav

> hello -
> 
> Sorry I am an extreme newbie at this vb.net - but i copy your code and am curious where is the output?


Do you know how to call a function or a sub?
If you copied that function as is, you see that it takes 2 parameters. The first one is an array of string which contains the paths to the pdf files you wish to merge. The 2nd parameter is a string that you specify the path of the output (merged) pdf file. For example, if you have the following pdf files:
c:\1.pdf, c:\2.pdf, c:\3.pdf and you want to merge them all to a file called merged.pdf in the c:\ drive. You would set it up like this:


```
Dim input() as string = {"c:\1.pdf", "c:\2.pdf", "c:\3.pdf"}
Dim output as string = "c:\merged.pdf"
'Then call the function to merge the files
If MergePdfFiles(input, output) = True Then
      MessageBox.Show("Files successfully merged.")
Else
      MessageBox.Show("Files did not merged successfully")
End If
```

----------


## cthai

> Do you know how to call a function or a sub?
> If you copied that function as is, you see that it takes 2 parameters. The first one is an array of string which contains the paths to the pdf files you wish to merge. The 2nd parameter is a string that you specify the path of the output (merged) pdf file. For example, if you have the following pdf files:
> c:\1.pdf, c:\2.pdf, c:\3.pdf and you want to merge them all to a file called merged.pdf in the c:\ drive. You would set it up like this:
> 
> 
> ```
> Dim input() as string = {"c:\1.pdf", "c:\2.pdf", "c:\3.pdf"}
> Dim output as string = "c:\merged.pdf"
> 'Then call the function to merge the files
> ...


Thank you for your guidance =) it work now

----------


## freakinhuge

I'm stumped. How do you get the paragraph text to be on top of the original pdf. And also how can I take the numbers off of the chapters. Thanks!

----------


## freakinhuge

I figured out how to take the numbers off of the chapters...
chpter.NumberDepth = 0

Still can't figure out how to get it on top of the original pdf though...

----------


## vijy

Hi stanav, 

   After merging the PDF, the links in the input PDF has disabled in the output(Merged) PDF.Is there any way to Keep the link as it is.

----------


## stanav

> Hi stanav, 
> 
>    After merging the PDF, the links in the input PDF has disabled in the output(Merged) PDF.Is there any way to Keep the link as it is.


Are you talking about hyperlinks or bookmark links?

----------


## vijy

> Are you talking about hyperlinks or bookmark links?


am talking about hyperlinks..

----------


## Max Brown

Hi everyone,

I am trying to find a way to convert html code to pdf. I have been seeing many references to iTextSharp and that it is a good tool to convert html to pdf. But I notice that Stanov specifically says it only converts very simple html.  I am looking into HTMLDoc and ICEBrowser currently but was wondering if anyone had any examples of how to convert html using one of these tools?

----------


## ton.oosterhoff

I used the code and it works great. What I would like to do is add a table of contents to my merged-pdf-files document. Would that be possible, e.g. based on bookmarks or chapters?

----------


## koolprasad2003

Hey thanks for great code.

can i read the Hyperlink in PDF file. ?

if yes please provide me code ... :Frown: 

thanks in advance
koolprasad2003 :Smilie:

----------


## JesusMtz

Thanks a lot. It works perfectly

For those that use VB 2010, at declaring the bookmark just write 12 instead of _12 and use BaseColor instad of Color.

----------


## sxia

Hi All,

The code "MergePdfFiles" works fine for me to merge pdf.  But it is required for me that the output pdf should be page numbered consecutively throughout the file.

Please help.

Thanks


sxia






> Hello all,
> A while ago I posted a little demo project that merges pdf files and adds bookmarks to the merged output file using PDFBox (see this thread http://www.vbforums.com/showthread.php?t=462929 ). However, when I recently started to use iTextSharp for manipulating Pdfs, I found a better way of merging pdfs and adding bookmarks to it using iTextSharp. These are a few advantages I found:
> 1. iTextSharp.dll is small (only 2.3 MB vs PDFBox's 16MB)
> 2. iTextSharp is significantly faster for manipulating pdf documents
> 3. Can add bookmarks while merging using itextSharp (with PDFBox, I first had to merge the files to a temp location, then reopen it to add bookmarks, then save it to the final location, then delete the temp file... pheeewww, too much work!)
> 4. You can fill in from here 
> 5. ???
> 6. ???
> 
> ...

----------


## vijy

Hi Stanav,
is it possible to merge multiple images to single pdf using iTextSharp?

----------


## stanav

> Hi Stanav,
> is it possible to merge multiple images to single pdf using iTextSharp?


Yes. You create a new pdf file and add the images to the page on the fly...

----------


## federicosis

I have registered at this forum just to say thank you.

----------


## mpour

Is it possible to Merge multiple PDF files from folder and merge into one file per folder name and create hierarchical bookmarking of given document type and name of the doc(filename) itself..
example

Folder A
file1
       A.pdf
       B.pdf
       C.pdf
file2
       A.pdf
       B.pdf
       C.pdf

file3
       A.pdf
       B.pdf
       C.pdf


out put would be file1.pdf
file2pdf
and file3 pdf

the bookmarking would be given document type as parent bookmark
and then A (#of pages)
               B(#of pages)

and so on...

----------

