How to reduce file size while merging PDF documents in C# and VB.Net
Reducing the size of the PDF file while merging PDF document in C# and VB.Net:
property in the Instance. Optimization will be effective only with merge,append and import functions.Enabling this property will optimize the memory but a difference in time occurs based on the document size
For example, if the PDF document has 100 pages and each page has an image in common. If we import those document pages with the disabled (by default it is disabled). We will clone the image resource for each page separately. That leads to the imported document having a huge size when compared to the source PDF. If we enable the , we won’t clone any resources from the source. That does not cause any PDF file size issue. Moreover, it does not cause any memory leak issues. So, without any hassle, you can enable.
Steps to reduce the pdf file size while merging PDF document programmatically:
- Create a new C# Windows Forms application project.
- Include the following namespaces in Form.cs file:
using Syncfusion.Pdf; using Syncfusion.Pdf.Parsing;
Imports Syncfusion.Pdf Imports Syncfusion.Pdf.Parsing
- Refer to the following code snippet to reduce the PDF file size while importing pages from multiple documents.
//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);
'Load the document. Dim lDoc As New PdfLoadedDocument("file1.pdf") 'Create a new document. Dim document As New PdfDocument() 'Enable memory optimization. document.EnableMemoryOptimization= true ' Import the page range from 0 to 1 from the lDoc. document.Merge(document,lDoc) 'Save the document. document.Save("sample.pdf") 'Close the document. document.Close(True) lDoc.Close(True)
By executing the program, the original PDF document size is reduced, and you will get the optimized PDF.
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.