私はobjective-c(ココアを使用)でアプリケーションを書いています。私はPDFテンプレートを持っているので、実際の値をPDFのプレースホルダに置き換え、その結果を新しいPDFに保存する必要があります。objective-cでPDFを編集するには?
どうすればいいですか?どのライブラリを使うべきですか?
私はobjective-c(ココアを使用)でアプリケーションを書いています。私はPDFテンプレートを持っているので、実際の値をPDFのプレースホルダに置き換え、その結果を新しいPDFに保存する必要があります。objective-cでPDFを編集するには?
どうすればいいですか?どのライブラリを使うべきですか?
解決策を見つけました。これは、quartz2dのパワーとUIGraphicsのシンプルさを接続します。
PS:私は、私は質問がobj-Cについてですが、あなたが原因編集PDFでここにいる場合は、以下のスウィフト3内の溶液があることを知っている
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();
おそらくPDFKit。いくつかのタスクでは、高レベルのPDFKit APIはあなたが望むことをすることができず、低レベルのCG PDF parsing librariesを使わなければならないかもしれません。彼らはかなり低いレベルです。彼らは実際にPDFファイル形式を理解することを意味します。
ソリューション私は新しいPDFのドキュメント情報を編集する必要があったので、これを行うにはというサブジェクトパラメータを使用しました。
func createPDF(on path: String?, from templateURL: URL?, with subject: String?) {
guard let newPDFPath = path,
let pdfURL = templateURL else { return }
let options = [(kCGPDFContextSubject as String): subject ?? ""] as CFDictionary
UIGraphicsBeginPDFContextToFile(newPDFPath, .zero, options as? [AnyHashable : Any])
let templateDocument = CGPDFDocument(pdfURL as CFURL)
let pageCount = templateDocument?.numberOfPages ?? 0
for i in 1...pageCount {
//get bounds of template page
if let templatePage = templateDocument?.page(at: i) {
let templatePageBounds = templatePage.getBoxRect(.cropBox)
//create empty page with corresponding bounds in new document
UIGraphicsBeginPDFPageWithInfo(templatePageBounds, nil)
let context = UIGraphicsGetCurrentContext()
//flip context due to different origins
context?.translateBy(x: 0.0, y: templatePageBounds.height)
context?.scaleBy(x: 1.0, y: -1.0)
//copy content of template page on the corresponding page in new file
context?.drawPDFPage(templatePage)
//flip context back
context?.translateBy(x: 0.0, y: templatePageBounds.height)
context?.scaleBy(x: 1.0, y: -1.0)
}
}
UIGraphicsEndPDFContext()
}
私はあなたが大好きです! – Seb