Thumbnail from PDF Page [iOS ... ]

Q:

I just tried getting a thumbnail from a page in a PDFDoc, but it just returns nil. What gives?

  • (UIImage*)thumbnailOfPage:(NSUInteger)pageNumber {

PDFDoc *activePDF = [self GetDoc];

Page* page = [activePDF GetPage:pageNumber];

NSObject *thumbnail = [page GetThumb];

return (UIImage*)thumbnail;

}

Also, how do I get from the return Image object class back to a UIImage that I can use on iOS, the casting I do above is probably incorrect

A:

You are getting Nil because the PDF does not appear to contain a thumbnail for the page. A PDF document may contain a thumbnail for a page, which if it exists, is what GetThumb returns. The call does not create a thumbnail. If you wish to generate a thumbnail, your best bet would be to use an instance of PDFDraw to rasterize the page at a small resolution.

Finally, yes the cast is incorrect, GetThumb returns an Obj*, not an NSObject*. You could use the Obj* that it returns to create a new Image (a PDFNet type), read out the pixel data from the Image and use the raw pixel data to create a new UIImage. Let me know if you would like some example code on how to do this.

Here is some C++ / C# pseudo code to give you some ideas…

Obj th_obj = page.GetThumb()
if (th_obj != null) {
PDF::Image thumb(th_obj);
thumb.ExportAsPng(“my.png”);
}

you can also extract data without writing to disk using thumb.GetImageData() for example:

int w = thumb.GetImageWidth();
int h = thumb.GetImageHeight();
int out_data_sz = wh3;
Filters::FilterReader reader(thumb.GetImageData());
std::vector image_data_out(out_data_sz);
reader.Read(&image_data_out[0], out_data_sz);
void* buffer = &image_data_out[0];

assuming the thumbnail image is stored in 8-bpc RGB format.

Please keep in mind that most PDF files do not store thumbnail and that in this case ‘page.GetThumb()’ will return NULL obj.
You could pre-process your PDFs for fast vieweing on mobile devices (by embedding thumbnails, downsampling images, etc) as shown in the following article - https://groups.google.com/d/msg/pdfnet-sdk/PV_M_FvUfQ0/RJKZc9LzU1cJ

In case you can’t count on embedded thumbnails you would need to render them using PDFDraw (as a starting point please take a look at PDFDraw sample - http://www.pdftron.com/pdfnet/samplecode.html#PDFDraw).

The upcoming version of PDFNet will also include a new method in PDFViewCtrl → GetThumbAsync() that could pick up embedded thumb (if available) or generate a dynamic thumb … however this major version will not be available for another month or so.

Sorry for not being specific enough but i was referring to he last sentence in the previous answer. I am able to retrieve thumbnail data in a matter similar to the one you described. My difficulties are in transforming that data into a UIImage instance.

Thank you so much for your in depth help and sorry for not clearing this out in the first place.

Nimrod

Sorry for not being specific enough but i was referring to he last sentence in the previous answer. I am able to retrieve thumbnail data in a matter similar to the one you described. My difficulties are in transforming that data into a UIImage instance.

Thank you so much for your in depth help and sorry for not clearing this out in the first place.

Nimrod

The code to fetch embedded thumb (in VB, .NET) is:

Function GetThumb(CurPag As pdftron.PDF.Page) As pdftron.PDF.Image

Dim Itr As DictIterator = CurPag.GetSDFObj.Find(“Thumb”)

If Itr.HasNext Then Return New pdftron.PDF.Image(Itr.Value)

Return Nothing

End Function