开发者

Creating a PDF programmatically using CGContext - cannot get line breaks + iPhone

I am creating a PDF programmatically in my iPhone app using CGContext and CGContextShowTextAtPoint. While this works fine for smaller text, whenever I have line breaks in my text (\n) or want the text to wrap automatically to the next line when it reaches the end of the page, this does not happen.

Any line breaks in the text are simply replaced by spaces and the text does not wrap. Would really appreciate a heads-up on how this can be achieved. Here's the code I use to create the PDF.

void CreatePDFFile (CGRect pageRect, const char 开发者_开发问答*filename, NSString *fromFile) {
    CGContextRef pdfContext;
    CFStringRef path = CFStringCreateWithCString (NULL, filename, kCFStringEncodingUTF8);
    CGURLRef url = CFURLCreateWithFileSystemPath (NULL, path, kCFURLPOSIXPathStyle, 0);
    CFRelease (path);
    CFMutableDictionaryRef myDictionary = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
    CFDictionarySetValue(myDictionary, kCGPDFContextTitle, CFSTR("My PDF File"));
    CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("My Name"));
    pdfContext = CGPDFContextCreateWithURL (url, &pageRect, myDictionary);
    CFRelease(myDictionary);
    CFRelease(url);

    CGContextBeginPage (pdfContext, &pageRect);
    CGContextSelectFont (pdfContext, "Helvetica", 14, kCGEncodingMacRoman);
    CGContextSetTextDrawingMode (pdfContext, kCGTextFill);
    CGContextSetRGBFillColor (pdfContext, 0, 0, 150, 1);
    /* This works
    const char *text = "Sed ut perspiciatis unde omnis iste natus error sit voluptatem";
    CGContextShowTextAtPoint (pdfContext, 50, 760, text, strlen(text));*/
    //This doesn't print line breaks in the PDF
    const char *text = "Sed ut perspiciatis unde omnis iste natus error sit voluptatem\n accusantium doloremque laudantium, totam rem aperiam, eaque ipsa";
    CGContextShowTextAtPoint (pdfContext, 50, 760, text, strlen(text));
    //This doesn't wrap text to the next line
    const char *text = "Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa";
    CGContextShowTextAtPoint (pdfContext, 50, 760, text, strlen(text));

    CGContextEndPage (pdfContext);
    CGContextRelease (pdfContext);
}


CGContextShowTextAtPoint only does basic text drawing. You should be able to use the UIKit additions to NSString for things like wrapping. Eg:

NSString* text = @"Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa";
[text drawInRect:CGRectMake(50,760,200,200) withFont:[UIFont fontWithName:@"Helvetica" size:14]];

Note, though, that the NSString methods act on the current context, so you will need to make your context current in order to use them. I guess might be a problem if you're doing this in a background thread or something.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜