3

How to convert a pdf file to image through coding in objective c

I have a pdf file, but i want to save it into photo album of iPhone, So that's why i want to convert it. If there is another way then please suggest me.

Thanks in advance.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
  • Refer [convert-a-pdf-page-to-image-on-the-iphone-and-ipad](http://ipdfdev.com/2011/03/28/convert-a-pdf-page-to-image-on-the-iphone-and-ipad/) link. Refer [image-of-pdf-page-ios](http://stackoverflow.com/questions/10919075/image-of-the-first-pdf-page-ios-sdk) link get helped – Paresh Navadiya Sep 14 '12 at 09:38

1 Answers1

-1
    CGPDFDocumentRef PDFfile;

            CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("iPhone Sample apps.pdf"), NULL, NULL);
            PDFfile = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
            CFRelease(pdfURL);

     CGPDFPageRef page = CGPDFDocumentGetPage(PDFfile,currentpage);

        context = UIGraphicsGetCurrentContext();
        CGContextSaveGState(context);
        CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);
        CGContextFillRect(context,self.bounds);   
        CGContextTranslateCTM(context, -1.0, [self bounds].size.height);
        CGContextScaleCTM(context, 1.0, -1.0);
        CGContextConcatCTM(context, CGPDFPageGetDrawingTransform(page,  kCGPDFArtBox, [self bounds], 0, true));
        CGContextDrawPDFPage(context, page);    
        CGContextRestoreGState(context);
CGImageRef imgref;
 imgref=CGBitmapContextCreateImage(context);      
image= [[UIImage alloc]initWithCGImage:imgref ];
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
NANNAV
  • 4,875
  • 4
  • 32
  • 50
  • @thomas-kilian Please post your own answer. You're inserting Swift code into an answer from 2012. Now if someone wants to downvote your version they would actually downvote OP! Anyway, please just don't put foreign code into someone else's answer. A simple typo fix is ok, for example, but not this. Thanks. // And for the edited question, see this: http://meta.stackoverflow.com/questions/311285/user-adding-off-topic-tag-to-question-to-make-it-inline-with-their-answer – Eric Aya Jun 29 '16 at 12:03
  • @EricD Ah, sorry, but no. The answer is not bound to obj-c, its CG and as such C. This is open to Swift and Obj-C. AND I inserted both code versions which is not uncommon. – qwerty_so Jun 29 '16 at 12:33
  • @ThomasKilian You can add the tag to the question if you really think it's necessary and/or useful - although I strongly disagree, I won't stop you again. // But in any way you can't hijack other users answers like that. Post your own. *This* is the important part of my comment. Thanks. :) – Eric Aya Jun 29 '16 at 12:36
  • @EricD It was not highjacking. The original answer was quite poor. And I extracted the kernel plus adding my own Swift solution as give-away. I'm not touching this thread again. If some is interested in my solution, click the edited text above. – qwerty_so Jun 29 '16 at 12:41