如何让pdf页面适合整个视图

发布于 2024-10-05 12:35:57 字数 4195 浏览 1 评论 0原文

我阅读了pdf文件并也显示了,代码是这样的:

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx

{

NSLog(@"the layer  height %d, %d,%d ,%d",layer.bounds.size.width,layer.bounds.size.height,layer.bounds.origin.x,layer.bounds.origin.y);


int c=[currentPage intValue]+1;
NSLog(@"drawing started");



if(UIInterfaceOrientationIsPortrait([self interfaceOrientation])){
    layer.backgroundColor=[UIColor blackColor];
    myPageRef = CGPDFDocumentGetPage(myDocumentRef, c);

    NSLog(@"draw layer");
    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;
/*  
    if (aspectRatio>0.5 && aspectRatio<=0.74) {
        NSLog(@"aspect ratio is between 0.5 and 0.74");
        CGContextTranslateCTM(ctx,-110 ,layer.bounds.size.height+100);
        CGContextScaleCTM(ctx, 1.28,-1.22);
    }
    else
        if (aspectRatio>0.74 && aspectRatio<1) {
            NSLog(@"aspect ratio is between 0.75 and 1");
            //CGContextTranslateCTM(ctx, -145,layer.bounds.size.height+85);
            //CGContextScaleCTM(ctx, 1.4,-1.2);
            CGContextTranslateCTM(ctx, -130,layer.bounds.size.height+75);
            CGContextScaleCTM(ctx, 1.35,-1.18);

        }
        else if(aspectRatio>1) {
            NSLog(@"aspect ratio is above 1");
            CGContextTranslateCTM(ctx,-80,layer.bounds.size.height+80);
            CGContextScaleCTM(ctx, 1.2,-1.2);
        }
        else {
            CGContextTranslateCTM(ctx, 0, layer.bounds.size.height);
            CGContextScaleCTM(ctx, 1.0, -1.0);
        }*/


/*  
    CGFloat scalingRatio;


    if ((layer.bounds.size.width/pageSize.width)<((layer.bounds.size.height-41)/pageSize.height)){

     scalingRatio=layer.bounds.size.width/pageSize.width;

    }
    else {
        scalingRatio=(layer.bounds.size.height-41)/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)); //evans
    CGContextConcatCTM(ctx, CGAffineTransformMakeScale(scaleToApply, scaleToApply));
    CGContextConcatCTM(ctx, CGPDFPageGetDrawingTransform(myPageRef, kCGPDFCropBox, layer.bounds, 0, true));
    //
    //CGContextConcatCTM(ctx, CGAffineTransformMakeScale(scaleToApply, scaleToApply));


    CGContextSetInterpolationQuality(ctx, kCGInterpolationHigh); 
    CGContextSetRenderingIntent(ctx, kCGRenderingIntentDefault);
    CGContextDrawPDFPage(ctx, myPageRef);
    //CGContextRestoreGState(ctx);

    [loadingIndicator stopAnimating];
    [loadingIndicator1 stopAnimating];
    [loadingIndicator2 stopAnimating];

}

它读取&准确地显示pdf,但问题是,它不适合每个pdf的整个视图,我的代码中是否需要任何修改或更正。请发表一些建议和评论。谢谢你!!!

i read pdf files and also displayed, the code is like this:

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx

{

NSLog(@"the layer  height %d, %d,%d ,%d",layer.bounds.size.width,layer.bounds.size.height,layer.bounds.origin.x,layer.bounds.origin.y);


int c=[currentPage intValue]+1;
NSLog(@"drawing started");



if(UIInterfaceOrientationIsPortrait([self interfaceOrientation])){
    layer.backgroundColor=[UIColor blackColor];
    myPageRef = CGPDFDocumentGetPage(myDocumentRef, c);

    NSLog(@"draw layer");
    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;
/*  
    if (aspectRatio>0.5 && aspectRatio<=0.74) {
        NSLog(@"aspect ratio is between 0.5 and 0.74");
        CGContextTranslateCTM(ctx,-110 ,layer.bounds.size.height+100);
        CGContextScaleCTM(ctx, 1.28,-1.22);
    }
    else
        if (aspectRatio>0.74 && aspectRatio<1) {
            NSLog(@"aspect ratio is between 0.75 and 1");
            //CGContextTranslateCTM(ctx, -145,layer.bounds.size.height+85);
            //CGContextScaleCTM(ctx, 1.4,-1.2);
            CGContextTranslateCTM(ctx, -130,layer.bounds.size.height+75);
            CGContextScaleCTM(ctx, 1.35,-1.18);

        }
        else if(aspectRatio>1) {
            NSLog(@"aspect ratio is above 1");
            CGContextTranslateCTM(ctx,-80,layer.bounds.size.height+80);
            CGContextScaleCTM(ctx, 1.2,-1.2);
        }
        else {
            CGContextTranslateCTM(ctx, 0, layer.bounds.size.height);
            CGContextScaleCTM(ctx, 1.0, -1.0);
        }*/


/*  
    CGFloat scalingRatio;


    if ((layer.bounds.size.width/pageSize.width)<((layer.bounds.size.height-41)/pageSize.height)){

     scalingRatio=layer.bounds.size.width/pageSize.width;

    }
    else {
        scalingRatio=(layer.bounds.size.height-41)/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)); //evans
    CGContextConcatCTM(ctx, CGAffineTransformMakeScale(scaleToApply, scaleToApply));
    CGContextConcatCTM(ctx, CGPDFPageGetDrawingTransform(myPageRef, kCGPDFCropBox, layer.bounds, 0, true));
    //
    //CGContextConcatCTM(ctx, CGAffineTransformMakeScale(scaleToApply, scaleToApply));


    CGContextSetInterpolationQuality(ctx, kCGInterpolationHigh); 
    CGContextSetRenderingIntent(ctx, kCGRenderingIntentDefault);
    CGContextDrawPDFPage(ctx, myPageRef);
    //CGContextRestoreGState(ctx);

    [loadingIndicator stopAnimating];
    [loadingIndicator1 stopAnimating];
    [loadingIndicator2 stopAnimating];

}

it reads & displays pdf exactly, but problem is,it is not fit to entire view for each pdf,is there any modification or correction need in my code.please post some suggestions and comments. thank you!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

む无字情书 2024-10-12 12:35:57

您需要计算页面需要什么比例值才能适合您的视图。然后在您的调用中输入正确的值

CGContextScaleCTM(ctx, 1.0, -1.0);

You need to calculate what scale values you need for the page to fit your view. Then put the proper values in your

CGContextScaleCTM(ctx, 1.0, -1.0);

call.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文