IFXPDFFactory – part 8 – PDF fonts and text
July 30th, 2013
Support for the base 14 PDF fonts has been implemented. Also the text drawing methods have been implemented.
The base class for all fonts is IFXPDFBaseFont
. The standard 14 PDF fonts are implemented through the IFXPDFFont
class, which inherits from IFXPDFBaseFont
. The supported fonts are: Helvetica, Helvetica Bold, Helvetica Italic, Helvetica Bold-Italic, Courier, Courier Bold, Courier Italic, Courier Bold-Italic, Times Roman, Times Roman Bold, Times Roman Italic, Times Roman Bold-Italic, ZapfDingbats and Symbol.
The simplest way to create a font takes one line:
IFXPDFFont* helvetica = [IFXPDFFont pdfFont]; |
This line of code will create a PDF font with Helvetica font face and a size of 10 points. A more elaborate font setup looks like this:
IFXPDFFont* courierBold = [[IFXPDFFont alloc] init]; courierBold.fontFace = IFXPDFCourierBoldFontFace; courierBold.size = 24; courierBold.isUnderline = TRUE; courierBold.isStrikethrough = FALSE; |
The font objects are used with the text drawing methods for displaying text on PDF pages. The text drawing methods, like the other drawing methods, are part of the IFXPDFGraphics
class. There are 2 groups of text drawing methods: one for drawing single lines of text and one for drawing text boxes with automatic text wrapping.
Text line methods
- (void) drawText:withFont:brush:atX:y:
- draws a line of text with the specified font. The text interior is filled with the brush. The text top left corner is located at (x,y).
- (void) drawText:withFont:brush:atX:y:rotation:
- draws a line of text with the specified font. The text interior is filled with the brush. The text top left corner is located at (x,y). The text is rotated CCW with rotation degrees.
- (void) drawText:withFont:brush:atX:y:rotation:horizontalAlign:verticalAlign:
- draws a line of text with the specified font. The text interior is filled with the brush. The text is rotated CCW with rotation degrees. The (x,y) point serves as reference for text alignment.
- (void) drawText:withFont:pen:brush:atX:y:rotation:horizontalAlign:verticalAlign:
- draws a line of text with the specified font. The text interior is filled with the brush and the text outline is stroked with the specified pen. The text is rotated CCW with rotation degrees. The (x,y) point serves as reference for text alignment.
A simple Hello world looks like this:
IFXPDFDocument* document = [[IFXPDFDocument alloc] init]; IFXPDFPage* page = [IFXPDFPage emptyPage]; [document.pages addPage: page]; IFXPDFPen* pen = [IFXPDFPen penWithColor: [IFXPDFRgbColor blueColor] andWidth: 1]; IFXPDFBrush* brush = [IFXPDFBrush brushWithColor: [IFXPDFRgbColor darkRedColor]]; IFXPDFFont* font = [IFXPDFFont pdfFont]; font.fontFace = IFXPDFHelveticaBoldFontFace; font.size = 96; // Draw text in the middle of the page rotated with 45 degrees. [page.graphics drawText:@"Hello IFXPDFFactory" withFont:font pen:pen brush:brush atX:page.width / 2 y:page.height / 2 rotation:45 horizontalAlign:IFXPDFTextCenterHorizontalAlign verticalAlign:IFXPDFTextMiddleVerticalAlign]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *pdfFile = [documentsDirectory stringByAppendingPathComponent:@"textline.pdf"]; [document writeToFile: pdfFile]; [document release]; |
Text box methods
- (void) drawTextBox:withFont:brush:atX:y:width:height:
- draws multiple lines of text with the specified font. The text interior is filled with the brush. The top left corner of the box is located at (x,y), the box size is (width, height). The text is automatically wrapped at box width. The lines of text that fall outside the text box are not displayed. The text is aligned inside the box to the left on horizontal and at top on vertical.
- (void) drawTextBox:withFont:brush:atX:y:width:height:rotation:
- draws multiple lines of text with the specified font. The text interior is filled with the brush. The top left corner of the box is located at (x,y), the box size is (width, height). The text is automatically wrapped at box width. The lines of text that fall outside the text box are not displayed. The text is aligned inside the box to the left on horizontal and at top on vertical. The entire box is rotated CCW around top left corner with rotation degrees.
- (void) drawTextBox:withFont:brush:atX:y:width:height:rotation:horizontalAlign:verticalAlign:
- draws multiple lines of text with the specified font. The text interior is filled with the brush. The top left corner of the box is located at (x,y), the box size is (width, height). The text is automatically wrapped at box width. The lines of text that fall outside the text box are not displayed. The text is aligned inside the box on horizontal and on vertical according to horizontalAlign and verticalAlign. The entire box is rotated CCW around top left corner with rotation degrees.
- (void) drawTextBox:withFont:pen:brush:atX:y:width:height:rotation:horizontalAlign:verticalAlign:
- draws multiple lines of text with the specified font. The text interior is filled with the brush and the text outline is stroked with pen. The top left corner of the box is located at (x,y), the box size is (width, height). The text is automatically wrapped at box width. The lines of text that fall outside the text box are not displayed. The text is aligned inside the box on horizontal and on vertical according to horizontalAlign and verticalAlign. The entire box is rotated CCW around top left corner with rotation degrees.
A simple text box is drawn like this:
IFXPDFDocument* document = [[IFXPDFDocument alloc] init]; IFXPDFPage* page = [IFXPDFPage emptyPage]; [document.pages addPage: page]; IFXPDFPen* pen = [IFXPDFPen penWithColor: [IFXPDFRgbColor blueColor] andWidth: 1]; IFXPDFBrush* brush = [IFXPDFBrush brushWithColor: [IFXPDFRgbColor darkRedColor]]; IFXPDFFont* font = [IFXPDFFont pdfFont]; NSString* text = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc laoreet ligula quis nulla dictum, adipiscing molestie velit interdum. Aliquam odio orci, ultrices nec posuere sed, lobortis eget lorem."; [page.graphics drawRectangleWithPen:pen atX:10 y:10 withWidth:180 height:180]; [page.graphics drawTextBox:text withFont:font brush:brush atX:10 y:10 width:180 height:180 rotation:0 horizontalAlign:IFXPDFTextCenterHorizontalAlign verticalAlign:IFXPDFTextMiddleVerticalAlign]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *pdfFile = [documentsDirectory stringByAppendingPathComponent:@"textbox.pdf"]; [document writeToFile: pdfFile]; [document release]; |
Supported alignments are left, center and right on horizontal and top, middle and bottom on vertical. Textboxes also support justified alignment on horizontal.
Leave a comment