2

I want to overwrite a existing PDF file, instead of creating a new one. I don't want to draw the entire PDF file but only one page if there are hundred pages in PDF file.

Actually, I want to place some images on a page of PDF files . It would be wrote over if user tap the save button . Isn't it possible ? Is there any framework or sdk ?

Thank you for any replies.

Carina
  • 2,260
  • 2
  • 20
  • 45

3 Answers3

3

PDF manipulation in iPhone is supported. Apple's documentation is the following http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_pdf/dq_pdf.html

-(void)createPDFFileFromSelectedPageNumber:(int)selectedPageNumber:(NSString*)fileName{
NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [arrayPaths objectAtIndex:0];

NSString *outpath = [[NSString alloc]initWithFormat:@"%@/output.pdf",path];

UIGraphicsBeginPDFContextToFile(outpath, CGRectZero, nil);
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 792), nil);

NSURL *url = [[NSURL alloc] initFileURLWithPath:fileName];

CGPDFDocumentRef originalPDF = CGPDFDocumentCreateWithURL((__bridge CFURLRef)url);

CGPDFPageRef pdfPage = CGPDFDocumentGetPage(originalPDF, selectedPageNumber);

CGSize size = CGSizeMake(612.0, 792.0);
CGRect outRect = CGRectMake(0, 0, 612.0, 792.0);


UIGraphicsBeginImageContext(size);

CGContextRef context = UIGraphicsGetCurrentContext();

CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(pdfPage, kCGPDFMediaBox, outRect, 0, true);
CGContextConcatCTM(context, pdfTransform);

CGContextDrawPDFPage(context, pdfPage);

UIImage *output = UIGraphicsGetImageFromCurrentImageContext();


CGContextSaveGState (context);

UIGraphicsEndImageContext();

[output drawInRect:outRect];

UIGraphicsEndPDFContext();}

Passing the filePath of the source PDF file and pageNumber to the function would give you a new PDF with the single page. Further enhancement can be done by you according to your requirements. Hope at least this helps.

Vimal Venugopalan
  • 4,091
  • 3
  • 16
  • 25
  • Thank you . But it seems that I need to create PDF files of each page ,not one page which I want to overwrite . Isn't it ? – Carina Aug 23 '12 at 01:52
  • you should access the whole pdf file and manipulate the the particular page from it and create a new pdf – Vimal Venugopalan Aug 23 '12 at 04:03
  • Only in this way can ? I would draw so many pages content to create a new pdf. – Carina Aug 23 '12 at 06:34
  • Checkout the Listing No. 14-5 in the [link](https://developer.apple.com/library/ios/#documentation/graphicsimaging/conceptual/drawingwithquartz2d/dq_pdf_scan/dq_pdf_scan.html#//apple_ref/doc/uid/TP30001066-CH220-TPXREF101) – Vimal Venugopalan Aug 23 '12 at 18:35
  • Do you mean that I could overwrite a page when `CGPDFScannerRef` scans each page in pdf document? I'm so confused. – Carina Aug 24 '12 at 06:53
  • It's very useful but not what I want . I want to get the PDF file which includes the modified page and other original pages. – Carina Aug 25 '12 at 07:39
2

Until now, iOS does not provide a method to edit the entire PDF file . Like this issue,I thought that he also want to modify one of pages.

Third-party lib will be better.

Community
  • 1
  • 1
1

Very similar :Merge PDF files on iOS

It's not possible to reopen and continue editing a PDF file . Only read and redraw whole PDF again .

Community
  • 1
  • 1
Carina
  • 2,260
  • 2
  • 20
  • 45