Depending on your needs and data type, the term “optimize” can have several meanings. It can mean to optimize the size of the PDF file or it can mean to organize the data within the PDF file for a specific purpose such as web viewing. Thankfully with LEADTOOLS, several PDF optimization methods are available depending on your situation.
Optimize the file size of PDF files
The following C# and Java code snippets optimize the PDF compression. It bases the compression type on the data type.
C# code
private static void OptimizePdfCompression(string sourcePdfPath, string destinationPdfPath)
{
var pdfFile = new PDFFile(sourcePdfPath) {
OptimizerOptions = new PDFOptimizerOptions {
AutoOptimizerMode = PDFAutoOptimizerMode.Customized,
ColorImageDownsamplingMode = PDFDownsamplingMode.Bicubic,
ColorImageDownsampleFactor = 2.0,
ColorImageDPI = 150,
ColorImageCompression = RasterImageFormat.Jpx,
GrayImageDownsamplingMode = PDFDownsamplingMode.Bicubic,
GrayImageDownsampleFactor = 2.0,
GrayImageDPI = 150,
GrayImageCompression = RasterImageFormat.Jpeg,
MonoImageDownsamplingMode = PDFDownsamplingMode.Bicubic,
MonoImageDownsampleFactor = 2.0,
MonoImageDPI = 150,
MonoImageCompression = RasterImageFormat.Jbig2,
EmbedAllFonts = true,
SubsetFonts = true
}
};
pdfFile.Optimize(destinationPdfPath);
}
Java code
private static void optimizePdfCompression(String sourcePdfPath, String destinationPdfPath) throws Exception {
// Set the options for the PDF optimization
final var options = new PDFOptimizerOptions();
options.setAutoOptimizerMode(PDFAutoOptimizerMode.CUSTOMIZED);
options.setColorImageDownsamplingMode(PDFDownsamplingMode.BICUBIC);
options.setColorImageDownsampleFactor(2.0);
options.setColorImageDPI(150);
options.setColorImageCompression(RasterImageFormat.JPX);
options.setGrayImageDownsamplingMode(PDFDownsamplingMode.BICUBIC);
options.setGrayImageDownsampleFactor(2.0);
options.setGrayImageDPI(150);
options.setGrayImageCompression(RasterImageFormat.JPEG);
options.setMonoImageDownsamplingMode(PDFDownsamplingMode.AVERAGE);
options.setMonoImageDownsampleFactor(2.0);
options.setMonoImageDPI(150);
options.setMonoImageCompression(RasterImageFormat.JBIG2);
options.setEmbedAllFonts(true);
options.setSubsetFonts(true);
// Load the PDF file
final var pdfFile = new PDFFile(sourcePdfPath);
// Optimize the PDF document
pdfFile.setOptimizerOptions(options);
pdfFile.optimize(destinationPdfPath);
}
Read more about the PDFOptimizerOptions class.
Optimize a PDF file for the web
A PDF file can be for optimized web viewing. When the PDF is saved for the web, the data should be organized in a way that is optimal for streaming so the PDF can be viewed as the data is received instead of waiting for the entire file to be downloaded.
The following line of C# code will optimize PDF compression for web viewing.
C# code
new PDFFile(sourcePdfPath).Linearize(destinationPdfPath);
Java code
new PDFFile(sourcePdfPath).linearize(destinationPdfPath);
Read more about the Linearize method.
With LEADTOOLS at your disposal, you can optimize any aspect of your PDF files.
See for yourself – Free evaluation
Download the LEADTOOLS SDK for free. It’s fully-functional for 60 days and comes with free chat and email support.
Need help in the meantime?
Contact our support team for free technical support! For pricing or licensing questions, you can contact our sales team via email or call us at +1-704-332-5532.