在iPad上显示PDF

时间:2011-01-18 09:36:09

标签: iphone objective-c ipad pdf

嗨我正在尝试打印pdf,打印效果不错,但问题不适合每个pdf.my代码的宽度都是这样的..

//for scalling
-(CGSize)getPageFitSizeOfSize:(CGSize)sourceSize inSize:(CGSize)containerSize{
    // now fit the source to the container size - compute the width and height of the //image once it is aspect fit
    // first fit it to the height

    CGFloat heightWhenHeightIsFit = containerSize.height;

    CGFloat widthWhenHeightIsFit = (sourceSize.width * heightWhenHeightIsFit)/sourceSize.height;

    CGFloat heightWhenAspectFit = heightWhenHeightIsFit;

    CGFloat widthWhenAspectFit = widthWhenHeightIsFit;

    if(widthWhenAspectFit > containerSize.width){
        widthWhenAspectFit = containerSize.width;
        heightWhenAspectFit = (sourceSize.height *widthWhenAspectFit)/sourceSize.width;
    }

    return CGSizeMake(widthWhenAspectFit, heightWhenAspectFit);
}


//printing

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{  
    int c=[currentPage intValue]+1;
    NSLog(@"drawing started");

    if(UIInterfaceOrientationIsPortrait([self interfaceOrientation])){
        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);
        NSLog(@"the CGContextGetClipBoundingBox method rect %@",NSStringFromCGRect(CGContextGetClipBoundingBox(ctx)));

    CGContextFillRect(ctx, CGContextGetClipBoundingBox(ctx));
    GLogDebug(@"aspect fitsize of image to %@", NSStringFromCGSize(aspectFitSize));
    GLogDebug(@"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;
    NSLog(@"the value of aspect ratio is %f ",aspectRatio);
    CGRect cropBox = CGPDFPageGetBoxRect(myPageRef, kCGPDFCropBox);
    NSLog(@"the crop box values %@",NSStringFromCGRect(cropBox));
    CGRect targetRect = layer.bounds;
    GLogDebug(@"the targetRect is %@",NSStringFromCGRect(targetRect));
    CGFloat xScale = targetRect.size.width / cropBox.size.width;
    CGFloat yScale = (targetRect.size.height-41) / cropBox.size.height;
    CGFloat scaleToApply = (xScale < yScale) ? xScale : yScale;
    NSLog(@"the scaleToApply is %f,X-scale is %f,Y-scale is %f",scaleToApply,xScale,yScale);

    CGContextTranslateCTM(ctx, 0.0, layer.bounds.size.height);
    CGContextScaleCTM(ctx, 1.0, -1.0);

    NSLog(@"the view bounds are %@",[self.view description]);

    if (scaleToApply == yScale){
    NSLog(@"the values of CGAffineTransformMakeTranslation in if condition is %@ ",NSStringFromCGAffineTransform(CGAffineTransformMakeTranslation(-100, -150)));
        CGContextConcatCTM(ctx, CGAffineTransformMakeTranslation(-100, -150));
    }
    else{
        CGContextConcatCTM(ctx, CGAffineTransformMakeTranslation(-180, -260)); //evans
        NSLog(@"the values of CGAffineTransformMakeTranslation in else condition is %@ ",NSStringFromCGAffineTransform(CGAffineTransformMakeTranslation(-180, -260)));

    }

    NSLog(@"the affineTransform makescale after applaying scaleToApplay %@",NSStringFromCGAffineTransform(CGAffineTransformMakeScale(scaleToApply, scaleToApply)));
    CGContextConcatCTM(ctx, CGAffineTransformMakeScale(scaleToApply, scaleToApply));
        GLogDebug(@"the CGPDFPageGetDrawingTransform values are %@",NSStringFromCGAffineTransform(CGPDFPageGetDrawingTransform(myPageRef, kCGPDFCropBox, layer.bounds, 0, true)));
    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宽度,但它不起作用..我发布了许多关于此的问题,并在网上搜索aslo没有用任何人可以给我精确缩放。 谢谢大家。

1 个答案:

答案 0 :(得分:0)

有一种非常简单的方法来获取PDF页面的矩形,

CGRect pdfPageRect = CGPDFPageGetBoxRect(CGPDFDocumentGetPage(myDocumentRef,pageNumber), kCGPDFMediaBox);

三件事会影响绘制PDF的大小:

  • CGContextTranslateCTM(ctx,0.0,    HEIGHT); // HEIGHT

  • CGContextConcatCTM(ctx,CGPDFPageGetDrawingTransform(p,kCGPDFCropBox,MY_RECT,0,true)); // MY_RECT

  • CALayer.tileSize

比例也可以通过此函数中的两个数字来实现:CGContextScaleCTM(ctx, 1, -1);