[PDFNet] How do I optimize a PDF with uncompressed images?

Q:
I am running SQL reporting services which is generating huge PDF files
(e.g. 50 pages is ~ 120MB mostly because images are not compressed
properly). I would like to use PDFNet to optimize these files. Using
Acrobat Pro I was able to shrink file down to ~10MB. Can you point me
in the right direction.
-----------------------
A:

We were able to optimize your 112MB file down to 5MB.

1) To test this download the PDFNet SDK (http://www.pdftron.com/
downloads/PDFNet.zip).
2) Extract the attached sample under PDFNet/Samples
3) Update the input/output path and run the solution

You can download the full sample code from Files section in this
forum:
  http://groups.google.com/group/pdfnet-sdk/web/ImageRecompress.zip

using System;
using pdftron;
using pdftron.Common;
using pdftron.Filters;
using pdftron.SDF;
using pdftron.PDF;

namespace PDFTronCS
{
  /// <summary>
  //-----------------------------------------------------------------------------------------
  // The sample code shows a naive approach to image re-compression in
PDF.
  // The method works fairly well on files with uncompressed images,
but it may
  // not produce great results if the images are already compressed.
  //-----------------------------------------------------------------------------------------
  /// </summary>
  class ImageRecompressTest
  {
    static private void Compress(PDFDoc doc)
    {
      SDFDoc cos_doc = 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;

          FilterReader reader = new FilterReader(obj.GetDecodedStream());
          new_image = pdftron.PDF.Image.Create(
            cos_doc,
            reader,
            input_image.GetImageWidth(),
            input_image.GetImageHeight(),
            input_image.GetBitsPerComponent(),
            input_image.GetImageColorSpace());

          Obj new_img_obj = new_image.GetSDFObj();

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

          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());
        }
      }
    }

    static void Main(string[] args)
    {
      try
      {
        PDFNet.Initialize();
        using (PDFDoc doc = new PDFDoc(@"d:\in.pdf"))
        {
          doc.InitSecurityHandler();
          Compress(doc);
          doc.Save(@"d:\out.pdf", SDFDoc.SaveOptions.e_linearized);
        }
      }
      catch (PDFNetException e)
      {
        System.Console.WriteLine(e.Message);
      }
    }
  }
}

--
You received this message because you are subscribed to the "PDFTron PDFNet SDK" group. To post to this group, send email to support@pdftron.com
To unsubscribe from this group, send email to pdfnet-sdk-unsubscribe@googlegroups.com. For more information, please visit us at http://www.pdftron.com