PDF rendering using CGContextDrawPDFPage : cannot find CropBox

岁酱吖の 提交于 2019-12-10 10:39:53

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!