问题
I need to display the content of PDF files that i receive from a Web Service.
My problem is that all PDF boxes returns the same value. Do you have any idea what can be wrong with the files i'm using ?
CGPDFPageRef drawPDFPageRef = CGPDFPageRetain( CGPDFDocumentGetPage(pdf, 1) );
CGRect cropBoxRect = CGPDFPageGetBoxRect(drawPDFPageRef, kCGPDFCropBox);
CGRect mediaBoxRect = CGPDFPageGetBoxRect(drawPDFPageRef, kCGPDFMediaBox);
cropBoxRect = (0,0,595,842) mediaBoxRect = (0,0,595,842)
When i display the PDF in a UIWebView the content is ok
This is the PDF rendered by a UIWebView
This is the PDF rendered by CGContextDrawPDFPage
Thanks for your help, Vincent
回答1:
That doesn't look like metadata, but rather like the media box was being used to crop the PDF instead of the crop box. In your code you probably have something like this:
CGContextSetInterpolationQuality(ctx, kCGInterpolationHigh);
CGContextSetRenderingIntent(ctx, kCGRenderingIntentDefault);
CGContextSetRGBFillColor( ctx, 1.0, 1.0, 1.0, 1.0 );
CGContextFillRect( ctx, CGContextGetClipBoundingBox( ctx ));
CGContextTranslateCTM( ctx, 0.0, self.bounds.size.height );
CGContextScaleCTM( ctx, 1.0, -1.0 );
CGAffineTransform pdfXfm = CGPDFPageGetDrawingTransform( pdfPage, kCGPDFMediaBox, self.bounds, 0, true );
CGContextConcatCTM( ctx, pdfXfm );
CGContextDrawPDFPage( ctx, pdfPage );
Try using kCGPDFCropBox in CGPDFGetPageDrawingTransform, that could help
回答2:
I believe those markings are drawn outside the visible page area. Since you do not clip the content at page boundary, everything in the page content is displayed.
This code should help (before drawing the page):
CGContextAddRect(ctx, cropBoxRect);
CGContextClip(ctx);
This article shows how to display properly a PDF page on iOS: http://ipdfdev.com/2011/03/23/display-a-pdf-page-on-the-iphone-and-ipad/
来源:https://stackoverflow.com/questions/7939551/pdf-rendering-using-cgcontextdrawpdfpage-cannot-find-cropbox