【问题标题】:Edit PDF in iphone application在 iPhone 应用程序中编辑 PDF
【发布时间】:2011-09-07 09:45:00
【问题描述】:

现在我正在创建一个类似于“iAnnotate PDF”的应用程序 到目前为止,我已经完成了在 UIView 中阅读和显示 pdf 页面。 即使我成功获得 TOC。

现在我想编辑 PDF,编辑包括标记、突出显示和书写笔记。

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

或者有什么方法可以更新现有 pdf 中的单页? 如果是的话是怎么做的?

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

【问题讨论】:

    标签: iphone objective-c ios


    【解决方案1】:

    您可以使用 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);
    

    【讨论】:

    • 我知道如何创建 pdf。我想编辑 pdf。如何编辑单行而不是单页。我想避免重写整个pdf。如果可以在现有 pdf 中编辑单个页面,那将很容易.. 或者复制大量页面并替换特定页面的任何方式也可以。
    • 更新,展示如何复制页面。有了它,您可以在页面上绘制或添加新内容..
    • 好的.. 现在明白你的意思了.. 谢谢.. 将实现它:) 所以我们没有任何东西可以更新单个页面,对,我们需要重新创建整个 pdf :(跨度>
    • 是的 :(。您可以使用 PDFKit,但它的功能非常有限...developer.apple.com/library/mac/#documentation/GraphicsImaging/…
    • stackoverflow.com/questions/289186/… pdf 套件不适用于 ios :(
    【解决方案2】:

    也许这个想法可以在 iPhone 上运行逐页包含编辑后的页面,然后您将通过图像再次创建 PDF,然后我认为您将获得编辑后的 ​​PDF。

    【讨论】:

    • 但你不认为这会造成开销吗?对于单页我们需要写整个pdf,pdf中的页面可以没有限制。
    • 但这就是方式。我不认为没有直接的 API 或库可以提供这种功能,这取决于你的想法。
    【解决方案3】:

    现在有了最新版本的 iOS,在 iPhone 上编辑 PDF 变得非常容易。

    1. 在 iPhone 上打开 PDF(通常在书籍/照片上)
    2. 点击标记工具。
    3. 从底部的更多选项中,您可以选择文本、签名和放大镜。

    您还可以为文本添加不同的颜色。但是您不能编辑已经存在的文本。我建议你去购买一些最好的 PDF 编辑软件。

    【讨论】:

    • OP 想要实现自己的 PDF 编辑软件 - 未预装书籍/照片。
    猜你喜欢
    • 1970-01-01
    • 2011-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-05
    • 1970-01-01
    相关资源
    最近更新 更多