1
votes

I have a problem with rendering a pdf into a UIScroolView. When I launch my application in landscape mode the pdf is filled to the view width bound, but isn't filled to the view height bound. The pdf content is into the screen but appear the UIScroolView bar and under the pdf content I have a white area. But when I launch my application in portrait mode i don't have any issue, because the pdf is filled to the view bound(width and height).

The scroolView has this properties:

theScrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
theScrollView.contentSize = self.view.bounds.size;

The method that render the pdf is this:

- (void)drawLayer:(CATiledLayer *)layer inContext:(CGContextRef)context{

CGPDFPageRef drawPDFPageRef = NULL;
CGPDFDocumentRef drawPDFDocRef = NULL;

@synchronized(self) // Briefly block main thread
{
    drawPDFDocRef = CGPDFDocumentRetain(_PDFDocRef);
    drawPDFPageRef = CGPDFPageRetain(_PDFPageRef);
}

CGContextSetRGBFillColor(context, 1.0f, 1.0f, 1.0f, 1.0f);
CGContextFillRect(context, CGContextGetClipBoundingBox(context));

if (drawPDFPageRef != NULL) // Render the page into the context
{
    NSLog(@"DRAW LAYER: drawPDFPageRef != NULL ");

    CGFloat boundsHeight = self.bounds.size.height;

    if (CGPDFPageGetRotationAngle(drawPDFPageRef) == 0)
    {
        NSLog(@"DRAW LAYER: CGPDFPageGetRotationAngle(drawPDFPageRef) == 0");
        CGFloat boundsWidth = self.bounds.size.width;

        CGRect cropBoxRect = CGPDFPageGetBoxRect(drawPDFPageRef, kCGPDFCropBox);
        CGRect mediaBoxRect = CGPDFPageGetBoxRect(drawPDFPageRef, kCGPDFMediaBox);
        CGRect effectiveRect = CGRectIntersection(cropBoxRect, mediaBoxRect);

        CGFloat effectiveWidth = effectiveRect.size.width;
        CGFloat effectiveHeight = effectiveRect.size.height;

        CGFloat widthScale = (boundsWidth / effectiveWidth);
        CGFloat heightScale = (boundsHeight / effectiveHeight);

        CGFloat scale = (widthScale < heightScale) ? widthScale : heightScale;

        CGFloat x_offset = ((boundsWidth - (effectiveWidth * scale)) / 2.0f);
        CGFloat y_offset = ((boundsHeight - (effectiveHeight * scale)) / 2.0f);

        y_offset = (boundsHeight - y_offset); // Co-ordinate system adjust

        CGFloat x_translate = (x_offset - effectiveRect.origin.x);
        CGFloat y_translate = (y_offset + effectiveRect.origin.y);

        CGContextTranslateCTM(context, x_translate, y_translate);

        CGContextScaleCTM(context, scale, -scale); // Mirror Y
    }
    else // Use CGPDFPageGetDrawingTransform for pages with rotation (AKA kludge)
    {
        NSLog(@"DRAW LAYER ELSE : CGPDFPageGetRotationAngle(drawPDFPageRef) == 0");

        CGContextTranslateCTM(context, 0.0f, boundsHeight);
        CGContextScaleCTM(context, 1.0f, -1.0f);

        CGContextConcatCTM(context, CGPDFPageGetDrawingTransform(drawPDFPageRef, kCGPDFCropBox, self.bounds, 0, true));
    }

    CGContextDrawPDFPage(context, drawPDFPageRef);
}

CGPDFPageRelease(drawPDFPageRef); // Cleanup
CGPDFDocumentRelease(drawPDFDocRef);}

Thank you very much!!

1
Regarding the display of the PDF page I recommend this blog post: ipdfdev.com/2011/03/23/…, it shows how to use the page CropBox and rotation in order to display correctly any PDF page.iPDFdev

1 Answers

1
votes

I have fixed my issue symply adding

theScrollView.contentSize= self.view.bounds.size

in DidRotateFromInterfaceOrientation