Extracting PDF Document Page Dimensions

Discussion in 'Mac Programming' started by Kelmon, Apr 16, 2007.

  1. Kelmon macrumors 6502a

    Kelmon

    Joined:
    Mar 28, 2005
    Location:
    United Kingdom
    #1
    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
     
  2. slooksterPSV macrumors 68040

    slooksterPSV

    Joined:
    Apr 17, 2004
    Location:
    Nowheresville
  3. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #3
    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).
     
  4. Kelmon thread starter macrumors 6502a

    Kelmon

    Joined:
    Mar 28, 2005
    Location:
    United Kingdom
    #4
    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;
    }
    
     
  5. slooksterPSV macrumors 68040

    slooksterPSV

    Joined:
    Apr 17, 2004
    Location:
    Nowheresville
    #5
    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 allocinitWithString:[openPanel filename]];
            [
    code setString:[NSString stringWithContentsOfFile:path]];
            [[
    stkWebView mainFrameloadHTMLString:[NSString stringWithContentsOfFile:pathbaseURL:[NSURL URLWithString:[NSString stringWithFormat:@"file:///%@"path]]];
            [
    editWindow setTitle:path];
        }
    }
     
  6. Kelmon thread starter macrumors 6502a

    Kelmon

    Joined:
    Mar 28, 2005
    Location:
    United Kingdom

Share This Page