开发者

Display PDF on iPad

开发者 https://www.devze.com 2023-02-05 10:27 出处:网络
hi i am trying to print pdf,It is printing well but the problem is not fit the width for every pdf.my code is like this..

hi i am trying to print pdf,It is printing well but the problem is not fit the width for every pdf.my code is like this..

//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 = heightWhenHeigh开发者_Go百科tIsFit;

    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];
}

i am trying to fit all pdfs width but it is not working..i posted many questions regarding this and searched in net aslo no use can any one please give me the exact scaling. thank you all.


There is a pretty easy way to get the rect of a PDF page,

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

Three things will effect the size of the drawn PDF:

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

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

  • CALayer.tileSize

The Scale can also be effected by the two numbers in this function: CGContextScaleCTM(ctx, 1, -1);

0

精彩评论

暂无评论...
验证码 换一张
取 消