How to Render a PDF Document to UIImage on iOS

We will show you two different ways to create thumbnail images for PDF documents and how to export some pages of a PDF document to an image on iOS.

Firstly, we will create an UIImage object from a page of a PDF document using PlugPDF. The following is the code snippet for this.

Very easy!

Next, you can use functions that are provided by the iOS SDK to create an Ullmage object. This is a bit complex. The following is the source snippet to create an UIImage object from the PAGE_NO page of a PDF document at PATH.

In order to use iOS SDK, you should understand that the coordination system for a PDF is different than that of iOS. On iOS, the origin (0, 0) is at top-left. On PDF the origin is at bottom-left. This difference has to be applied to the current transformation matrix(CTM) in the graphics context to get the correct result.

You should also count the rotation property in the page. This is the angle for page rotation. To render an image from a PDF page correctly, you also have to apply this angle to the CTM. This requires you to understand some math like affine transformation.

With PlugPDF SDK, in a few lines of code (actually it can be done in one single line!), you can create an UIImage object from a PDF page with a given size. You don’t need to understand and deal with different PDF coordinate system and transformation matrix.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *