问题描述
嗨我正在尝试打印pdf,它打印得很好,但问题不适合每个pdf.my代码的宽度是这样的..
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 = 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宽度,但它不起作用.i发布了很多关于这个的问题,并在网上搜索aslo没有用任何人可以给我准确的缩放。
谢谢大家。
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.
推荐答案
有一种非常简单的方法来获取PDF页面的矩形,
There is a pretty easy way to get the rect of a PDF page,
CGRect pdfPageRect = CGPDFPageGetBoxRect(CGPDFDocumentGetPage(myDocumentRef,pageNumber),kCGPDFMediaBox)
;
三件事会影响绘制的PDF的大小:
Three things will effect the size of the drawn PDF:
-
CGContextTranslateCTM(ctx,0.0,
HEIGHT); // HEIGHT
CGContextTranslateCTM(ctx, 0.0, HEIGHT);//HEIGHT
CGContextConcatCTM(ctx,CGPDFPageGetDrawingTransform(p,kCGPDFCropBox,MY_RECT,0,true)); // MY_RECT
CGContextConcatCTM(ctx, CGPDFPageGetDrawingTransform(p,kCGPDFCropBox,MY_RECT, 0,true));//MY_RECT
CALayer.tileSize
CALayer.tileSize
规模也可以是受此函数中的两个数字影响: CGContextScaleCTM(ctx,1,-1);
The Scale can also be effected by the two numbers in this function: CGContextScaleCTM(ctx, 1, -1);
这篇关于在iPad上显示PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!