Edit PDF in iphone application

前端 未结 3 767
逝去的感伤
逝去的感伤 2021-02-06 19:54

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 i

3条回答
  •  一整个雨季
    2021-02-06 20:21

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

提交回复
热议问题