3

I wanted to replace the content of a pdf page. I have done that by the following code,

-(void)modifyPdf:(NSString *)pdfPath atPage:(int)_page
{
NSURL* url = [NSURL fileURLWithPath:pdfPath];

CGPDFDocumentRef document = CGPDFDocumentCreateWithURL ((CFURLRef) url);

CGRect paperSize = CGPDFPageGetBoxRect(CGPDFDocumentGetPage (document, 1), kCGPDFMediaBox);
UIGraphicsBeginPDFContextToFile(pdfPath, paperSize, nil);

UIGraphicsBeginPDFPageWithInfo(paperSize, nil);

CGContextRef currentContext = UIGraphicsGetCurrentContext();

CGContextTranslateCTM(currentContext, 0, paperSize.size.height);
CGContextScaleCTM(currentContext, 1.0, -1.0); 

CGPDFPageRef page = CGPDFDocumentGetPage (document, _page);

CGContextDrawPDFPage (currentContext, page);

CGContextDrawImage(currentContext, CGRectMake(0, 0, 100, 100), [UIImage imageNamed:@"0_0_0.png"].CGImage);


UIGraphicsEndPDFContext();

CGPDFDocumentRelease(document);
document = nil;   
}

(It adds an image in a specified page,i may need to draw a path or text).

The problem i faced here is, The modified file has only the changed page. To avoid it I have drawn all the pages. But the performance is so horrible for large file as expected. Is the any way to modify only a particular page?. Is there any other better way to do it?. Please help.

Vignesh
  • 10,205
  • 2
  • 35
  • 73

0 Answers0