Question

Today i searched a while for a way to programmatically pasting text in a PDF file in an iOS 7 app. Unfortunately there is no easy way to edit a PDF form. You can use a paid library but that was not an option for me. So i did it by pasting text at specified coordinates by using CGPDF. This is the way i did it:

- (void)editPDF
{
    NSString* fileName = @"new.pdf";

    NSArray *arrayPaths =
    NSSearchPathForDirectoriesInDomains(
                                        NSDocumentDirectory,
                                        NSUserDomainMask,
                                        YES);
    NSString *path = [arrayPaths objectAtIndex:0];
    NSString* pdfFileName = [path stringByAppendingPathComponent:fileName];

    NSString *templatePath = [[NSBundle mainBundle] pathForResource:@"mytemplate" ofType:@"pdf"];

    //create empty pdf file;
    UIGraphicsBeginPDFContextToFile(pdfFileName, CGRectMake(0, 0, 792, 612), nil);

    CFURLRef url = CFURLCreateWithFileSystemPath (NULL, (CFStringRef)templatePath, kCFURLPOSIXPathStyle, 0);

    //open template file
    CGPDFDocumentRef templateDocument = CGPDFDocumentCreateWithURL(url);
    CFRelease(url);

    //get amount of pages in template
    size_t count = CGPDFDocumentGetNumberOfPages(templateDocument);

    //for each page in template
    for (size_t pageNumber = 1; pageNumber <= count; pageNumber++) {
        //get bounds of template page
        CGPDFPageRef templatePage = CGPDFDocumentGetPage(templateDocument, pageNumber);
        CGRect templatePageBounds = CGPDFPageGetBoxRect(templatePage, kCGPDFCropBox);

        //create empty page with corresponding bounds in new document
        UIGraphicsBeginPDFPageWithInfo(templatePageBounds, nil);
        CGContextRef context = UIGraphicsGetCurrentContext();

        //flip context due to different origins
        CGContextTranslateCTM(context, 0.0, templatePageBounds.size.height);
        CGContextScaleCTM(context, 1.0, -1.0);

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

        //flip context back
        CGContextTranslateCTM(context, 0.0, templatePageBounds.size.height);
        CGContextScaleCTM(context, 1.0, -1.0);

        //create dictionary for font
        UIFont *font = [UIFont fontWithName: @"Courier" size:12];
        NSDictionary *attribdict = [[NSDictionary alloc] initWithObjectsAndKeys: font, NSFontAttributeName, nil];

        /* Here you can do any drawings */
        [@"Test" drawAtPoint:CGPointMake(200, 300) withAttributes:attribdict];
    }
    CGPDFDocumentRelease(templateDocument);
    UIGraphicsEndPDFContext();

    [self showPDFFile];
}

-(void)showPDFFile
{
    NSString* fileName = @"new.pdf";

    NSArray *arrayPaths =
    NSSearchPathForDirectoriesInDomains(
                                        NSDocumentDirectory,
                                        NSUserDomainMask,
                                        YES);
    NSString *path = [arrayPaths objectAtIndex:0];
    NSString* pdfFileName = [path stringByAppendingPathComponent:fileName];

    UIWebView* webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];

    NSURL *url = [NSURL fileURLWithPath:pdfFileName];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [webView setScalesPageToFit:YES];
    [webView loadRequest:request];

    [self.view addSubview:webView];
}

You only have to find out the CGPoint(s) of the point where you want to paste the text and paste a line for each text.

Very helpful was this link: how to edit a PDF in objective-c?

If anybody has a better solution for this i would appreciate if he/she can answer and post it.

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top