将创建PDF文档从iOS移植到Mac OS X

编程入门 行业动态 更新时间:2024-10-26 11:30:22
本文介绍了将创建PDF文档从iOS移植到Mac OS X的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在将我的代码从iPhone移植到Mac,我不知道如何在Mac中做到这一点.这是我要转换的代码,我知道Mac中没有UIGraphic.有人可以给我指出指南或给我快速提示吗?谢谢.

I am porting my code from iPhone to Mac and I have no idea how I can do this in Mac. Here's my code that I am trying to convert and I know that there's no UIGraphic in Mac. Can someone point me to a guide or give me a quick hint? Thanks.

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(参数非常相似).要开始/结束页面,请使用CGPDFContextBeginPage和CGPDFContextEndPage.完成后,呼叫CGPDFContextClose而不是UIGraphicsEndPDFContext.

Use CGPDFContextCreateWithURL instead of UIGraphicsBeginPDFContextToFile (the parameters are very similar). To begin/end pages, use CGPDFContextBeginPage and CGPDFContextEndPage. When you're done, call CGPDFContextClose instead of UIGraphicsEndPDFContext.

其余部分可以保持不变-iOS和Mac OS X上都存在核心图形-这也意味着如果要在两个平台上使用相同的代码,也可以使用上面在iOS上提到的功能

The rest can remain the same – Core Graphics exists on both iOS and Mac OS X – which also means that you could use the functions I've mentioned above on iOS as well if you want to use the same code on both platforms.

更多推荐

将创建PDF文档从iOS移植到Mac OS X

本文发布于:2023-10-07 15:42:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1469805.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文档   PDF   iOS   OS   Mac

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!