pdfOptimizer: Support added for Font, Image, Color Space optimization
Background:
pdfOptimizer is iText's latest and greatest innovation in the PDF document space. It provides extensive additive functionality to our Core libraries, specifically within the realms of compression and modification of attributes within documents.
Code Sample:
Below is a fully-featured code snippet demonstrating each of pdfOptimizer's currently released features as of our 1.0.1 stable release. In this code snippet, we cover the following features:
Font Duplication Optimization:
Refers to the removal of duplicate instances of entire font files, or duplicate embedded subsets of font files within a PDF document.
Font Subsetting Optimization:
Allows the embedding of just the glyph data actually being utilized by text in a document, instead of all of the glyph data from a font file to be embedded.
Image Scaling Control & Compression:
Empowers users to both scale down the resolution of embedded images in PDF documents, as well as apply a deflator compression algorithm to further save on space.
Colorspace Conversion:
Enables a user to transfer a document's color scheme from RGB to CMYK, or vice versa.
As our development team continues to add more functionality to this stable release, however, this example will expand significantly!
Below this code snippet are input and output documents for each optimization handler so that viewers may see the efficacy of PdfOptimizer first-hand.
JAVA
import com.itextpdf.licensekey.LicenseKey;
import com.itextpdf.pdfoptimizer.PdfOptimizer;
import com.itextpdf.pdfoptimizer.handlers.ColorSpaceConverter;
import com.itextpdf.pdfoptimizer.handlers.CompressionOptimizer;
import com.itextpdf.pdfoptimizer.handlers.FontDuplicationOptimizer;
import com.itextpdf.pdfoptimizer.handlers.ImageQualityOptimizer;
import com.itextpdf.pdfoptimizer.handlers.converters.ColorConversionMode;
import com.itextpdf.pdfoptimizer.handlers.converters.CsConverterProperties;
import com.itextpdf.pdfoptimizer.handlers.converters.RgbToCmykCsConverter;
import com.itextpdf.pdfoptimizer.handlers.imagequality.processors.BitmapCompressor;
import com.itextpdf.pdfoptimizer.report.builder.FileReportBuilder;
import com.itextpdf.pdfoptimizer.report.message.SeverityLevel;
import com.itextpdf.pdfoptimizer.report.publisher.FileReportPublisher;
import java.io.File;
import java.io.IOException;
public class PdfOptimizer_Sample {
public static String SRC = "input_to_be_optimized.pdf";
public static String DEST = "output_optimized.pdf";
public static void main(String args[]) throws IOException {
PdfOptimizer_Sample test = new PdfOptimizer_Sample();
test.pdfOptimizerTest();
}
public void pdfOptimizerTest() throws IOException {
// First, we must load our license file in order to utilize pdfOptimizer.
LicenseKey.loadLicenseFile("iText7_licensekey.xml");
// As the namesake class of the PdfOptimizer add-on, this is where we configure our optimization.
PdfOptimizer optimizer = new PdfOptimizer();
/* Here we instantiate a FileReportBuilder which we can use as a log of the efficacy of
our optimization. Security level configuration possible. */
FileReportPublisher publisher = new FileReportPublisher(new File("report.txt"));
FileReportBuilder builder = new FileReportBuilder(SeverityLevel.INFO, publisher);
optimizer.setReportBuilder(builder);
/* From here on, we configure how PdfOptimizer will optimize our documents.
Each handler represents a different optimization strategy */
// Removes duplicate instances of font files embedded within documents.
optimizer.addOptimizationHandler(new FontDuplicationOptimizer());
// Removes all character data per font which is not actively utilized in the input document.
optimizer.addOptimizationHandler(new FontSubsettingOptimizer());
/* Scales down and compresses Image objects. In this case, we scale and compress a
Tiff image by 50% */
ImageQualityOptimizer tiffOptimizer = new ImageQualityOptimizer();
tiffOptimizer.setTiffProcessor(new BitmapCompressor(.5f, .5f));
optimizer.addOptimizationHandler(tiffOptimizer);
// Here, we add a general compression strategy on our entire document based on Deflater logic.
optimizer.addOptimizationHandler(new CompressionOptimizer());
/* Adds a handler to our optimization process to convert our input document's colorspace
from RGB, to CMYK. */
ColorSpaceConverter rgbToCmykConverter = new ColorSpaceConverter();
CsConverterProperties csConversionProperties = new CsConverterProperties(ColorConversionMode.NORMAL);
rgbToCmykConverter.setCsConverter(new RgbToCmykCsConverter(csConversionProperties));
optimizer.addOptimizationHandler(rgbToCmykConverter);
// Document is optimized according to defined handlers and written out to file.
optimizer.optimize(
new File(SRC),
new File(DEST));
}
}
C#
using iText.License;
using iText.Pdfoptimizer;
using iText.Pdfoptimizer.Handlers;
using iText.Pdfoptimizer.Handlers.Converters;
using iText.Pdfoptimizer.Handlers.Imagequality.Processors;
using iText.Pdfoptimizer.Report.Builder;
using iText.Pdfoptimizer.Report.Message;
using iText.Pdfoptimizer.Report.Publisher;
using System.IO;
namespace itext_dev.Optimizer_Demo
{
class PdfOptimizer_Sample
{
public static void Main()
{
PdfOptimizer_Sample test = new PdfOptimizer_Sample();
test.PdfOptimizerTest();
}
public void PdfOptimizerTest()
{
// First, we must load our license file in order to utilize pdfOptimizer.
LicenseKey.LoadLicenseFile("iText7_licensekey.xml");
// As the namesake class of the PdfOptimizer add-on, this is where we configure our optimization.
PdfOptimizer optimizer = new PdfOptimizer();
/* Here we instantiate a FileReportBuilder which we can use as a log of the efficacy of
our optimization. Security level configuration possible. */
FileReportPublisher publisher = new FileReportPublisher(new FileInfo("report.txt"));
FileReportBuilder builder = new FileReportBuilder(SeverityLevel.INFO, publisher);
optimizer.SetReportBuilder(builder);
/* From here on, we configure how PdfOptimizer will optimize our documents.
Each handler represents a different optimization strategy */
// Removes duplicate instances of font files embedded within documents.
optimizer.AddOptimizationHandler(new FontDuplicationOptimizer());
// Removes all character data per font which is not actively utilized in the input document.
optimizer.AddOptimizationHandler(new FontSubsettingOptimizer());
/* Scales down and compresses Image objects. In this case, we scale and compress a
Tiff image by 50% */
ImageQualityOptimizer tiff_optimizer = new ImageQualityOptimizer();
tiff_optimizer.SetTiffProcessor(new BitmapCompressor(.5f, .5f));
optimizer.AddOptimizationHandler(tiff_optimizer);
// Here, we add a general compression strategy on our entire document based on Deflater logic.
optimizer.AddOptimizationHandler(new CompressionOptimizer());
/* Adds a handler to our optimization process to convert our input document's colorspace
from RGB, to CMYK. */
ColorSpaceConverter rgbToCmykConverter = new ColorSpaceConverter();
CsConverterProperties csConversionProperties = new CsConverterProperties(ColorConversionMode.NORMAL);
rgbToCmykConverter.SetCsConverter(new RgbToCmykCsConverter(csConversionProperties));
optimizer.AddOptimizationHandler(rgbToCmykConverter);
// Document is optimized according to defined handlers and written out to file.
optimizer.Optimize(
new FileInfo(SRC, FileMode.Open),
new FileInfo(DEST, FileMode.Create, FileAccess.Write));
}
}
}
Resources:
Font Duplication Optimization:
Input (278 kb) | Output (41 kb) .
Font Subsetting Optimization:
Input (269 kb) | Output (45 kb).
Image Quality Optimization:
Input (5 845 kb) | Output (138 kb)
Color Space Conversion:
Input (67 kb) | Output (56 kb)