加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何在Objective-C中编辑PDF?

发布时间:2020-12-16 07:49:19 所属栏目:百科 来源:网络整理
导读:我在Objective-C(使用可可)中编写一个应用程序.我有一个PDF模板,我需要将实际值替换为PDF中的占位符,然后将结果保存到新的PDF中. 我该怎么做?我应该使用哪个库? 解决方法 我找到了解决方案!它连接了quartz2d的功能和UIGraphics的简单性. NSString *newFil
我在Objective-C(使用可可)中编写一个应用程序.我有一个PDF模板,我需要将实际值替换为PDF中的占位符,然后将结果保存到新的PDF中.

我该怎么做?我应该使用哪个库?

解决方法

我找到了解决方案!它连接了quartz2d的功能和UIGraphics的简单性.
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 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,-1.0);

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读