C# tutorial: combine PDF files


Combine PDF files

In the previous tutorial, you learnt to read pages from a PDF file and insert them to another PDF file by using the PdfStamper class.In this tutorial, I am going to show you how to concatenate or combine two PDF files to a single PDF file. In fact, you can use the PdfStamper class to merge PDF files. However, interactive features of the PDF document will lose. A better way to merge PDF files is using the PdfCopy class. One frequently used interactive feature of PDF files is link. By using the PdfCopy class, the links still work in the result file.



To merge source PDF files in iTextSharp, you need to create PdfReader objects to read from the source files. Then you will use the GetImportedPage method of the PdfCopy class to import pages from the readers and add them to a PdfCopy object by using the AddPage method. The pages imported by the GetImportedPage method of the PdfCopy class cannot be scaled, or rotated. You can only add the pages to a new PDF document. This is a reason of preserving the interactive features of PDF files when they are merged. You should note that when the pages of the original document are only half full, the new document will put both halves on one
page. That's not how PDFworks! In PDF, you work with complete pages; it's not possible to reflow the content on those pages.

The PdfCopy object can be created in the same way as you create the PdfWriter object.

The example C# code below will merge two PDF files: jmf_tutorial.pdf, and chart.pdf to a single PDF file called result.pdf.


String[] source_files= {"D:/jmf_tutorial.pdf","d:/chart.pdf"};
String result = "d:/result.pdf";
//create Document object
Document document = new Document();
//create PdfCopy object
PdfCopy copy = new PdfCopy(document, new FileStream(result, FileMode.Create));
//open the document
document.Open();
//PdfReader variable
PdfReader reader;
for (int i = 0; i < source_files.Length; i++)
{
//create PdfReader object
reader = new PdfReader(source_files[i]);
//merge combine pages
for(int page=1;page<=reader.NumberOfPages;page++)
  copy.AddPage(copy.GetImportedPage(reader,page));

}
//close the document object
document.Close();

combine pdf files



Comments

Francis comment

 Francis

When I ran this Code it copied the 1st pdf twice and added them to one file I want to take 2 different pdf and combine them


2017-12-11
PROCESO comment

 PROCESO

MERGE PDF


2017-09-15
Arun comment

 Arun

What is the Use of Document object here


2016-05-12



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.