使用 CGContext 以编程方式创建 PDF - 无法换行 + iPhone

发布于 2024-09-11 23:38:11 字数 2044 浏览 3 评论 0原文

我正在使用 CGContext 和 CGContextShowTextAtPoint 在我的 iPhone 应用程序中以编程方式创建 PDF。虽然这对于较小的文本效果很好,但每当我的文本中有换行符 (\n) 或希望文本在到达页面末尾时自动换行到下一行时,这种情况就不会发生。

文本中的任何换行符都会简单地替换为空格,并且文本不会换行。如果能了解如何实现这一目标,我真的很感激。这是我用来创建 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);
}

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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一刻暧昧 2024-09-18 23:38:11

CGContextShowTextAtPoint 仅进行基本的文本绘制。您应该能够使用 UIKit 添加NSString 用于诸如包装之类的事情。例如:

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]];

但请注意,NSString 方法作用于当前上下文,因此您需要将上下文设为当前上下文才能使用它们。我想如果您在后台线程或其他线程中执行此操作可能会出现问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文