Home > Articles > .Net Framework Articles > How to merge and split the PDF files from code

How to merge and split the PDF files from code

by Jane Smith   on Oct 26, 2016   Category: .Net Framework  | Level: Beginner  |  Views: 817    |  Points: 100   
Like this article? Bookmark and Share:
Merge the PDF files together into one single PDF file document and Split the PDF files into two parts for the introduction and report contents.


Introduction


I am a .NET programmer and our current project needs to combine many PDF files from different companies into a single PDF and later, we need to depart the whole PDF file into several parts by its different contents. To keep our system clean, we are not allowed to install any Adobe Acrobat on our machine. We found that there are lots of freeware out there working for merge several PDF documents to one file. For we also need to split the PDF files later, finally we choose Free Spire.PDF for its high efficiency and independence. We can get the Spire.Pdf.dll from Nuget and then easily add it as reference for our applications.




There are two main parts of our projects, please have an overview as below:

1. Merge the PDF files together into one single PDF file document

2. Split the PDF files into two parts for the introduction and report contents.

Now we will demonstrate how to merge the PDF files into a single one. Free Spire.PDF offers a method of PdfDocument.MergeFiles() to combine the PDF files together easily without considering the structure of the files.

Firstly check the original four PDF files:




public MergePDF()

{

    String[] files = new String[]{ "Sample.pdf","Sample1.pdf", "Sample2.pdf", "Sample3.pdf" };

    string outputFile = "result.pdf";

    PdfDocumentBase doc = PdfDocument.MergeFiles(files);

    doc.Save(outputFile, FileFormat.PDF);

}


After merge into a single PDF file, we will get a whole PDF files with in total Six pages.
 


Then we need to split the result page into two single PDF files by the text introduction and reports for our later use.

public SplitPDF()

 {

     PdfDocument pdf = new PdfDocument();

     pdf.LoadFromFile("result.pdf");

     PdfDocument pdf1 = new PdfDocument();

     PdfPageBase page;

 

     for (int i = 0; i < 1; i++)

     {

         page = pdf1.Pages.Add(pdf.Pages[i].Size, new Spire.Pdf.Graphics.PdfMargins(0));

         pdf.Pages[i].CreateTemplate().Draw(page, new System.Drawing.PointF(0, 0));

     }

      pdf1.SaveToFile("Introduction.pdf");

      PdfDocument pdf2 = new PdfDocument();

      for (int i = 1; i < 6; i++)

     {

         page = pdf2.Pages.Add(pdf.Pages[i].Size, new Spire.Pdf.Graphics.PdfMargins(0));

         pdf.Pages[i].CreateTemplate().Draw(page, new System.Drawing.PointF(0, 0));

     }

      pdf2.SaveToFile("ReportData.pdf");

 }

After processing the code, we will get two PDF pages, one with one page, and the other with 5 pages. Please check the result PDF pages:


Conclusion

Merge and Split PDF files is a widely used feature for us to handle the PDF files for reports.


e" Name="heading 6"/>
Like this article? Bookmark and Share:

Most viewed Articles

User Comments


No response found, be the first to review this article.

Submit feedback about this article

Please sign in to post feedback

Latest Posts