2011-08-02 47 views
8

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

我该如何解决这个问题?到目前为止,我已经解析了pdf内容(我之前的帖子是pdf parsing problem)。是否有任何开放源码的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是我在创建新pdf时创建的同一个对象。我的pdf有一个像“你好世界”的线。我正在尝试添加一行。

我收到以下错误:

GContextShowTextAtPoint: invalid context 0x0 
+0

你可以在这里发布pdf解析器代码片段吗? – ajay

+0

你是否发现任何解决方案 – wan

+0

不,我没有..它需要高水平的图形。 – cancerian

回答

3

这一次将是真的棘手

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

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

http://libharu.org/

+1

嗨感谢您的答复,但我需要编辑PDF这就是我的要求。但是有什么方法可以重写pdf的全部内容,而不是仅仅编辑它。这是我的代码(编辑我的问题)。我试过,但它显示错误说无效的背景 – cancerian

7

所以在标题你说你想要的注释添加到PDF,但随后你正试图使工作在你的问题的身体的例子仅仅是添加文本到PDF 。这些都是非常不同事情....

这里是一个“Hello World”的文本添加到现有的PDF(类似于您尝试):

我没有测试过这一点,但它是基于现有的代码(我正在使用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); 
} 
+0

伟大的!真的很有帮助。有没有教程或这方面的东西。 –

+0

@KunalBalani可能有,但我没有参与。如果您有任何问题没有解决,请随时在此提出您自己的问题! – lnafziger

+0

@lnafziger您的代码将添加只读文本注释为pdf。为了使其可以在其他浏览器上编辑,您需要在/ Annotations pdf中输入内容。 –