views:

540

answers:

1

I have a PDF file where every page is a (LZW) TIFF file. I know this because I created it. I want to be able to load it and save it as a bunch of TIFF files.

I can open the PDF file with CGPDFDocumentCreateWithURL, and get a page. I can even draw the page onto the screen.

What I WANT to do is draw the page into a bitmapContext, so that I can use CGBitmapContextCreateImage to get the image into a CGImageRef. However, in order to create a bitmap context, I need to know the size and resolution of the image. I can't seem to find out how to get either a CGPDFDocument or a CGPDFPage to tell me the resolution of the image object on that page.

Is there an easier way to do this that I'm not realizing?

thanks.

A: 

Ghostscript will work for you here :

gs -sDEVICE=tiff32nc -sOutputFile=foo-Page%d.tif foo.pdf

For 2 page document foo.pdf you should get :

  • foo-Page1.tif
  • foo-Page2.tif

From memory I think the output resolution from GS is that of the containing Page, not necessarily the resolution of the embedded file (unless these are the same to begin with).

If this is the case and you want to recover the image as it was originally res-wise, you can use iText (java) or iTextSharp(.net) to get to the image content stream (ie. Bytes) and write them out to disk in the format of your choice, after converting the content stream into a PdfImage iirc.

Hope the ghostscript option is applicable to save writing yet another utility...

Spiffeah
I'd use the pdfimages command (from poppler-utils) to extract the images at their native resolution, instead of having gs render it. But yeah, there are libs you can use to do it programmatically.
Peter Cordes