开发者

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. Even I am sucessful in getting TOC.

Now I want to edit PDFs, Editing includes marker, highlights and writing notes.

Now my question is how to edit? I mean do we need to create another pdf and over write it with new changes?

Or there is any way we can update single page in existing pdfs?? If yes how is that done?

If we are rewriting the entire pdf, wont that be creating overhead?


You can create/edit pdf's easily using CoreGraphics. 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:

    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.

//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);


Maybe this idea can work in iPhone I think there is no directly way we can edit a PDF but what we can do we can produce such functionality that iAnnotate app have i.e. drawing line and text and then just save the PDF as a picture page by page in which you will include the edited page too and then you will create the PDF again by images then I think so you will get the edited PDF.


Now with the latest release of iOS, it becomes really easy to edit PDF on your iPhone.

  1. Open PDF on your iPhone (usually on Books/ Photos)
  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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜