7

I have a PDF file which I downloaded from a server, sometimes the users uploads a corrupted PDF and I need to check if my application can open such a file or not.

So is there a built-in way I could use to check for the PDF corruption ? If not is there a free lightweight PDF framework that I could use to view or checking the corruption ?

Note: Currently I am opening PDF files on a UIWebView.

suspectus
  • 16,548
  • 8
  • 49
  • 57
Ahmed I. Khalil
  • 743
  • 6
  • 20
  • Corrupted how? Compete junk or only certain defects in detail? – mkl Jul 28 '13 at 18:58
  • Well, I am not sure which part of the file is corrupted. I knew that it is corrupted when I tried to open it with a PDF reader (Adobe Reader for example). In my case I only see a white page when opening that file, so I want to inform the user that the file is corrupted like a normal PDF reader would do. – Ahmed I. Khalil Jul 28 '13 at 22:31

1 Answers1

16

I've found the solution using the CoreGraphics PDF drawing APIs. Thanks for this answer.

NSString *path = [[NSBundle mainBundle] pathForResource:@"A_Corrupted_PDF" ofType:@"pdf"];
NSData *data = [[NSFileManager defaultManager] contentsAtPath:path];

CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);
CGPDFDocumentRef document = CGPDFDocumentCreateWithProvider(provider);

if (document == nil) {
    NSLog(@"The PDF is corrupted");
}
CGDataProviderRelease(provider);
CGPDFDocumentRelease(document);
Community
  • 1
  • 1
Ahmed I. Khalil
  • 743
  • 6
  • 20