Skip to content
Guides

Extract Document Information from PDF

This sample shows how to extract information about PDF documents, such as: author, date created.

objective-c
// Init Methods
- (instancetype)initWithDocument:(CPDFDocument *)document {
  CPDFDocument *myDocument = document;
  [self printDocumentInfo:myDocument];
}

// Print document info
- (void)printDocumentInfo:(CPDFDocument *)document {
  NSString *commandLineStr = @"";
  
  // Abstract
    NSDictionary *documentAttributes = [document documentAttributes];
    commandLineStr = [commandLineStr stringByAppendingFormat:@"File Name: %@\n", document.documentURL.lastPathComponent];
    commandLineStr = [commandLineStr stringByAppendingFormat:@"File Szie: %@\n", [self fileSizeStr]];
    commandLineStr = [commandLineStr stringByAppendingFormat:@"Title: %@\n", documentAttributes[CPDFDocumentTitleAttribute]];
    commandLineStr = [commandLineStr stringByAppendingFormat:@"Author: %@\n", documentAttributes[CPDFDocumentAuthorAttribute]];
    commandLineStr = [commandLineStr stringByAppendingFormat:@"Subject: %@\n", documentAttributes[CPDFDocumentSubjectAttribute]];
    commandLineStr = [commandLineStr stringByAppendingFormat:@"Keywords: %@\n", documentAttributes[CPDFDocumentKeywordsAttribute]];
  
  // Create Information
    NSString * versionString = [NSString stringWithFormat:@"%ld.%ld",(long)document.majorVersion,(long)document.minorVersion];
    commandLineStr = [commandLineStr stringByAppendingFormat:@"Version: %@\n", versionString];
    commandLineStr = [commandLineStr stringByAppendingFormat:@"Pages: %zd\n", document.pageCount];
    commandLineStr = [commandLineStr stringByAppendingFormat:@"Creator: %@\n", documentAttributes[CPDFDocumentCreatorAttribute]];
    
    {
      // Creation Date
        if (documentAttributes[CPDFDocumentCreationDateAttribute]) {
            NSMutableString* mSting = [NSMutableString string];
            NSString*    tstring = [NSString stringWithFormat:@"%@",documentAttributes[CPDFDocumentCreationDateAttribute]];
            if (tstring.length >= 16) {
                NSRange        range;
                range.location = 2;range.length=4;
                [mSting appendString:[tstring substringWithRange:range]];
                range.location = 6;range.length=2;
                [mSting appendFormat:@"-%@",[tstring substringWithRange:range]];
                range.location = 8;range.length=2;
                [mSting appendFormat:@"-%@",[tstring substringWithRange:range]];
                
                range.location = 10;range.length=2;
                [mSting appendFormat:@" %@",[tstring substringWithRange:range]];
                range.location = 12;range.length=2;
                [mSting appendFormat:@":%@",[tstring substringWithRange:range]];
                range.location = 14;range.length=2;
                [mSting appendFormat:@":%@",[tstring substringWithRange:range]];
                
                commandLineStr = [commandLineStr stringByAppendingFormat:@"Creation Date: %@\n", mSting];
            }
        }
    }
    
    {
      // Modification Date
        if (documentAttributes[CPDFDocumentModificationDateAttribute]){
            NSMutableString* mSting = [NSMutableString string];
            NSString*    tstring = [NSString stringWithFormat:@"%@",documentAttributes[CPDFDocumentModificationDateAttribute]];

            if (tstring.length >= 16) {
                NSRange        range;
                range.location = 2;range.length=4;
                [mSting appendString:[tstring substringWithRange:range]];
                range.location = 6;range.length=2;
                [mSting appendFormat:@"-%@",[tstring substringWithRange:range]];
                range.location = 8;range.length=2;
                [mSting appendFormat:@"-%@",[tstring substringWithRange:range]];
                
                range.location = 10;range.length=2;
                [mSting appendFormat:@" %@",[tstring substringWithRange:range]];
                range.location = 12;range.length=2;
                [mSting appendFormat:@":%@",[tstring substringWithRange:range]];
                range.location = 14;range.length=2;
                [mSting appendFormat:@":%@",[tstring substringWithRange:range]];
                
                commandLineStr = [commandLineStr stringByAppendingFormat:@"Modification Date: %@\n", mSting];
            }
        }
    }
    
  // Accesss permisssions
    commandLineStr = [commandLineStr stringByAppendingFormat:@"Printing: %@\n", document.allowsPrinting ? @"true" : @"flase"];
    commandLineStr = [commandLineStr stringByAppendingFormat:@"Content Copying:: %@\n", document.allowsCopying ? @"true" : @"flase"];
    commandLineStr = [commandLineStr stringByAppendingFormat:@"Document Change: %@\n", document.allowsDocumentChanges ? @"true" : @"flase"];
    commandLineStr = [commandLineStr stringByAppendingFormat:@"Document Assembly: %@\n", document.allowsDocumentAssembly ? @"true" : @"flase"];
    commandLineStr = [commandLineStr stringByAppendingFormat:@"Commenting: %@\n", document.allowsCommenting ? @"true" : @"flase"];
    commandLineStr = [commandLineStr stringByAppendingFormat:@"Filling of Form Field: %@\n", document.allowsFormFieldEntry ? @"true" : @"flase"];
}

// Get file size
- (NSString *)fileSizeStr {

    NSFileManager *defaultManager = [NSFileManager defaultManager];
    if (![defaultManager fileExistsAtPath:self.document.documentURL.path]) {
        return @"";
    }
    
    NSDictionary *attrib = [[NSFileManager defaultManager] attributesOfItemAtPath:self.document.documentURL.path error:nil];
    float tFileSize   = [[attrib objectForKey:NSFileSize] floatValue];
    
    float fileSize = tFileSize / 1024;
    float size = fileSize >= 1024 ?(fileSize < 1048576 ? fileSize/1024.0 : fileSize/1048576.0) : fileSize;
    char  unit = fileSize >= 1024 ? (fileSize < 1048576 ? 'M':'G'):'K';
    return [NSString stringWithFormat:@"%0.1f%c", size, unit];
}