gpt4 book ai didi

iphone - 给pdf添加注释

转载 作者:可可西里 更新时间:2023-11-01 03:08:42 27 4
gpt4 key购买 nike

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

我该怎么做?到目前为止,我已经解析了 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 有一行像“hello world”。我正在尝试再添加一行。

我收到以下错误:

GContextShowTextAtPoint: invalid context 0x0

最佳答案

所以在标题中你说你想向 pdf 添加注释,但是你试图在问题正文中工作的示例只是向 pdf 添加文本。这些是非常不同的东西....

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

我没有对此进行测试,但它是基于现有代码(我使用 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);
}

关于iphone - 给pdf添加注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6914906/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com