Some test text!

Optimize a PDF

Contents

About compression
Compress images inside a PDF

To optimize a PDF by reducing image quality and using jpeg compression for all non monochrome images.

PDFDoc doc = new PDFDoc(input_path + input_filename + ".pdf");

Optimizer.ImageSettings image_settings = new Optimizer.ImageSettings();

// low quality jpeg compression
image_settings.SetCompressionMode(Optimizer.ImageSettings.CompressionMode.e_jpeg);
image_settings.SetQuality(1);

// Set the output dpi to be standard screen resolution
image_settings.SetImageDPI(144,96);

// this option will recompress images not compressed with
// jpeg compression and use the result if the new image
// is smaller.
image_settings.ForceRecompression(true);

// this option is not commonly used since it can
// potentially lead to larger files.  It should be enabled
// only if the output compression specified should be applied
// to every image of a given type regardless of the output image size
//image_settings.ForceChanges(true);

// use the same settings for both color and grayscale images
Optimizer.OptimizerSettings opt_settings = new Optimizer.OptimizerSettings();
opt_settings.SetColorImageSettings(image_settings);
opt_settings.SetGrayscaleImageSettings(image_settings);

Optimizer.Optimize(doc, opt_settings);

Compress & optimize PDF files
Full code sample which shows how to use 'pdftron.PDF.Optimizer' to reduce PDF file size by removing redundant information and compressing data streams using the latest in image compression technology.

linkAbout compression

Compression as a subset of optimization represents encoding specific data using fewer bits than the original content by reducing the size of the data. This is distinct from optimize because you have the ability to choose individual images and to selectively choose the compression type for each data.

PDFTron SDK supports all basic and advanced compression filters allowed in PDF including:

  • JPEG2000
  • JBIG2
  • CCITT Fax
  • Flate/PNG
  • JPEG/DCT
  • Crypt Filters

linkCompress images inside a PDF

To compress images using JBIG2 compression inside a PDF.

PDFDoc pdf_doc = new PDFDoc(input_path + input_filename);
SDFDoc cos_doc = pdf_doc.GetSDFDoc();
int num_objs = cos_doc.XRefSize();

for (int i=1; i<num_objs; ++i)
{
    Obj obj = cos_doc.GetObj(i);
    if (obj!=null && !obj.IsFree() && obj.IsStream())
    {
        // Process only images
        DictIterator itr = obj.Find("Subtype");
        if (!itr.HasNext() || itr.Value().GetName() != "Image")
            continue;

        pdftron.PDF.Image input_image = new pdftron.PDF.Image(obj);
        pdftron.PDF.Image new_image = null;

        // Process only gray-scale images
        if (input_image.GetComponentNum() != 1)
            continue;

        int bpc = input_image.GetBitsPerComponent();
        if (bpc != 1) // Recompress 1 BPC images
            continue;

        // Skip images that are already compressed using JBIG2
        itr = obj.Find("Filter");
        if (itr.HasNext() && itr.Value().IsName() && itr.Value().GetName() == "JBIG2Decode")
            continue;

        FilterReader reader = new FilterReader(obj.GetDecodedStream());

        ObjSet hint_set = new ObjSet();
        Obj hint = hint_set.CreateArray();
        hint.PushBackName("JBIG2");
        hint.PushBackName("Lossless");
        hint.PushBackName("Threshold");
        hint.PushBackNumber(0.4);
        hint.PushBackName("SharePages");
        hint.PushBackNumber(10000);

        new_image = pdftron.PDF.Image.Create(
            cos_doc,
            reader,                             
            input_image.GetImageWidth(),
            input_image.GetImageHeight(),
            1,
            ColorSpace.CreateDeviceGray(),
            hint  // A hint to image encoder to use JBIG2 compression
        );

        Obj new_img_obj = new_image.GetSDFObj();

        // Copy any important entries from the image dictionary
        itr = obj.Find("ImageMask");
        if (itr.HasNext()) new_img_obj.Put("ImageMask", itr.Value());

        itr = obj.Find("Mask");
        if (itr.HasNext()) new_img_obj.Put("Mask", itr.Value());

        cos_doc.Swap(i, new_image.GetSDFObj().GetObjNum());
    }
}
pdf_doc.Save(output_path + "US061222892_JBIG2.pdf", SDFDoc.SaveOptions.e_remove_unused);
pdf_doc.Close();

PDF Image JBIG2 Compression
Full sample code which illustrates how to recompress bitonal (black and white) images in existing PDF documents using JBIG2 compression.

Get the answers you need: Support

Contents

About compression
Compress images inside a PDF