iPDFdev Tips & Tricks for PDF development

IFXPDFFactory – part 10 – TrueType fonts in PDF files

February 24th, 2015

The IFXPDFTrueTypeFont class brings support for using TrueType fonts in PDF files. TrueType font objects can be created both in ANSI and Unicode mode.

IFXPDFTrueTypeFont object can be created with from a path or from memory. The available initializers are:

- (id) initWithFontPath:(NSString*)path andSize:(float)size;
- (id) initWithFontData:(NSData*)data andSize:(float)size;
- (id) initWithFontPath:(NSString*)path size:(float)size andUnicode:(BOOL)unicode;
- (id) initWithFontData:(NSData*)data size:(float)size andUnicode:(BOOL)unicode;

By default the IFXPDFTrueTypeFont objects are created in ANSI mode. If you need to display text that contains Unicode characters (code greater than 256) then use the last 2 initializers and set the unicode parameter to YES.

An IFXPDFTrueTypeFont object can be used with any function that expects an IFXPDFBaseFont parameter (IFXPDFTrueTypeFont inherits from IFXPDFBaseFont).

IFXPDFDocument* doc = [[IFXPDFDocument alloc] init];
IFXPDFPage* page = [IFXPDFPage emptyPage];
[doc.pages addPage:page];
IFXPDFBrush* blackBrush = [IFXPDFBrush brushWithColor:[IFXPDFRgbColor blackColor]];
NSString *ttfPath = [[NSBundle mainBundle] pathForResource:@"verdana" ofType:@"ttf"];
IFXPDFTrueTypeFont* ansiTtf = [[IFXPDFTrueTypeFont alloc] initWithFontPath:ttfPath andSize:16];
[page.graphics drawText:@"Ansi Verdana - 16 points" withFont:ansiTtf brush:blackBrush atX:20 y:50];
IFXPDFTrueTypeFont* unicodeTtf = [[IFXPDFTrueTypeFont alloc] initWithFontPath:ttfPath size:16 andUnicode:YES];
[page.graphics drawText:@"Unicode Verdana \u0398 \u03A3 \u03A6 \u03A8 \u03A9 - 16 points"
               withFont:unicodeTtf brush:blackBrush atX:20 y:80];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfFile = [documentsDirectory stringByAppendingPathComponent:@"TrueTypeFonts.pdf"];
[doc writeToFile:pdfFile];
Comments (0) Trackbacks (0)

No comments yet.

Leave a comment

No trackbacks yet.