PDA

View Full Version : Extracting PDF Document Page Dimensions




Kelmon
Apr 16, 2007, 01:18 PM
Hi Guys,

I'm hoping that someone can help me as this is driving me nuts. I need to write a quick 'n' dirty application that can look a PDF files in a directory and output the page dimensions for each document (we're testing that they are all A4). While Apple seems to have delivered a useful framework in 10.4, PDF Kit, I'm damned if I can find a method in either PDFDocument or PDFPage that will return the page dimensions of the PDFDocument object. The PDFDocument object has an instance method called "documentAttributes" but that only seems to return information about the author, etc. (5 keys in all). There is a boundsForBox method for PDFPage that will return an instance of NSRect (depending upon which of the constant values specified you use, and I'm not sure what each of them is meant to do) but I have no idea how to translate an NSRect struct into something like 21.0 x 29.7cm.

Any ideas?

Regards,

Kelmon



slooksterPSV
Apr 16, 2007, 07:13 PM
convert cm to pixels
72 pixels = 1 inch

robbieduncan
Apr 17, 2007, 04:11 AM
convert cm to pixels
72 pixels = 1 inch

Normally I'd say that that's not a safe assumption to make, but various parts of the PDFKit documentation refer to "page space" as being 72 ppi with an origin in the lower left.

I'm still not 100% convinced. Are you using the boundsForBox: (http://developer.apple.com/documentation/GraphicsImaging/Reference/QuartzFramework/Classes/PDFPage_Class/index.html#//apple_ref/doc/uid/TP40003875) method of PDFPage? If so I'd expect the NSRect returned to be in the same co-ordinate space as box, which for most box types (http://developer.apple.com/documentation/GraphicsImaging/Reference/QuartzFramework/Classes/PDFPage_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40001306-430356) appears to be user-space units. Apple helpfully suggest you read the Adobe PDF documentation to find out what this means (and don't provide a link).

Kelmon
Apr 17, 2007, 01:12 PM
I think this issue is solved. The following appears to generate results consistent with the page dimensions of the PDF file as according to Preview and Adobe Reader (note: I'm just learning Cocoa so this code may well be shockingly poor):


- (NSSize) sizeOfPDFDocument:(NSURL *) documentURL {

// Declare size struct to return
NSSize documentSize;

// Get the PDFDocument
PDFDocument *myDocument = [[PDFDocument alloc] initWithURL:documentURL];

// Get the pixel dimensions of the first page of the PDF document
PDFPage *firstPage = [myDocument pageAtIndex:0];
NSRect bounds = [firstPage boundsForBox:kPDFDisplayBoxMediaBox];
NSSize pixelSize = bounds.size;

// Convert the pixel dimensions to inches and return
documentSize.width = pixelSize.width / 72;
documentSize.height = pixelSize.height / 72;

// Release objects
[myDocument release];

return documentSize;
}

slooksterPSV
Apr 17, 2007, 06:10 PM
if you need to convert a file to NSURL, here's how to do it:
fileLocation could be what is returned from opening a document. Make it global if needed.

NSURL *fileURL = [NSURL URLWithString:[NSString stringWithFormat:@"file:///%@", fileLocation]];

EDIT:
Here's a clip of code from my STK WebEdit Lite:

- (void)openPanelDidEnd:(NSOpenPanel *)openPanel returnCode:(int)returnCode contextInfo:(void *)contextInfo
{
NSString *path;
//Did they choose open?
if(returnCode == NSOKButton) {
path = [openPanel filename];
globalpath = [[NSString alloc] initWithString:[openPanel filename]];
[code setString:[NSString stringWithContentsOfFile:path]];
[[stkWebView mainFrame] loadHTMLString:[NSString stringWithContentsOfFile:path] baseURL:[NSURL URLWithString:[NSString stringWithFormat:@"file:///%@", path]]];
[editWindow setTitle:path];
}
}

Kelmon
Apr 18, 2007, 01:33 AM
Always handy information. Thanks for that.