In iOS, using CGPDFDocumentRef, why is my PDF showing big margins and not the whole page? [duplicate]

断了今生、忘了曾经 提交于 2019-12-24 04:53:25

问题


Possible Duplicate:
how to fit pdf page in entire view

I am showing with no problem a PDF, however, the original PDF does not have the big margins shown at the moment I compile the App. My code:

In the H File:

@interface viewPDF : UIView 

{

CGPDFDocumentRef document;


}

In the M file:

 - (id)initWithFrame:(CGRect)frame {

self = [super initWithFrame:(CGRect)frame];
if (self) {
    self.backgroundColor = [UIColor clearColor];
    NSString *pathToPdfDoc = [[NSBundle mainBundle] pathForResource:@"myPDF" ofType:@"pdf"];
    NSURL *pdfUrl = [NSURL fileURLWithPath:pathToPdfDoc];
    document = CGPDFDocumentCreateWithURL((__bridge CFURLRef)pdfUrl);
    currentPage = 1;

}

return self;
}

-(void)drawRect:(CGRect)inRect{                 
if(document) 

{

    CGPDFPageRef page = CGPDFDocumentGetPage(document, currentPage);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSaveGState(ctx);
    CGContextTranslateCTM(ctx, 0.0, [self bounds].size.height);

    CGContextScaleCTM(ctx, 1.0, -1.0);
    CGContextConcatCTM(ctx, CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, [self bounds], 0, true));
    CGContextDrawPDFPage(ctx, page);    
    CGContextRestoreGState(ctx);

 }

Does any great PDF developer know how to avoid the margins?


回答1:


The PDF page is defined by 2 boxes: the media box which defines the physical page size and the crop box which defines the visible area of the page inside the media box. What you need to do is to set a clipping path that matches the crop box before drawing the PDF page. Then when you draw the page everything outside the crop box will be clipped and you will see the page as you see it in any viewer.



来源:https://stackoverflow.com/questions/11641068/in-ios-using-cgpdfdocumentref-why-is-my-pdf-showing-big-margins-and-not-the-wh

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