How to reduce file size while merging PDF documents in C# and VB.Net
This article explains how to reduce PDF file size while merging PDF document using Syncfusion PDF Library in C# and VB.Net
Reducing the size of the PDF file while merging PDF document in C# and VB.Net:
Essential PDF provides support for optimization of memory using EnableMemoryOptimization
property in the PdfDocument 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 EnableMemoryOptimization 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 EnableMemoryOptimization, 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.
- Install the Syncfusion.Pdf.WinForms NuGet package as a reference to your .NET standard applications from NuGet.org.
- Include the following namespaces in Form.cs file:
C#:
using Syncfusion.Pdf; using Syncfusion.Pdf.Parsing;
VB.Net:
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.
C#:
//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);
VB.NET
'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)
A complete working sample can be downloaded from ReduceFileSize.zip.
By executing the program, the original PDF document size is reduced, and you will get the optimized PDF.
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.