How to Append, Merge, and Import Pages from the PDF in WinForms PDF?
Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can merge, append, and import pages from PDF documents.
Best practices to append, merge, and import pages from PDF documents
While performing append, merge, and import pages from PDF documents, by default the EnableMemoryOptimization property is set to true. Effective from Essential Studio product version 17.1.0.32, we have changed this behavior and set the EnableMemoryOptimization property to false by default.
Please refer to the details below, which explain enabling and disabling of the EnableMemoryOptimization property while performing append, merge, and import pages from PDF documents.
EnableMemoryOptmization=true
If you need to optimize memory, you can set EnableMemoryOptimization to true. It will optimize the memory only, but differences in time will occur based on the document size. While setting this property to true, we clone each and every object from the existing PDF document. Therefore, you can close the existing document before saving the new PDF document.
Please refer to the code example below for more details:
Appending a PDF document:
// Loads the PDF document
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
// Creates a new PDF document
PdfDocument document = new PdfDocument();
// Set EnableMemoryOptimization to true
document.EnableMemoryOptimization = true;
// Appending the document with the source document
document.Append(loadedDocument);
// Close the loaded document
loadedDocument.Close(true);
// Save the document
document.Save("Sample.pdf");
// Close the new document
document.Close(true);
Merging a PDF document:
//Loads the PDF document
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
//Creates a new PDF document
PdfDocument document = new PdfDocument();
//Set EnableMemoryOptimization to true
document.EnableMemoryOptimization = true;
//Appending the document with the source document
PdfDocumentBase.Merge(document, loadedDocument);
//Close the loaded document
loadedDocument.Close(true);
//Save the document
document.Save("Sample.pdf");
//Close the new document
document.Close(true);
Importing pages from a PDF document:
// Loads the PDF document
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
// Creates a new PDF document
PdfDocument document = new PdfDocument();
// Set EnableMemoryOptimization to true
document.EnableMemoryOptimization = true;
// Imports the pages from the loaded document
document.ImportPageRange(loadedDocument, 0, loadedDocument.Pages.Count - 1);
// Close the loaded document
loadedDocument.Close(true);
// Save the document
document.Save("Sample.pdf");
// Close the new document
document.Close(true);
EnableMemoryOptimization=false
If you need to improve performance, you can disable the EnableMemoryOptimization property, or by default, it is set to false. Performance will be increased, but memory optimization will be lower compared to enabling this property. If it is set to false, you need to close the existing PDF document only after saving the new PDF document.
Please refer to the code example below for more details:
Appending a PDF document:
// Loads the PDF document
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
// Creates a new PDF document
PdfDocument document = new PdfDocument();
// Appending the document with the source document
document.Append(loadedDocument);
// Save the document
document.Save("Sample.pdf");
// Close the loaded document
loadedDocument.Close(true);
// Close the new document
document.Close(true);
Merging PDF document:
// Loads the PDF document
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
// Creates a new PDF document
PdfDocument document = new PdfDocument();
// Appending the document with the source document
PdfDocumentBase.Merge(document, loadedDocument);
// Save the document
document.Save("Sample.pdf");
// Close the loaded document
loadedDocument.Close(true);
// Close the new document
document.Close(true);
Import pages from PDF document:
// Loads the PDF document
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
// Creates a new PDF document
PdfDocument document = new PdfDocument();
// Imports the pages from the loaded document
document.ImportPageRange(loadedDocument, 0, loadedDocument.Pages.Count - 1);
// Save the document
document.Save("Sample.pdf");
// Close the loaded document
loadedDocument.Close(true);
// Close the new document
document.Close(true);
Take a moment to peruse the documentation, where you can find options like merging multiple documents from disk and stream, importing pages from multiple documents, and splitting a PDF document with code examples.
Refer here to explore the rich set of Syncfusion Essential PDF features.
Conclusion
I hope you enjoyed learning about how to append, merge, and import pages from PDFs in WinForms PDF.
You can refer to our WinForms PDF feature tour page to learn about its other groundbreaking features and documentation, and how to quickly get started with configuration specifications. You can also explore our WinForms PDF examples to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to explore other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums or feedback portal. We are always happy to assist you!
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering a Syncfusion® license key in your application to use the components without a trial message.