On Sun, Oct 4, 2009 at 8:07 AM, Squ Aire <squ...@live.com> wrote:
>
> I know that, in an open dialog for example, I can view all PDF files by just 
> filtering using the "pdf" file extension. But that isn't good enough really, 
> because a file can have the pdf extension without being a true PDF file.

>From /usr/share/file/magic :

    # pdf:  file(1) magic for Portable Document Format
    #

    0       string          %PDF-           PDF document
    >5      byte            x               \b, version %c
    >7      byte            x               \b.%c

So, a "real" PDF begins with "%PDF-", then two bytes with the major
and minor version numbers, respectively. See "man magic" for details.

> How can I check whether a file is indeed a true PDF file (that can be, for 
> example, opened in Preview)? Would I be able to filter true PDFs file in an 
> open dialog, or would I just have to stick to filtering with only the pdf 
> extension in open dialogs, and later ignore the non-valid PDF files?

I would take the second approach. First, it's going to perform better;
to filter the list in an open dialog, you'd have to open and verify
every .pdf file in the directory being shown. You'd also need to
inform the user somehow why some supposedly-pdf files are not
selectable. Also, note that Preview's open dialog will allow you to
select any file with a .pdf extension, then alerts the user if the
file's contents aren't actually PDF data.

> Do you have sample code that checks whether a given file is a genuine PDF 
> file?

Here's a simple command-line tool that checks a single file. Please
keep in mind, this is just an example! In the Real World(tm), you'd
want more robust error checking of the number of arguments given, the
size of the file before attempting to read the first 7 bytes, etc.

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

        NSString *filename = [[[NSProcessInfo processInfo] arguments] 
objectAtIndex:1];
        char buf[7];

        [[NSData dataWithContentsOfMappedFile:filename] getBytes:(void*)buf 
length:7];

        if (strncmp("%PDF-", buf, 5) == 0)
                NSLog(@"%@ is a PDF file, version %c.%c", filename, buf[5], 
buf[6]);
        else
                NSLog(@"%@ is not a PDF file", filename);

        [pool drain];
    return 0;
}

sherm--

-- 
Cocoa programming in Perl: http://camelbones.sourceforge.net
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to