Q:
I use PDFNet.dll for C#. My program creates thumbnails but can’t save it into pdf. Below is piece of code I use to get this approach. Could take a look on it and say is it correct? I tried it on PDFNet lib in versions 5.8.0 and 5.9.3 without success.
public void createThumbnails(string thumbImage, string sourceFile)
{
using (PDFDoc doc = new PDFDoc(sourceFile)) {
for (PageIterator it = doc.GetPageIterator(); it.HasNext(); it.Next()) {
Page p = it.Current();
p.GetSDFObj().Erase(“Thumb”);
using (PDFDraw draw = new PDFDraw()) {
draw.SetImageSize((int)p.GetPageWidth() / 8, (int)p.GetPageHeight() / 8);
draw.Export(p, fileToSave, “PNG8”);
p.GetSDFObj().Put(“Thumb”, pdftron.PDF.Image.Create(doc.GetSDFDoc(), thumbImage).GetSDFObj());
}
}
doc.Save(SDFDoc.SaveOptions.e_remove_unused | SDFDoc.SaveOptions.e_compatibility);
}
A:
It looks like you are trying to export the page to fileToSave while using thumbImage in pdftron.PDF.Image.Create().
You can embed thumbnails to a PDF without saving to disk. To render a PDF page into an image, you can call pdftron.PDF.PDFDraw.GetBitmap(). Once you have a PDF thumbnail (System.Drawing.Bitmap), you can embed it in the document with pdftron.PDF.Image.Create() method. Finally, to associate this PDF image with a page use the following line:
page.GetSDFObj().Put(“Thumb”, image.GetSDFObj());
For example, here is a snippet for 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);
}
}