It's a frequent need to merge two or more PDFs like reports, brochures, product manuals, curriculum vitae, etc. ComPDFKit PDF SDK allows you to quickly and easily combine multiple PDF files into one new PDF document without having to render every single page of each PDF separately. The original PDF data will be preserved in the new document like annotations, font size, styles, and colors.
In this blog post, we will show you how simple it can be to merge two or more PDFs with ComPDFKit PDF SDK. Whether you need to combine the whole PDFs or the specific pages of PDF files, we are all here to help.
Merge the Whole PDF
ComPDFKit PDF SDK allows you to instantiate multiple CPDFDocument
, and you can use the CPDFDocument
API to merge two or more PDF files into a single one.
To merge PDF documents into one file, please use the following method:
1. Create a blank PDF document.
CPDFDocument document = CPDFDocument.CreateDocument();
2. Open the PDF documents that contain the pages you want to merge.
// File path
CPDFDocument document1 = CPDFDocument.InitWithFilePath("filePath");
// File path
CPDFDocument document2 = CPDFDocument.InitWithFilePath("filePath2");
3. Merge all the pages from the documents you just opened, and import them into the blank PDF document.
document.ImportPagesAtIndex(document1,"",document.PageCount);
document.(document2,"",document.PageCount);
4. Save the document.
// Save path
document.WriteToFilePath("savePath");
Choose Specific Pages to Merge
Looking to merge or add specific pages from one document to another, you can use pageRange
of CPDFDocument.ImportPagesAtIndex(CPDFDocument otherDocument,string pageRange,int pageIndex)
to set specific pages.
1. First, you should follow the steps below:
- Create a blank PDF document.
CPDFDocument document = CPDFDocument.CreateDocument();
- Open the PDF documents that contain the pages you want to merge.
// File path
CPDFDocument document1 = CPDFDocument.InitWithFilePath("filePath");
// File path
CPDFDocument document2 = CPDFDocument.InitWithFilePath("filePath2");
2. Then, choose the pages you want to merge. Merge all the pages you just chose, and import it into the blank PDF document. The code for this step looks like this:
document.ImportPagesAtIndex(document1,"1-10",document.PageCount);
document.ImportPagesAtIndex(document2,"1-10",document.PageCount);
3. At last, save the document.
// Save path
document.WriteToFilePath("savePath");
How to integrate
To get started, you need a ComPDFKit PDF SDK license key first and download the package of the ComPDFKit PDF SDK for Windows. All you need to do is contact us for a free trial. Then, follow the steps below to integrate the merging function.
1. Create a new Windows project in C#.
2. Integrate ComPDFKit PDF SDK into your project.
3. Apply the license key.
4. Display a PDF document.
You can find the detailed steps and code samples in our document.
More about Merging
You can combine them with the merging tool we introduced above to get a complete workflow to deal with documents. More functions are waiting for you to explore.
- Conversion: Convert the PDF files you just merged to editable files like Word, Excel, and Powerpoint
- Annotation: Annotate your merged documents for work or study
- Viewer: After merging, you can choose the display modes, themes, thumbnails, outline, bookmarks, and more to satisfy your needs
- Document editor: After merging, you can insert, replace, or extract pages as you want
- Security: Add a watermark on the files you have merged to clarify ownership
Conclusion
Now you should know how to combine PDFs with ComPDFKit. Contact us and try our PDF library for free. Check out our developer guides for more examples and other important features of the SDK.