Skip to content

Instantly share code, notes, and snippets.

@atr000
Created April 1, 2010 01:46
Show Gist options
  • Save atr000/351205 to your computer and use it in GitHub Desktop.
Save atr000/351205 to your computer and use it in GitHub Desktop.
#import <Foundation/Foundation.h>
#import <Quartz/Quartz.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// collecting the given command line arguments
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *pdfFilePath = [userDefaults stringForKey:@"file"];
if (!pdfFilePath) {
printf("Error: Missing PDF file path (-file)\n");
return 1;
}
// does the given PDF file path exist? and if so, is it a folder?
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL isDir;
if (![fileManager fileExistsAtPath:pdfFilePath isDirectory:&isDir]) {
printf("Error: The given PDF file path does not exist:\n\t%s\n", [pdfFilePath UTF8String]);
return 1;
} else {
if (isDir) {
printf("Error: The given PDF file points to a directory:\n\t%s\n", [pdfFilePath UTF8String]);
return 1;
}
}
// creating the PDF object
NSURL *pdfFileURL = [NSURL fileURLWithPath:pdfFilePath];
PDFDocument *pdfDoc = [[PDFDocument alloc] initWithURL:pdfFileURL];
if (!pdfDoc) {
printf("Error: We could not create a PDF object from the given file path:\n\t%s\n", [pdfFilePath UTF8String]);
return 1;
}
//printing the page count!
int pageNums = [pdfDoc pageCount];
[pdfDoc release];
printf("Page count: %i\n", pageNums);
[pool drain];
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment