ios - 在 iOS 中,使用 CGPDFDocumentRef,为什么我的 PDF 显示大边距而不是整个页面?

标签 ios xcode pdf margins cgpdfdocument

Possible Duplicate:
how to fit pdf page in entire view

我毫无问题地显示了 PDF,但是,原始 PDF 在我编译应用程序时没有显示大边距。我的代码:

在 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 开发人员知道如何避免边距吗?

最佳答案

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

关于ios - 在 iOS 中,使用 CGPDFDocumentRef,为什么我的 PDF 显示大边距而不是整个页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11641068/

相关文章:

ios - 自定义 iOS 推送通知声音

ios - 如何将结构参数与 OCMock 匹配?

iOS URL 请求。信号量问题

html - CSS 到 PDF,使用 THEAD 在新页面上重复标题

ios - UINavigationBar 中的 UILabel 在横向中被切断

xcode - 使用 Mac Catalyst 上传新应用程序版本时出错

objective-c - iOS 4 上的推送通知如何显示?

ios - 如何在保持连接的同时将我的工作转移到另一个大小级别?

iOS:来自多个 UIView 的单页 PDF

xcode - NSImages 到 PDF 并在 Swift 中合并它们