I need to slice and dice a lot of images from a number of multi-page PDF files, but I don't happen to have the right bit of ImageMagick installed on my MacBook Pro. Instead, I took the slightly longer route of writing a utility to do it.
Mac OS X comes with a neat little utility called "sips" which can be used to transcode images and PDFs. However, I haven't found any way of telling it to choose a particular page of a PDF file. I've googled a bit on the subject, and couldn't find anything.
Of course, I could manually extract the pages using "Preview" but there's no fun in that.
ImageMagick would also usually do the trick, but I haven't installed GhostScript as part of ImageMagick, so PDF file support is fairly broken.
Anyway, extracting pages should be a fairly simple thing to do, considering OS X's drawing layer is very closely related to PDF.
I've put the code in my Subversion repository here, so you can extract the XCode project by running:
svn co http://gidden.net/svn/ExtractPagesFromPDF/
at the command-line.
The code is very basic, and doesn't handle encrypted PDFs or anything special, so treat it as "sample code". Also, I'm no XCode/Objective-C/CoreGraphics expert by any means, so this may not be the best way of doing it! No warranties, blah blah blah. Use at will and at your own risk.
How to use:
- Build the project in XCode
- Get the executable file, "ExtractPagesFromPDF" and put it somewhere in your $PATH
- ExtractPagesFromPDF myBigDocument.pdf
- The subpages should be created in the current directory, with the format %04d.pdf (ie. 0001.pdf, 0002.pdf, ...). If you have files with those names already, move them out of the way!
Now that's written, I can now use "sips", "ImageMagick", etc. to continue the processing I need to do for the project.
If this is of any help, feel free to show your appreciation by improving the code! :-)