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

objective-c – 将PDF文档的创建从iOS移植到Mac OS X.,第1张

概述我将我的代码从iPhone移植到Mac,我不知道如何在Mac中执行此 *** 作.这是我试图转换的代码,我知道Mac中没有UIGraphic.有人能指点我指导或给我一个快速提示吗?谢谢. NSString *newFilePath = @"path/to/your/newfile.pdf";NSString *templatePath = @"path/to/your/template.pdf";/ 我将我的代码从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,792,612),nil);CFURLRef url = CFURLCreateWithfileSystemPath (NulL,(CFStringRef)templatePath,kcfURLPOSIXPathStyle,0);//open template fileCGpdfdocumentRef templatedocument = CGpdfdocumentCreateWithURL(url);CFRelease(url);//get amount of pages in templatesize_t count = CGpdfdocumentGetNumberOfPages(templatedocument);//for each page in templatefor (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,-1.0);    /* Here you can do any drawings */    [@"Test" drawAtPoint:CGPointMake(200,300) withFont:[UIFont systemFontOfSize:20]];}CGpdfdocumentRelease(templatedocument);UIGraphicsEndPdfcontext();
解决方法 使用CGPdfcontextCreateWithURL而不是UIGraphicsBeginPdfcontextTofile(参数非常相似).要开始/结束页面,请使用CGPdfcontextBeginPage和CGPdfcontextEndPage.完成后,调用CGPdfcontextClose而不是UIGraphicsEndPdfcontext.

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

总结

以上是内存溢出为你收集整理的objective-c – 将PDF文档创建从iOS移植到Mac OS X.全部内容,希望文章能够帮你解决objective-c – 将PDF文档的创建从iOS移植到Mac OS X.所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1020755.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-23
下一篇2022-05-23

发表评论

登录后才能评论

评论列表(0条)

    保存