添加注释到pdf [英] add annotation to pdf
问题描述
我开发了一个包含所有建议和代码片段的pdf查看器。谢谢:)现在我想让它成为一个PDF编辑器。我想为iphone / ipad创建一个类似于PDFKit的应用程序(仅适用于桌面)。我希望用户能够添加注释并突出显示文本部分。
I have developed a pdf viewer with all your suggestions and code snippets. Thanks :) Now i want to make it a pdf editor. I want to create an app for iphone/ipad similar to PDFKit(which is only for desktop). I want the user to be able to add annotations and highlight text sections.
我该如何解决这个问题?到目前为止,我已经解析了pdf内容(我以前的帖子是 pdf解析问题)。是否有任何开源pdf编辑器为现有的pdf添加注释?
How do I go about with this? So far, I have parsed the pdf content (my previous post is pdf parsing problem). Is there any open source pdf editor which adds annotation to existing pdf???
此外,这可以使用Objective-C完成,还是有其他语言代码(C或javascript)可以做到这一点?
Also, can this be done using Objective-C or is there any other language code (C or javascript) which does this?
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
与我创建的对象相同创建一个新的pdf。我的pdf有一行像你好世界。我正在尝试添加一行。
pdfcontext
is the same object I created while creating a new pdf. My pdf has a line like "hello world". I'm trying to add one more line.
我收到以下错误:
GContextShowTextAtPoint: invalid context 0x0
推荐答案
所以在标题中你说你想要为pdf添加注释,但是您尝试在问题正文中工作的示例只是向pdf添加文本。这些是非常不同的东西....
So in the title you say that you want to add an annotation to a pdf, but then the example that you are trying to make work in the body of your question is simply adding text to the pdf. These are very different things....
这是一个Hello World,它将文本添加到现有的pdf中(类似于您的尝试):
Here is a "Hello World" which adds text to an existing pdf (similar to your attempt):
我没有对此进行测试,但它是基于现有代码(我使用CTLine而不是CGContextShowTextAtPoint绘图)所以它应该非常接近。为清楚起见,错误检查已被删除。
I have not tested this but it is based on existing code (where I was drawing using a CTLine instead of CGContextShowTextAtPoint) so it should be very close. Error checking has been removed for clarity.
/*
* 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);
}
这篇关于添加注释到pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!