-
Jun 26th, 2007, 08:50 AM
#1
Merge Pdf Files and Add Bookmarks to It (Using iTextSharp)
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. ???
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
-
Jun 30th, 2007, 02:13 PM
#2
Hyperactive Member
Re: Merge Pdf Files and Add Bookmarks to It (Using iTextSharp)
Thanks for the code. Works great.
Visual Studio .NET 2005/.NET Framework 2.0
-
Aug 5th, 2008, 08:45 AM
#3
New Member
Re: Merge Pdf Files and Add Bookmarks to It (Using iTextSharp)
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
-
Aug 5th, 2008, 08:52 AM
#4
New Member
Re: Merge Pdf Files and Add Bookmarks to It (Using iTextSharp)
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
-
Dec 3rd, 2008, 06:15 PM
#5
New Member
Re: Merge Pdf Files and Add Bookmarks to It (Using iTextSharp)
Code worked great got me off the ground with a running start!!! Thank You.
-
Jan 9th, 2009, 09:48 AM
#6
New Member
Re: Merge Pdf Files and Add Bookmarks to It (Using iTextSharp)
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?
-
Jan 9th, 2009, 01:45 PM
#7
Re: Merge Pdf Files and Add Bookmarks to It (Using iTextSharp)
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.
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Jan 10th, 2009, 02:12 AM
#8
New Member
Re: Merge Pdf Files and Add Bookmarks to It (Using iTextSharp)
Thanks! application is going to run in online mode. please let me know a good web service to convert html to pdf.
-
Apr 16th, 2009, 12:41 PM
#9
Member
Re: Merge Pdf Files and Add Bookmarks to It (Using iTextSharp)
hello -
Sorry I am an extreme newbie at this vb.net - but i copy your code and am curious where is the output?
-
Apr 17th, 2009, 07:45 AM
#10
Re: Merge Pdf Files and Add Bookmarks to It (Using iTextSharp)
Originally Posted by cthai
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:
Code:
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
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Apr 17th, 2009, 01:14 PM
#11
Member
Re: Merge Pdf Files and Add Bookmarks to It (Using iTextSharp)
Originally Posted by stanav
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:
Code:
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
Thank you for your guidance =) it work now
-
Apr 24th, 2009, 01:53 PM
#12
New Member
Re: Merge Pdf Files and Add Bookmarks to It (Using iTextSharp)
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!
-
Apr 24th, 2009, 04:35 PM
#13
New Member
Re: Merge Pdf Files and Add Bookmarks to It (Using iTextSharp)
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...
-
Jul 20th, 2009, 01:36 AM
#14
Fanatic Member
Re: Merge Pdf Files and Add Bookmarks to It (Using iTextSharp)
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.
Visual Studio.net 2010
If this post is useful, rate it
-
Jul 20th, 2009, 09:12 AM
#15
Re: Merge Pdf Files and Add Bookmarks to It (Using iTextSharp)
Originally Posted by 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.
Are you talking about hyperlinks or bookmark links?
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Jul 21st, 2009, 01:19 AM
#16
Fanatic Member
Re: Merge Pdf Files and Add Bookmarks to It (Using iTextSharp)
Originally Posted by stanav
Are you talking about hyperlinks or bookmark links?
am talking about hyperlinks..
Visual Studio.net 2010
If this post is useful, rate it
-
Sep 16th, 2009, 02:10 AM
#17
Member
Re: Merge Pdf Files and Add Bookmarks to It (Using iTextSharp)
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?
-
Sep 21st, 2010, 02:59 AM
#18
New Member
Re: Merge Pdf Files and Add Bookmarks to It (Using iTextSharp)
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?
-
Nov 26th, 2010, 09:58 AM
#19
Hyperactive Member
Re: Merge Pdf Files and Add Bookmarks to It (Using iTextSharp)
Hey thanks for great code.
can i read the Hyperlink in PDF file. ?
if yes please provide me code ...
thanks in advance
koolprasad2003
MCP, MCTS, Microsoft MVP [Asp.Net/IIS]
For more .NET development tips visit .NET Tips
If the post is useful then please Rate it
-
Feb 10th, 2011, 01:35 PM
#20
New Member
Re: Merge Pdf Files and Add Bookmarks to It (Using iTextSharp)
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.
-
Aug 1st, 2011, 11:01 AM
#21
New Member
Re: Merge Pdf Files and Add Bookmarks to It (Using iTextSharp)
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
Originally Posted by 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
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
-
Mar 22nd, 2013, 01:01 AM
#22
Fanatic Member
Re: Merge Pdf Files and Add Bookmarks to It (Using iTextSharp)
Hi Stanav,
is it possible to merge multiple images to single pdf using iTextSharp?
Visual Studio.net 2010
If this post is useful, rate it
-
Mar 22nd, 2013, 07:29 AM
#23
Re: Merge Pdf Files and Add Bookmarks to It (Using iTextSharp)
Originally Posted by vijy
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...
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Dec 19th, 2013, 11:29 AM
#24
New Member
Re: Merge Pdf Files and Add Bookmarks to It (Using iTextSharp)
I have registered at this forum just to say thank you.
-
May 4th, 2018, 10:59 AM
#25
New Member
Re: Merge Pdf Files and Add Bookmarks to It (Using iTextSharp)
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...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|