Thumbnail from PDF Page [iOS ... ]

708 views
Skip to first unread message

Support

unread,
Feb 14, 2012, 6:11:42 PM2/14/12
to pdfne...@googlegroups.com
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.

 

Support

unread,
Apr 22, 2013, 2:07:22 PM4/22/13
to pdfne...@googlegroups.com
 
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 = w*h*3; 
Filters::FilterReader reader(thumb.GetImageData()); 
std::vector<UChar> 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.

 


On Monday, April 22, 2013 5:46:49 AM UTC-7, Nimrod Astrahan wrote:
Would love to see some example code on this... 

Thanks a lot in advance!

Nimrod Astrahan

unread,
Apr 22, 2013, 2:58:42 PM4/22/13
to PDFTron PDFNet SDK on behalf of Support
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

Nimrod Astrahan

unread,
Apr 22, 2013, 6:11:33 PM4/22/13
to PDFTron PDFNet SDK on behalf of Support
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

On Monday, April 22, 2013, Support via PDFTron PDFNet SDK wrote:

Support

unread,
Aug 21, 2013, 2:19:46 PM8/21/13
to pdfne...@googlegroups.com
 
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

 
 
 
 

Support

unread,
Aug 21, 2013, 2:40:53 PM8/21/13
to pdfne...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages