views:

639

answers:

1

In my cocoa application , I need to save each page of the pdf as a jpg .. How can i proceeed using Objective C

+3  A: 

Using pure Objective-C (Cocoa and PDF Kit), iterate on the pages of your PDF document, and for each one, create an NSImage whose size is that of the media box of the page, lock focus on it, tell the page to draw, create an NSBitmapImageRep with the focused view (the image), unlock focus on the image, then ask the bitmap image rep for the JPEG data and write that data to the file. (This solution sucks; don't use it.)

Another way is Core Graphics and ImageIO. Create a bitmap context matching the media box and color space of the document, then for each page, create an image destination for the JPEG file, get the page from the document, draw the page in the context, create a CGImage from the context, clear the context using CGContextClearRect, add the image the destination, and finalize the destination.

Peter Hosey
Thank you for the reply.. But I am not using any UI for displaying the PDF. I just want to browse the PDF file and it should be stored in any object then it should render each pages. So how can i use PDF Kit in my application.
You would do exactly as I said. Neither solution requires a view. (NSImage does require a connection to the window server, so if this is a command-line tool, the Core Graphics solution is the only solution).
Peter Hosey
thank u.Im able to get each page but the resolution is very low when compared to the jpg files created by exporting from Acrobat Professional.
NSPDFImageRep *img = [NSPDFImageRep imageRepWithContentsOfFile:path];int count = [img pageCount];for(int i = 0 ; i < count ; i++){[img setCurrentPage:i];NSImage *temp = [[NSImage alloc] init];[temp addRepresentation:img];NSBitmapImageRep *rep = [NSBitmapImageRep imageRepWithData:[temp TIFFRepresentation]];NSData *finalData = [rep representationUsingType:NSJPEGFileType properties:nil];NSString *pageName = [NSString stringWithFormat:@"_Page_%d.jpg", [img currentPage]];[fileMangr createFileAtPath:[NSString stringWithFormat:@"%@/%@", foldr, pageName] contents:finalData attributes:nil];}
That's another way, different from what I suggested. Come to think of it, my first suggestion may have the same problem, so I recommend the second, Quartz-based solution.
Peter Hosey
Yeah i tried your first suggestion.. samething as you said... It looses the quality....Now i need to try with your second suggestion.......
Note that in the first suggestion, you could run into subtle colorspace problems. By locking focus on an NSImage for drawing into it, you are capturing the current drawing environment rather than neutrally drawing an image. Instead, I would create a bitmap image rep from scratch and use it for the graphics context.
Mike Abdullah
How to store the pdf page data representation as an image. if we use PDFPage *page = [PDFDoc pageAtIndex:i];NSData *pageData = [page dataRepresentation];NSImage *imageData = [[NSImage alloc]initWithData:pageData];NSBitmapImageRep *rep = [[NSBitmapImageRep alloc]initWithData:[imageData TIFFRepresentation]];Here itself it looses the resolution.. How to get the exact BitMapRep ??
See what Mike Abdullah said. For that reason and the resolution reason, you should use the Quartz-based solution. Make sure you multiply the media box size in points by the resolution of the document, and use the resulting numbers as the width and height *in pixels* of the bitmap context.
Peter Hosey