在iOS中,使用CGPDFDocumentRef,为什么我的PDF显示大页边距而不显示整个页面? [英] In iOS, using CGPDFDocumentRef, why is my PDF showing big margins and not the whole page?

查看:504
本文介绍了在iOS中,使用CGPDFDocumentRef,为什么我的PDF显示大页边距而不显示整个页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
如何在整个视图中容纳pdf页面

Possible Duplicate:
how to fit pdf page in entire view

我毫无问题地显示了PDF,但是,当我编译App时,原始PDF没有显示出很大的空白.我的代码:

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:

在H文件中:

@interface viewPDF : UIView 

{

CGPDFDocumentRef document;


}

在M文件中:

 - (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);

 }

任何优秀的PDF开发人员都知道如何避免边距吗?

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

推荐答案

PDF页面由2个框定义:定义物理页面大小的介质框和定义介质内部页面可见区域的裁切框盒子.您需要做的是在绘制PDF页面之前设置与裁剪框匹配的剪切路径.然后,当您绘制页面时,裁剪框外部的所有内容都会被剪切,您将在任何查看器中看到该页面.

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.

这篇关于在iOS中,使用CGPDFDocumentRef,为什么我的PDF显示大页边距而不显示整个页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆