
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.所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)