gpt4 book ai didi

iphone - 在 iPhone 应用程序中编辑 PDF

转载 作者:行者123 更新时间:2023-11-28 23:13:40 25 4
gpt4 key购买 nike

现在我正在创建一个类似于“iAnnotate PDF”的应用程序至此,我已经完成了在 UIView 中阅读和显示 pdf 页面。甚至我也成功获得了TOC。

现在我想编辑PDF,编辑包括标记、高亮和书写笔记。

现在我的问题是如何编辑?我的意思是我们是否需要创建另一个 pdf 并用新的更改覆盖它?

或者有什么方法可以更新现有 pdf 中的单个页面??如果是,那是怎么做到的?

如果我们要重写整个 pdf,会不会产生开销?

最佳答案

您可以使用 CoreGraphics 轻松创建/编辑 pdf。只需创建一个定义大小的矩形,然后创建上下文,推送上下文,调用 CFPDFContextBeginPage 并像往常一样开始绘图...这是一个示例:

    CGRect mediaBox = CGRectMake(0, 0, 550, 800);
NSString *url = [path stringByExpandingTildeInPath];
CGContextRef ctx = CGPDFContextCreateWithURL((CFURLRef)[NSURL fileURLWithPath:url], &mediaBox, NULL);
UIGraphicsPushContext(ctx);


//Page1
CGPDFContextBeginPage(ctx, NULL);
CGContextScaleCTM(ctx, 1, -1);
CGContextTranslateCTM(ctx, 0, -mediaBox.size.height);
//name and value are NSStrings
[name drawInRect:rectName withFont:fontName];
[value drawInRect:rectValue withFont:fontValue];
[[UIImage imageNamed:@"logo.png"] drawInRect:CGRectMake(8, 15, 91, 99)];
CGPDFContextEndPage(ctx);

UIGraphicsPopContext();
CFRelease(ctx);

更新您可以复制所需的页面,然后像这样在它们上面绘图:

CGPDFDocumentRef  originalDoc = NULL;
CGContextRef pdfContext = NULL;
CGRect docRect = CGRectZero;


NSURL *originalURL = [[NSBundle mainBundle] URLForResource:@"test" withExtension:@"pdf"];
NSString *newPath = @"/Users/***/Desktop/new.pdf";
CFURLRef newURL = CFURLCreateWithFileSystemPath (NULL,
(CFStringRef)newPath,
kCFURLPOSIXPathStyle,
false);

//Original doc and it's dimesnions
originalDoc = CGPDFDocumentCreateWithURL((CFURLRef)originalURL);
CGPDFPageRef firstPage = CGPDFDocumentGetPage(originalDoc, 1);
if (firstPage == NULL) {
NSLog(@"This document has no pages..Exiting.");
}
docRect = CGPDFPageGetBoxRect(firstPage, kCGPDFCropBox);
NSLog(@"%@", NSStringFromRect(docRect));


//New doc context
if (newURL != NULL) {
pdfContext = CGPDFContextCreateWithURL(newURL, &docRect, NULL);

if (pdfContext == NULL) {
NSLog(@"Error creating context");
}

CFRelease(newURL);
} else {
NSLog(@"Error creating url");
}

然后单独复制每一页。在此特定示例中,我将“Bates”(编号)添加到页面。

//Copy original to new, and write bates
size_t count = CGPDFDocumentGetNumberOfPages(originalDoc);

for (size_t pageNumber = 1; pageNumber <= count; pageNumber++) {



CGPDFPageRef originalPage = CGPDFDocumentGetPage(originalDoc, pageNumber);

CGContextBeginPage (pdfContext,nil);

CGContextSetRGBFillColor(pdfContext, 0, 0, 255, 0.1);
CGContextSetRGBStrokeColor(pdfContext, 0, 0, 255, 0.5);

// Draw a circle (filled)
CGContextFillEllipseInRect(pdfContext, CGRectMake(0, 0, 25, 25));

CGContextSaveGState(pdfContext);

//flip context due to different origins
CGContextTranslateCTM(pdfContext, 0.0, (docRect.size.height - (docRect.size.height * 0.80))/2);
CGContextScaleCTM(pdfContext, 1.0, 0.8);

//copy content of template page on the corresponding page in new file
CGContextDrawPDFPage(pdfContext, originalPage);

CGContextRestoreGState(pdfContext);

//flip context back
//CGContextTranslateCTM(pdfContext, 0.0, -(docRect.size.height - (docRect.size.height * 0.80))/2);
//CGContextScaleCTM(pdfContext, 1.0, 1.25);

CGContextSetRGBFillColor(pdfContext, 0, 0, 255, 0.1);
CGContextSetRGBStrokeColor(pdfContext, 0, 0, 255, 0.5);

// Draw a circle (filled)
CGContextFillEllipseInRect(pdfContext, CGRectMake(0, 0, 25, 25));

NSString *bate = [self generateStringForCount:(int)pageNumber-1];

int fontSize = 15;
CGContextSelectFont(pdfContext, "Helvetica", fontSize, kCGEncodingMacRoman);
CGContextSetTextDrawingMode(pdfContext, kCGTextFill);
CGContextSetTextPosition(pdfContext, 0.0f, round(fontSize / 4.0f));
CGContextShowText(pdfContext, [bate UTF8String], strlen([bate UTF8String]));


CGContextEndPage(pdfContext);

}

CFRelease(pdfContext);

关于iphone - 在 iPhone 应用程序中编辑 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7331791/

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