gpt4 book ai didi

objective-c - 将 PDF 文档的创建从 iOS 移植到 Mac OS X

转载 作者:太空狗 更新时间:2023-10-30 03:31:44 24 4
gpt4 key购买 nike

我正在将我的代码从 iPhone 移植到 Mac,但我不知道如何在 Mac 中执行此操作。这是我尝试转换的代码,我知道 Mac 中没有 UIGraphic。有人可以指点我一个指南或给我一个快速提示吗?谢谢。

NSString *newFilePath = @"path/to/your/newfile.pdf";
NSString *templatePath = @"path/to/your/template.pdf";

//create empty pdf file;
UIGraphicsBeginPDFContextToFile(newFilePath, CGRectMake(0, 0, 792, 612), nil);

CFURLRef url = CFURLCreateWithFileSystemPath (NULL, (CFStringRef)templatePath, kCFURLPOSIXPathStyle, 0);

//open template file
CGPDFDocumentRef templateDocument = CGPDFDocumentCreateWithURL(url);
CFRelease(url);

//get amount of pages in template
size_t count = CGPDFDocumentGetNumberOfPages(templateDocument);

//for each page in template
for (size_t pageNumber = 1; pageNumber <= count; pageNumber++) {
//get bounds of template page
CGPDFPageRef templatePage = CGPDFDocumentGetPage(templateDocument, pageNumber);
CGRect templatePageBounds = CGPDFPageGetBoxRect(templatePage, kCGPDFCropBox);

//create empty page with corresponding bounds in new document
UIGraphicsBeginPDFPageWithInfo(templatePageBounds, nil);
CGContextRef context = UIGraphicsGetCurrentContext();

//flip context due to different origins
CGContextTranslateCTM(context, 0.0, templatePageBounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);

//copy content of template page on the corresponding page in new file
CGContextDrawPDFPage(context, templatePage);

//flip context back
CGContextTranslateCTM(context, 0.0, templatePageBounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);

/* Here you can do any drawings */
[@"Test" drawAtPoint:CGPointMake(200, 300) withFont:[UIFont systemFontOfSize:20]];
}
CGPDFDocumentRelease(templateDocument);
UIGraphicsEndPDFContext();

最佳答案

使用CGPDFContextCreateWithURL 代替UIGraphicsBeginPDFContextToFile(参数非常相似)。要开始/结束页面,请使用 CGPDFContextBeginPageCGPDFContextEndPage。完成后,调用 CGPDFContextClose 而不是 UIGraphicsEndPDFContext

其余的可以保持不变——Core Graphics 存在于 iOS 和 Mac OS X 上——这也意味着如果你想在两个平台上使用相同的代码,你也可以在 iOS 上使用我上面提到的功能.

关于objective-c - 将 PDF 文档的创建从 iOS 移植到 Mac OS X,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7050124/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com