How to Reduce File while Merging PDF in C# and VB.Net in WinForms PDF?
This article explains how to reduce PDF file size while merging PDF documents using Syncfusion PDF Library in C# and VB.NET.
Reducing the size of the PDF file while merging PDF documents 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#:
// Load the PDF document
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
// Create a new PDF document
PdfDocument document = new PdfDocument();
// Set EnableMemoryOptimization to true
document.EnableMemoryOptimization = true;
// Append 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);
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 options like merging multiple documents from disk and stream, importing pages from multiple documents and split PDF document with code examples.
Refer here to explore the rich set of Syncfusion Essential PDF features.
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from the trial setup or the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without a trial message.
Conclusion
I hope you enjoyed learning about how to reduce file while merging Pdf in C# and VB.Net 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 example 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 check out our 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!