diff --git a/PdfView.js b/PdfView.js index 686f39f0..8f4ceef2 100644 --- a/PdfView.js +++ b/PdfView.js @@ -43,7 +43,7 @@ export default class PdfView extends Component { scale: 1, spacing: 10, style: {}, - fitPolicy: 0, + fitPolicy: 2, horizontal: false, page: 1, currentPage: -1, @@ -63,6 +63,7 @@ export default class PdfView extends Component { page: -1, currentPage: -1, pageAspectRate: 0.5, + pdfPageSize: {width: 0, height: 0}, contentContainerSize: {width: 0, height: 0}, scale: this.props.scale, contentOffset: {x: 0, y: 0}, @@ -88,7 +89,8 @@ export default class PdfView extends Component { pdfLoaded: true, fileNo: pdfInfo[0], numberOfPages: pdfInfo[1], - pageAspectRate: pdfInfo[3] === 0 ? 1 : pdfInfo[2] / pdfInfo[3] + pageAspectRate: pdfInfo[3] === 0 ? 1 : pdfInfo[2] / pdfInfo[3], + pdfPageSize: {width: pdfInfo[2], height: pdfInfo[3]} }); if (this.props.onLoadComplete) this.props.onLoadComplete(pdfInfo[1], this.props.path); } @@ -156,10 +158,10 @@ export default class PdfView extends Component { return this.state.contentContainerSize.height * this.state.pageAspectRate * this.state.scale; case 2: //fit both default: { - if ((this.state.contentContainerSize.width / this.state.contentContainerSize.height) > this.state.pageAspectRate) { - return this.state.contentContainerSize.height * this.state.scale * this.state.pageAspectRate; - } else { + if (this.state.contentContainerSize.width/this.state.contentContainerSize.height this.state.pageAspectRate) { - return this.state.contentContainerSize.height * this.state.scale; - } else { - + if (this.state.contentContainerSize.width/this.state.contentContainerSize.height *)changedProps - (void)reactSetFrame:(CGRect)frame { [super reactSetFrame:frame]; - CATiledLayer *tiledLayer = (CATiledLayer *)[self layer]; - tiledLayer.tileSize = frame.size; + _viewFrame = frame; } -(void)drawLayer:(CALayer*)layer inContext:(CGContextRef)context @@ -100,33 +101,26 @@ -(void)drawLayer:(CALayer*)layer inContext:(CGContextRef)context // Fill the background with white. CGContextSetRGBFillColor(context, 1.0,1.0,1.0,1.0); - CGContextFillRect(context, self.bounds); + CGContextFillRect(context, _viewFrame); // PDF page drawing expects a Lower-Left coordinate system, so we flip the coordinate system before drawing. CGContextScaleCTM(context, 1.0, -1.0); + CGContextTranslateCTM(context, 0, -_viewFrame.size.height); - CGRect pageBounds; - - CGRect cropBox = CGPDFPageGetBoxRect(pdfPage, kCGPDFCropBox); - - pageBounds = CGRectMake(0, - -self.bounds.size.height, - cropBox.size.width, - cropBox.size.height); - - // first transform the same size as original pdf page - CGAffineTransform pageTransform = CGPDFPageGetDrawingTransform(pdfPage, kCGPDFCropBox, pageBounds, 0, true); - CGContextConcatCTM(context, pageTransform); + CGRect pdfPageRect = CGPDFPageGetBoxRect(pdfPage, kCGPDFMediaBox); - // then calculate the real scale and scale it - CGFloat scale = 1.0f; - if (self.bounds.size.width/self.bounds.size.height>cropBox.size.width/cropBox.size.height) { - scale = self.bounds.size.height/cropBox.size.height; - } else { - scale = self.bounds.size.width/cropBox.size.width; + int rotation = CGPDFPageGetRotationAngle(pdfPage); + if (rotation == 90 || rotation == 270) { + pdfPageRect = CGRectMake(0, 0, pdfPageRect.size.height, pdfPageRect.size.width); } - CGContextScaleCTM(context, scale, scale); + CGContextScaleCTM(context, _viewFrame.size.width/pdfPageRect.size.width, _viewFrame.size.height/pdfPageRect.size.height); + + if (rotation == 90 || rotation == 270) { + CGContextRotateCTM(context, -rotation*M_PI/180); + CGContextTranslateCTM(context, -pdfPageRect.size.height, 0); + } + // draw the content to context CGContextDrawPDFPage(context, pdfPage);