为pdf添加注释

我已经开发了一个PDF查看器的所有build议和代码片段。 谢谢:)现在我想使它成为一个PDF编辑器。 我想创build一个类似于PDFKit(仅适用于桌面)的iPhone / iPad应用程序。 我希望用户能够添加注释并突出显示文本部分。

我该如何解决这个问题? 到目前为止,我已经parsing了pdf内容(我之前的文章是pdfparsing问题 )。 是否有任何开放源码的PDF编辑器添加注释到现有的PDF?

另外,这可以使用Objective-C来完成,还是有任何其他的语言代码(C或JavaScript)这样做?

NSURL *newUrl = [NSURL fileURLWithPath:"path to pdf"]; CGPDFDocumentRef templateDocument = CGPDFDocumentCreateWithURL(newUrl); CFRelease(url); CGContextRef pdfContext = UIGraphicsGetCurrentContext(); CGPDFPageRef page = CGPDFDocumentGetPage(templateDocument, 1); CGContextDrawPDFPage(pdfContext, page); const char *text = "second line!"; CGContextShowTextAtPoint (pdfContext, 10, 30, text, strlen(text)); CGContextEndPage (pdfContext); CGContextRelease (pdfContext); 

pdfcontext是我创build一个新的pdf时创build的同一个对象。 我的pdf有一个像“你好世界”的线。 我正在尝试添加一行。

我收到以下错误:

 GContextShowTextAtPoint: invalid context 0x0 

因此,在标题中,您说要为PDF添加注释,但是您试图在您的问题的主体中工作的示例仅仅是将文本添加到PDF。 这些是非常不同的事情….

这是一个“Hello World”,它将文本添加到现有的pdf中(与您的尝试类似):

我没有testing过这个,但它是基于现有的代码(我正在使用CTLine而不是CGContextShowTextAtPoint绘制),所以它应该非常接近。 为了清晰起见,错误检查已被删除。

 /* * This function copies the first page of a source pdf into the destination pdf * and then adds a line of text to the destination pdf. * * This must be modified by putting the correct path into the NSURL lines * for sourceURL and destURL before it will work. * * This code is using ARC and has error checking removed for clarity. */ - (void)helloWorldPDF { // Open the source pdf NSURL *sourceURL = [NSURL fileURLWithPath:@"path to original pdf"]; CGPDFDocumentRef sourceDoc = CGPDFDocumentCreateWithURL((__bridge CFURLRef)sourceURL); // Create the new destination pdf & set the font NSURL *destURL = [NSURL fileURLWithPath:@"path to new pdf"]; CGContextRef destPDFContext = CGPDFContextCreateWithURL((__bridge CFURLRef)destURL, NULL, NULL); CGContextSelectFont(destPDFContext, "CourierNewPS-BoldMT", 12.0, kCGEncodingFontSpecific); // Copy the first page of the source pdf into the destination pdf CGPDFPageRef pdfPage = CGPDFDocumentGetPage(sourceDoc, 1); CGRect pdfCropBoxRect = CGPDFPageGetBoxRect(pdfPage, kCGPDFMediaBox); CGContextBeginPage (destPDFContext, &pdfCropBoxRect); CGContextDrawPDFPage(destPDFContext, pdfPage); // Close the source file CGPDFDocumentRelease(sourceDoc); // Draw the text const char *text = "second line!"; CGContextShowTextAtPoint(destPDFContext, 10.0, 30.0, text, strlen(text)); // Close the destination file CGContextRelease(destPDFContext); } 

这将是非常棘手的。

石英在这里并不真正帮助你。

你可能想尝试libHaru,它有一个非常悲观的许可证,并且与appstore兼容。 这不是免费的,但HARU只能生成pdf。

http://libharu.org/