When organizing files, merging multiple documents of the same type is an effective method to achieve document classification, and is also convenient for document management or document transmission. Of course, you can also split some larger files to get some of the documents you want. The ability to merge and split documents at will undoubtedly provides us with great convenience. The following will introduce a method of how to split and merge PDF documents. This article is reproduced from: http://www.cnblogs.com/Yesi/p/5604166.html
Required tools : Free Spire.PDF for .NET
Visual Studio
1. Merge multiple PDF documents
C#
using System; using Spire.Pdf; namespace MergePDF { class Program { static void Main(string[] args) { //Create a set of array instances, the array elements are the paths of multiple PDF documents that need to be merged String[] files = new String[] { "test1.pdf", "test2.pdf", "test3.pdf" }; //Call the method MergeFiles() to merge documents PdfDocumentBase doc = PdfDocument.MergeFiles(files); // save the document doc.Save("Merge.pdf", FileFormat.PDF); } } }
After debugging the runner, generate the documentation
Note: The PDF document merged here is a document that is merged with a new page. The purpose is not to merge multiple documents into a document with certain logic, but to facilitate document management and other operations.
2. Split PDF documents
Splitting a document here is also divided into splitting by page and splitting by the number of pages in a specified range .
(1) Split by each page
C#
using System; using Spire.Pdf; namespace SplitPDF1 { class Program { static void Main(string[] args) { //Initialize an instance of the PdfDocument class and load the PDF document that needs to be split from the file PdfDocument doc = new PdfDocument(@"C:\Users\Administrator\Desktop\test.pdf"); //Call the method Split() method to split the PDF document into pages and save it String pattern = "Split {0}.pdf"; doc.Split(pattern); } } }
Split result:
(2) Split by the specified number of pages
C#
using Spire.Pdf; using Spire.Pdf.Graphics; using System.Drawing; namespace SplitPDF2 { class Program { static void Main(string[] args) { //Create a PdfDocument class object and load an existing PDF document PdfDocument pdf = new PdfDocument(); pdf.LoadFromFile(@"C:\Users\Administrator\Desktop\测试.pdf"); //Create a new PDF document PdfDocument pdf1 = new PdfDocument(); PdfPageBase page; // Split pages 1-5 of the existing PDF document into one document for (int i = 0; i < 4; i++) { //Add a page to the new document that is the same size as the existing document page page = pdf1.Pages.Add(pdf.Pages[i].Size, new PdfMargins(0)); //Create a template for the pages of the existing document and draw the template on the pages of the new document pdf.Pages[i].CreateTemplate().Draw(page, new PointF(0, 0)); } // save the document pdf1.SaveToFile("1-5.pdf"); //Create a second PDF document PdfDocument pdf2 = new PdfDocument(); // Split pages 5-10 of the existing PDF document into another document for (int i = 4; i < 9; i++) { //Add a page to the new document that is the same size as the existing document page page = pdf2.Pages.Add(pdf.Pages[i].Size, new PdfMargins(0)); //Create a template for the pages of the existing document and draw the template on the pages of the new document pdf.Pages[i].CreateTemplate().Draw(page, new PointF(0, 0)); } // save the document pdf2.SaveToFile("6-10.pdf"); //Create a third PDF document PdfDocument pdf3 = new PdfDocument(); // Split pages 10-15 of the existing PDF document into another document for (int i = 9; i < 14; i++) { //Add a page to the new document that is the same size as the existing document page page = pdf3.Pages.Add(pdf.Pages[i].Size, new PdfMargins(0)); //Create a template for the pages of the existing document and draw the template on the pages of the new document pdf.Pages[i].CreateTemplate().Draw(page, new PointF(0, 0)); } // save the document pdf2.SaveToFile("11-15.pdf"); } } }