iPDFdev Tips & Tricks for PDF development

IFXPDFFactory – part 2 – page size and page boxes

August 13th, 2012

In the previous post I showed how PDF pages are created with iFXPDFFactory library. The default page size is Letter but it can be easily changed using the width and height properties.

IFXPDFDocument* doc = [[IFXPDFDocument alloc] init];
IFXPDFPage* page = [IFXPDFPage emptyPage];
[doc.pages addPage: page];
// Set page size to A4
page.width = 595;
page.height = 842;
 
[doc writeToFile: pathToPdfFile];
[doc release];

The page width and height are given in points, 1 point = 1/72 inches. The width is always measured on horizontal and the height is always measured on horizontal regardless of page rotation.

Developers with good knowledge of PDF specification can set directly the page boxes: media box, crop box, trim box, art box and bleed box. The media box defines the physical page size, the crop box defines visible page area inside the media box. If crop box is missing it is considered to match the media box. The other boxes have an informative role.

All boxes are defined as PDF rectangles, in terms of lower left and upper right corners. The IFXPDFStandardRectangle class encapsulates the standard PDF rectangle functionality. llx and lly properties define the lower left corner while the urx and ury properties define the upper right corner.

The code above can be written like this:

IFXPDFDocument* doc = [[IFXPDFDocument alloc] init];
IFXPDFPage* page = [IFXPDFPage emptyPage];
[doc.pages addPage: page];
// Set page size to A4
IFXPDFStandardRectangle* mediaBox = [IFXPDFStandardRectangle alloc] initWithLlx: 0 
                                                                            lly: 0 
                                                                            urx: 595 
                                                                            ury: 842];
page.mediaBox = mediaBox;
[mediaBox release];
 
// or simpler
page.mediaBox = [IFXPDFStandardRectangle standardRectangleWithLlx: 0 lly: 0 urx: 595 ury: 842];
page.cropBox = [IFXPDFStandardRectangle standardRectangleWithLlx: 0 lly: 0 urx: 595 ury: 842];
 
[doc writeToFile: pathToPdfFile];
[doc release];

Another property that affects the page visual aspect is the rotation. The rotation property rotates clockwise the entire page coordinate system around the lower left corner. This post shows the PDF page coordinate system for each possible rotation.

As I said earlier the width property is always measured on horizontal. This means that if the rotation is 0 or 180 then the width matches the crop box width (cropbox.urx - cropbox.llx), or media box width is crop box is not set, and if the rotation is 90 or 270 then the width matches the crop box height (cropbox.ury - cropbox.lly). The same rule applies for page height.

IFXPDFPage* page = [IFXPDFPage emptyPage];
// Set page size to A4 portrait
page.width = 595;
page.height = 842;
// Rotate the page with 90 degrees, A4 landscape
page.rotation = 90;
// page.width is now 842 and page.height is now 595
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

No trackbacks yet.