How to compress a PDF document in a Linux Docker environment
The Syncfusion Essential® PDF is a comprehensive, high-performance .NET PDF library that enables you to create, read, and edit PDF documents. Using this library, you can optimize PDF file size by compressing images and optimizing fonts and page contents.
Steps to compress a PDF document programmatically in a Linux Docker environment
- Create a new .NET Core console application: Use the following command
dotnet new console -n PDFCompressionExample
cd PDFCompressionExample
- Add the required Syncfusion® PDF package: Install the Syncfusion.Pdf.Imaging.Net.Core and SkiaSharp.NativeAssets.Linux NuGet package as a reference to your project from NuGet.org by execute the following command.
dotnet add package Syncfusion.Pdf.Imaging.Net.Core
dotnet add package SkiaSharp.NativeAssets.Linux
- Set up your environment: In the
Program.cs
file, include these namespaces.
using Syncfusion.Pdf.Parsing;
using Syncfusion.Pdf;
- Compress a PDF document: Use the following code example to reduce file size.
string path = Path.GetFullPath(@"Input.pdf");
Console.WriteLine($"'{path}' pdf compression started.");
FileStream inputDocument = new FileStream(path, FileMode.Open, FileAccess.Read);
Console.WriteLine($"Original file size: {inputDocument.Length:N0}");
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputDocument);
// Create a new compression option.
PdfCompressionOptions options = new PdfCompressionOptions
{
// Enable image compression.
CompressImages = true,
// Set the image quality.
ImageQuality = 30,
// Optimize fonts in the PDF document.
OptimizeFont = true,
// Optimize page contents.
OptimizePageContents = true,
// Remove metadata from the PDF document.
RemoveMetadata = true
};
// Compress the PDF document.
loadedDocument.Compress(options);
// Save the PDF document.
MemoryStream outputDocument = new MemoryStream();
// Save the PDF document to memory stream.
loadedDocument.Save(outputDocument);
Console.WriteLine($"Compressed file size: {outputDocument.Length:N0}");
// Clean up resources.
outputDocument.Close();
loadedDocument.Close(true);
inputDocument.Close();
- Restore the NuGet packages: Execute the following command in the terminal.
dotnet restore
- Run the application: Execute the following command in the terminal.
dotnet run
A complete working sample can be downloaded from GitHub.
By executing the program, you will see an output window like below:
Take a moment to peruse the documentation to learn how to compress a PDF document.
Conclusion
I hope you enjoyed learning on how to add radio button fields to multiple pages in a PDF document.
You can refer to our ASP.NET Core PDF feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET Core 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, Direct-Trac, or feedback portal. We are always happy to assist you!