Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Kelmon

macrumors 6502a
Original poster
Mar 28, 2005
733
0
United Kingdom
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
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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: 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 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

macrumors 6502a
Original poster
Mar 28, 2005
733
0
United Kingdom
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):

Code:
- (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

macrumors 68040
Apr 17, 2004
3,544
306
Nowheresville
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.
PHP:
NSURL *fileURL = [NSURL URLWithString:[NSString stringWithFormat:@"file:///%@", fileLocation]];
EDIT:
Here's a clip of code from my STK WebEdit Lite:
PHP:
- (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];
	}
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.