在iPhone应用程序中编辑PDF

现在我正在创build一个像“ iAnnotate PDF ”一样的应用程序,现在我已经完成了在UIView中读取和显示PDF页面的工作。 即使我获得TOC成功。

现在我想编辑PDF,编辑包括标记,高光和写作笔记。

现在我的问题是如何编辑? 我的意思是我们需要创build另一个PDF格式,并写入新的变化?

或者有什么办法可以在现有的pdf中更新单个页面? 如果是的话,那是怎么做的?

如果我们正在重写整个pdf,那是否会造成开销?

也许这个想法可以在iPhone中工作我认为没有直接的方式,我们可以编辑一个PDF,但我们可以做什么,我们可以产生这样的function,iAnnotate应用程序,即绘制线和文本,然后只保存PDF作为一页一页的图片在其中你将包括编辑过的页面,然后你将再次通过图像创buildPDF,那么我想你会得到编辑的PDF。

您可以使用CoreGraphics轻松创build/编辑pdf。 只需创build一个定义大小的矩形,然后创build上下文,按下上下文,调用CFPDFContextBeginPage并像通常那样开始绘制…以下是一个示例:

CGRect mediaBox = CGRectMake(0, 0, 550, 800); NSString *url = [path stringByExpandingTildeInPath]; CGContextRef ctx = CGPDFContextCreateWithURL((CFURLRef)[NSURL fileURLWithPath:url], &mediaBox, NULL); UIGraphicsPushContext(ctx); //Page1 CGPDFContextBeginPage(ctx, NULL); CGContextScaleCTM(ctx, 1, -1); CGContextTranslateCTM(ctx, 0, -mediaBox.size.height); //name and value are NSStrings [name drawInRect:rectName withFont:fontName]; [value drawInRect:rectValue withFont:fontValue]; [[UIImage imageNamed:@"logo.png"] drawInRect:CGRectMake(8, 15, 91, 99)]; CGPDFContextEndPage(ctx); UIGraphicsPopContext(); CFRelease(ctx); 

更新你可以复制你需要的页面,然后像这样对它们进行绘制:

 CGPDFDocumentRef originalDoc = NULL; CGContextRef pdfContext = NULL; CGRect docRect = CGRectZero; NSURL *originalURL = [[NSBundle mainBundle] URLForResource:@"test" withExtension:@"pdf"]; NSString *newPath = @"/Users/***/Desktop/new.pdf"; CFURLRef newURL = CFURLCreateWithFileSystemPath (NULL, (CFStringRef)newPath, kCFURLPOSIXPathStyle, false); //Original doc and it's dimesnions originalDoc = CGPDFDocumentCreateWithURL((CFURLRef)originalURL); CGPDFPageRef firstPage = CGPDFDocumentGetPage(originalDoc, 1); if (firstPage == NULL) { NSLog(@"This document has no pages..Exiting."); } docRect = CGPDFPageGetBoxRect(firstPage, kCGPDFCropBox); NSLog(@"%@", NSStringFromRect(docRect)); //New doc context if (newURL != NULL) { pdfContext = CGPDFContextCreateWithURL(newURL, &docRect, NULL); if (pdfContext == NULL) { NSLog(@"Error creating context"); } CFRelease(newURL); } else { NSLog(@"Error creating url"); } 

然后单独复制每个页面。 在这个特定的例子中,我将“贝茨”(编号)添加到页面。

 //Copy original to new, and write bates size_t count = CGPDFDocumentGetNumberOfPages(originalDoc); for (size_t pageNumber = 1; pageNumber <= count; pageNumber++) { CGPDFPageRef originalPage = CGPDFDocumentGetPage(originalDoc, pageNumber); CGContextBeginPage (pdfContext,nil); CGContextSetRGBFillColor(pdfContext, 0, 0, 255, 0.1); CGContextSetRGBStrokeColor(pdfContext, 0, 0, 255, 0.5); // Draw a circle (filled) CGContextFillEllipseInRect(pdfContext, CGRectMake(0, 0, 25, 25)); CGContextSaveGState(pdfContext); //flip context due to different origins CGContextTranslateCTM(pdfContext, 0.0, (docRect.size.height - (docRect.size.height * 0.80))/2); CGContextScaleCTM(pdfContext, 1.0, 0.8); //copy content of template page on the corresponding page in new file CGContextDrawPDFPage(pdfContext, originalPage); CGContextRestoreGState(pdfContext); //flip context back //CGContextTranslateCTM(pdfContext, 0.0, -(docRect.size.height - (docRect.size.height * 0.80))/2); //CGContextScaleCTM(pdfContext, 1.0, 1.25); CGContextSetRGBFillColor(pdfContext, 0, 0, 255, 0.1); CGContextSetRGBStrokeColor(pdfContext, 0, 0, 255, 0.5); // Draw a circle (filled) CGContextFillEllipseInRect(pdfContext, CGRectMake(0, 0, 25, 25)); NSString *bate = [self generateStringForCount:(int)pageNumber-1]; int fontSize = 15; CGContextSelectFont(pdfContext, "Helvetica", fontSize, kCGEncodingMacRoman); CGContextSetTextDrawingMode(pdfContext, kCGTextFill); CGContextSetTextPosition(pdfContext, 0.0f, round(fontSize / 4.0f)); CGContextShowText(pdfContext, [bate UTF8String], strlen([bate UTF8String])); CGContextEndPage(pdfContext); } CFRelease(pdfContext);