简体   繁体   English

在iphone应用程序中编辑PDF

[英]Edit PDF in iphone application

Now I am creating an application that works like ' iAnnotate PDF ' Till now I have completed reading and displaying pdf pages in UIView. 现在我正在创建一个类似于“ iAnnotate PDF ”的应用程序,直到现在我已经完成了在UIView中阅读和显示pdf页面。 Even I am sucessful in getting TOC. 即使我获得TOC也是成功的。

Now I want to edit PDFs, Editing includes marker, highlights and writing notes. 现在我想编辑PDF,编辑包括标记,高亮和书写笔记。

Now my question is how to edit? 现在我的问题是如何编辑? I mean do we need to create another pdf and over write it with new changes? 我的意思是我们需要创建另一个pdf并用新的更改写它吗?

Or there is any way we can update single page in existing pdfs?? 或者我们有什么方法可以更新现有pdf中的单页? If yes how is that done? 如果是,那怎么办?

If we are rewriting the entire pdf, wont that be creating overhead? 如果我们重写整个pdf,那会不会产生开销?

You can create/edit pdf's easily using CoreGraphics. 您可以使用CoreGraphics轻松创建/编辑pdf。 Just create a rect defining the size, then create the context, push the context, call CFPDFContextBeginPage and start drawing like you would normally do...Here's an example: 只需创建一个定义大小的矩形,然后创建上下文,推送上下文,调用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);

UPDATE You can copy the pages you require and then do the drawing over them like this: 更新您可以复制所需的页面,然后像这样绘制它们:

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");
}

And then copy each page individually. 然后单独复制每个页面。 In this particular example, I am adding "Bates" (numbering) to the pages. 在这个特定的例子中,我在页面中添加了“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中运行我认为没有直接的方式我们可以编辑PDF但我们可以做什么我们可以产生iAnnotate app具有的功能,即绘图线和文本,然后只需将PDF保存为图片页面您将在其中包含已编辑的页面,然后您将通过图像再次创建PDF然后我认为您将获得编辑的PDF。

Now with the latest release of iOS, it becomes really easy to edit PDF on your iPhone. 现在有了最新版本的iOS,在iPhone上编辑PDF变得非常容易。

  1. Open PDF on your iPhone (usually on Books/ Photos) 在iPhone上打开PDF(通常在书籍/照片上)
  2. Click on the markup tool. 单击标记工具。
  3. From the more option at the bottom, you can choose Text, Signature, and Magnifier. 从底部的更多选项中,您可以选择“文本”,“签名”和“放大镜”。

You can also add a different colour to text. 您还可以为文本添加不同的颜色。 But you can not edit the already existing text. 但是您无法编辑现有文本。 I recommend you to go for some best PDF editing software. 我建议你去寻找一些最好的PDF编辑软件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM