问题
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