【问题标题】:problem with rendering pdf on the entire screen of pdf在pdf的整个屏幕上渲染pdf的问题
【发布时间】:2011-06-05 18:44:56
【问题描述】:

大家好,我尝试了多种方法来根据 pdf 书的大小打印 pdf。我还搜索了许多链接和许多代码,例如苹果代码、github 和许多此类网站,但没有用。这个问题与许多问题和答案重复,但我仍然没有得到确切的结果。我的代码看起来像这样

  • (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
    if(UIInterfaceOrientationIsPortrait([self interfaceOrientation])){ layer.backgroundColor=[UIColor blackColor]; myPageRef = CGPDFDocumentGetPage(myDocumentRef, c);

    CGSize pageSize = [kbDataSource pageSize];
    GLogInfo(@"the page ize we are getting in draw layer is %@ ",NSStringFromCGSize(pageSize));
    //CGContextSaveGState(ctx);
    
    CGSize aspectFitSize = [self getPageFitSizeOfSize:pageSize inSize:CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height- __TOP_BRANDING_BAR_HEIGHT)];
    CGContextSetRGBFillColor(ctx, 1.0, 1.0, 1.0, 1.0);
    CGContextFillRect(ctx, CGContextGetClipBoundingBox(ctx));
    
    
    
    GLogDebug(@"aspect fitsize of image to %@", NSStringFromCGSize(aspectFitSize));
    NSLog(@"layer bounds are:%@  %@ ",NSStringFromCGSize(layer.bounds.size),NSStringFromCGPoint(layer.bounds.origin));
    NSLog(@"page size of the book is:%@",NSStringFromCGSize(pageSize));
    CGFloat aspectRatio=pageSize.width/pageSize.height;
        CGRect cropBox = CGPDFPageGetBoxRect(myPageRef, kCGPDFCropBox);
    CGRect targetRect = layer.bounds;
    CGFloat xScale = targetRect.size.width / cropBox.size.width;
    CGFloat yScale = (targetRect.size.height-41) / cropBox.size.height;
    CGFloat scaleToApply = (xScale < yScale) ? xScale : yScale;
    
    CGContextTranslateCTM(ctx, 0.0, -41+layer.bounds.size.height);
    CGContextScaleCTM(ctx, 1.0, -1.0);
    NSLog(@"the crop box values are %@",NSStringFromCGRect(cropBox));
    NSLog(@"the crop box values are %f",cropBox.origin.x);
    
    NSLog(@"the scaleToApply is %f",scaleToApply);
    NSLog(@"the view bounds are %@",[self.view description]);
    
    if (scaleToApply == yScale)
        CGContextConcatCTM(ctx, CGAffineTransformMakeTranslation(-100, -150));
    else
        CGContextConcatCTM(ctx, CGAffineTransformMakeTranslation(-180, -260)); 
    CGContextConcatCTM(ctx, CGAffineTransformMakeScale(scaleToApply, scaleToApply));
    CGContextConcatCTM(ctx, CGPDFPageGetDrawingTransform(myPageRef, kCGPDFCropBox, layer.bounds, 0, true));
    
    
    CGContextSetInterpolationQuality(ctx, kCGInterpolationHigh); 
    CGContextSetRenderingIntent(ctx, kCGRenderingIntentDefault);
    CGContextDrawPDFPage(ctx, myPageRef);
    

    }

我粘贴了我在 drawlayer 中尝试过的所有代码。我给了很多随机数来适应整个屏幕的页面,最后我只得到了八本书(pdf)。

我的目标是打印任何覆盖整个 ipad 屏幕的 pdf。 我的结果只有少数 pdf。谁能告诉我它发生的确切原因,并告诉我将 pdf 打印到 ipad 整个屏幕的确切代码。

谢谢大家。

【问题讨论】:

    标签: iphone objective-c ipad


    【解决方案1】:

    PDF 文件中的页面没有标准尺寸。由于您正在计算比例以保持正确的纵横比,因此并非所有 PDF 文件都会占据整个屏幕。

    根据您上面的代码,您可以通过更改使所有 PDF 文件适合整个屏幕

    CGContextConcatCTM(ctx, CGAffineTransformMakeScale(scaleToApply, scaleToApply));
    

    到

    CGContextConcatCTM(ctx, CGAffineTransformMakeScale(xScale, yScale));
    

    不过,这会破坏纵横比。

    【讨论】:

    • 嗨,Altealice,我尝试了上面提到的内容,但对我的解决方案毫无帮助。我发布了我的新代码。
    • 同样的问题不适用于所有 pdfs。有些渲染得很好,有些页面没有渲染整个屏幕。
    • 在互联网上修复“不适用于所有 pdf”是相当不可能的。 PDF 文件的变化太多,难以理解。您必须进一步调查并提供更有意义的信息。
    【解决方案2】:

    为什么不直接使用 UIDocumentInteractionController 来显示 PDF?然后尝试按照您的方式呈现 PDF 会容易得多?我在几个实例上使用 UIDocumentInteractionController 来预览 PDF、word 文档、PowerPoint 等。

    看看HERE.

    这可能会满足您的需求(用 PDF 填满整个屏幕)。如果需要,它还允许您打印文档并在另一个应用程序中打开它...

    【讨论】:

    • 能否请您发布更多详细信息和示例。
    • 谢谢 Geoff Baum,它工作正常,但如果我使用 UIDocumentInteractionController,我的应用程序的功能必须改变。所以你能告诉我如何使用 drawLayer 进行渲染。
    • 抱歉,我不确定如何使用 drawLayer。只是想我会抛出一个替代方案......
    【解决方案3】:

    有一种简单的方法可以在 iPhone/iPad 中阅读 PDF: 1.取一个 UIwebView(名称:pdfView)。 2.Give Iboutlet 连接并将其委托给 FilesOwner 3.In Viewdidload

    [self.pdfView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"ObjC" ofType:@"pdf"]]]];

    4.ObjC.pdf 应该在资源文件夹中。现在要使不同大小的 pdf 页面适合 iPad 的屏幕,只需打开相应的 .xib 文件,然后转到 UIwebView 的检查器,然后是 Web 视图属性,然后有勾号缩放页面以适应选项。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-20
      • 2018-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多