Best practices to append, merge, and import pages from the 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 practises to append, merge and import pages from PDF document
While perform append, merge and import pages from PDF document, by default EnableMemoryOptimization property set to true. Effective from Essential Studio product version 17.1.0.32, we have changed this behaviour and set EnableMemoryOptimization property to false by default.
Please refer the below details which explains about enable and disabling of EnableMemoryOptimization property while perform append, merge and import pages from PDF document.
EnableMemoryOptmization=true
If you need to optimize the memory, you can set EnableMemoryOptimization to true. It will optimize the memory only but different in time occurs based on the document size. While setting this property to true, we have cloned each and every object from existing PDF document. So, you can close the existing document before saving of new PDF document.
Please refer the below code example for more details,
Appending 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 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 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 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 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 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 the performance, you can disable the EnableMemoryOptimization or by default it set to false. Performance will be increased but memory optimization will be low compared to enabling of this property. If it is set to false, needs to close the existing PDF document only after saving of new PDF document.
Please refer the below code example for more details,
Appending 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 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 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 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 the options like merging multiple documents from disk and stream, importing pages from multiple documents and split PDF document with code example.
Refer here to explore the rich set of Syncfusion Essential PDF features.
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 link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.