IFXPDFFactory – part 7 – PDF images
June 28th, 2013
Support for drawing images on PDF pages has been implemented. An image drawn on a PDF page is represented by the IFXPDFImage
class. At this moment a PDF image can be created only from a CGImage object, later I'll add support for creating images directly from image files or memory.
An IFXPDFImage
object is created either using the designated initializer initWithCGImage:
or using the class method pdfImageWithCGImage:
.
The image is drawn on the page using one of the drawImage methods:
drawImage:atX:y:withWidth:height:
- draws the image on the page with top left corner at (x,y) and the size specified by width and height parameters.
drawImage:atX:y:withWidth:height:rotation:
- draws the image on the page with top left corner at (x,y) and the size specified by width and height parameters. Image is rotated around top left corner with rotation degrees.
drawImage:atX:y:withWidth:height:rotation:flip:
- draws the image on the page with top left corner at (x,y) and the size specified by width and height parameters. Image is rotated around top left corner with rotation degrees. The image is displayed flipped around X and/or Y axis.
The width and height parameters above are given in points and they represent the size of the image as it appears drawn on the page, they do not represent the image size in pixels. The image is stored in the PDF file as it is, its content is not scaled in any way.
The code fragment below draws an image that covers the entire page area:
IFXPDFDocument* document = [[IFXPDFDocument alloc] init]; IFXPDFPage* page = [IFXPDFPage emptyPage]; [document.pages addPage:page]; UIImage* img = [UIImage imageNamed:@"sample.jpg"]; IFXPDFImage* pdfImage = [IFXPDFImage pdfImageWithCGImage:img.CGImage]; [page.graphics drawImage:pdfImage atX:0 y:0 withWidth:page.width height:page.height]; [document writeToFile:@"image.pdf"]; [document release]; |
A IFXPDFImage
object can be drawn multiple times on the same page or on different pages. The actual image data is included in the PDF file only once no matter how many times the image is drawn on document's pages.
Leave a comment