添加注释到pdf

can*_*ian 8 pdf iphone objective-c ipad ios

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

我该如何解决这个问题?到目前为止,我已经解析了pdf内容(我之前的帖子是pdf解析问题).是否有任何开源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);
Run Code Online (Sandbox Code Playgroud)

pdfcontext是我在创建新pdf时创建的同一个对象.我的pdf有一行像"你好世界".我正在尝试添加一行.

我收到以下错误:

GContextShowTextAtPoint: invalid context 0x0
Run Code Online (Sandbox Code Playgroud)

lna*_*ger 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);
}
Run Code Online (Sandbox Code Playgroud)


ste*_*ete 3

这将非常棘手。

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

您可能想尝试 libHaru,它具有相当悲观的许可证并且与应用程序商店兼容。但这并不是免费的 - HARU 只能生成 pdf。

http://libharu.org/