How do I embed thumbnails in PDF?

150 views
Skip to first unread message

Support

unread,
Feb 15, 2013, 4:30:59 PM2/15/13
to pdfne...@googlegroups.com
Q:
 
I would like to embed thumbnails in PDF - to optimize viewing in my mobile app etc.
How do I do this with PDFNet?
-------------
A:
 
Use the following snippet:
 
C#:
 
static ObjSet objset = null;
static Obj encoder_param = null;
static void EmbedThumbs(PDFDoc doc, int thumb_size)
{
    using (PDFDraw draw = new PDFDraw())
    {
        PageIterator itr;
        for (itr = doc.GetPageIterator(); itr.HasNext(); itr.Next())
        {
            Page page = itr.Current();
            double output_image_height = thumb_size;
            double pw = page.GetPageWidth();
            double ph = page.GetPageHeight();
            draw.SetImageSize((int)(output_image_height * pw / ph), (int)output_image_height, true);
            using (Bitmap b = draw.GetBitmap(page))
            {
                pdftron.PDF.Image new_image = pdftron.PDF.Image.Create(doc.GetSDFDoc(), b, encoder_param);
                page.GetSDFObj().Put("Thumb", new_image.GetSDFObj());
            }
        }
        objset.Dispose();
    }
}
 
static void Main(string[] args)
{
 PDFNet.Initialize();
  objset = new ObjSet();
  encoder_param = objset.CreateDict();
  encoder_param.PutNumber("Quality", 30);
 try
 {
  using (PDFDoc doc = new PDFDoc(input_path + input_filename))
  {
   doc.InitSecurityHandler();
      EmbedThumbs(doc, 600);
   doc.Save(output_path + input_filename + "_opt2.pdf", SDFDoc.SaveOptions.e_linearized);
  }
 }
 catch (PDFNetException e)
 {
  Console.WriteLine(e.Message);
 }
}
-------
In C++ other languages are similar ...
 
pdftron::PDFNet::Initialize ();

 PDF::PDFDoc doc (path);  
  doc.InitSecurityHandler ();

 PDFDraw draw; 
 PDF::ObjSet hint_set;  
 SDF::Obj encoder_param=hint_set.CreateDict();
 encoder_param.PutNumber("Quality", 50); 
  for (PageIterator itr=doc.GetPageIterator(); itr.HasNext(); itr.Next())  { 
     Page page = itr.Current(); 
     double output_image_height = 600; 
     double pw = page.GetPageWidth(); 
     double ph = page.GetPageHeight(); 
     draw.SetImageSize((int)( output_image_height * pw/ph), (int)output_image_height, true); 
     BitmapInfo b = draw.GetBitmap(page, PDFDraw::e_rgb); 
     std::vector<unsigned char> data = b.GetBuffer(); 
     const char* ptr = (char*) &data.front(); 
     Image i = Image::Create2(doc.GetSDFDoc(), ptr, b.width, b.height, b.stride, 8, ColorSpace::CreateDeviceRGB(), encoder_param); 
     page.GetSDFObj().Put("Thumb", i.GetSDFObj()); 
  } 
doc.Save (path, SDF::SDFDoc::e_linearized, 0); 
}
 

Aaron

unread,
Jan 7, 2015, 3:25:59 PM1/7/15
to pdfne...@googlegroups.com
Note that you can also call PDFDoc.GenerateThumbs(max_dimension) to embed thumbnails in all pages of the document:


Reply all
Reply to author
Forward
0 new messages