Created
February 8, 2010 05:16
-
-
Save atr000/297901 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
#import <Quartz/Quartz.h> | |
// prints a short hint about how to use the utility | |
void printUsage (void) { | |
printf("\npdfprops - Tool to modify PDF metadata\n\n"); | |
printf("Usage: pdfprops [-file PDF file path] [arguments]\n\n"); | |
printf("Arguments:\n"); | |
printf("\t[-author 'Max Frisch']\n"); | |
printf("\t[-title 'Homo faber']\n"); | |
printf("\t[-subject 'Whatever']\n"); | |
printf("\t[-creator 'A wonderful Mac application']\n"); | |
printf("\t[-keywords 'Literature,Fiction,Book']\n\n"); | |
printf("Example: pdfprops -file '/Users/steve/Desktop/test.pdf' -author 'Steve Jobs' -keywords 'iPhone,Mac,iPod'\n\n"); | |
} | |
// workhorse routine | |
int main (int argc, const char * argv[]) { | |
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; | |
// checking the given command line arguments | |
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; | |
NSString *filePath = [userDefaults stringForKey:@"file"]; | |
if (filePath == NULL) { | |
printf("ERROR: Missing PDF file path\n"); | |
printUsage(); | |
return 1; | |
} | |
NSString *author = [userDefaults stringForKey:@"author"]; | |
NSString *title = [userDefaults stringForKey:@"title"]; | |
NSString *subject = [userDefaults stringForKey:@"subject"]; | |
NSString *creator = [userDefaults stringForKey:@"creator"]; | |
NSString *keywordsString = [userDefaults stringForKey:@"keywords"]; | |
NSArray *keywords; | |
if (!keywordsString == nil) { | |
keywords = [keywordsString componentsSeparatedByString:@","]; | |
} | |
// does the given file path exist? | |
NSFileManager *fileManager = [NSFileManager defaultManager]; | |
BOOL isDir; | |
if (! [fileManager fileExistsAtPath:filePath isDirectory:&isDir]) { | |
printf("ERROR: The given file path does not exist\n%s\n", [filePath UTF8String]); | |
return 1; | |
} else { | |
if (isDir) { | |
printf("ERROR: The given file path points to a directory\n%s\n", [filePath UTF8String]); | |
return 1; | |
} | |
} | |
// loading the PDF document | |
NSURL *fileURL = [NSURL fileURLWithPath:filePath]; | |
PDFDocument *pdfDoc = [[PDFDocument alloc] initWithURL:fileURL]; | |
if (pdfDoc == NULL) { | |
printf("ERROR: The file does not seem to be a PDF document\n%s\n", [filePath UTF8String]); | |
return 1; | |
} | |
// modifying the PDF document attributes | |
NSMutableDictionary *pdfProps = [[NSMutableDictionary alloc] initWithCapacity:1]; | |
[pdfProps addEntriesFromDictionary:[pdfDoc documentAttributes]]; | |
if (!author == nil) { | |
[pdfProps setValue:author forKey:@"Author"]; | |
} | |
if (!title == nil) { | |
[pdfProps setValue:title forKey:@"Title"]; | |
} | |
if (!subject == nil) { | |
[pdfProps setValue:subject forKey:@"Subject"]; | |
} | |
if (!creator == nil) { | |
[pdfProps setValue:creator forKey:@"Creator"]; | |
} | |
if (!keywordsString == nil) { | |
[pdfProps setValue:keywords forKey:@"Keywords"]; | |
} | |
[pdfDoc setDocumentAttributes:pdfProps]; | |
// writing the PDF out | |
BOOL pdfWriteSuccess = [pdfDoc writeToFile:filePath]; | |
if (! pdfWriteSuccess) { | |
printf("ERROR: Could not write to file\n%s\n", [filePath UTF8String]); | |
return 1; | |
} | |
// cleaning up | |
[pdfProps release]; | |
[pdfDoc release]; | |
[pool release]; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment