In iOS, can I use offsets in CGContextDrawPDFPage to show only half of a PDF page?

317 views Asked by At

I have PDF pages that are saved as spreads. This works out for me in one view where the user can zoom, highlight text and click on hyperlinks. However, I want to use the same PDF files in another view that requires individual pages instead of spreads. I'm hoping I can avoid having to have the user download extraneous PDF files.

I was able to adapt the awesome "Leaves" project to do what I wanted. But can I change any of the following code to get it to draw a PDF but cropping off either the right or left half of the page, and changing the offset when necessary?

- (void) renderPageAtIndex:(NSUInteger)index inContext:(CGContextRef)ctx
    {
        CGPDFPageRef page = CGPDFDocumentGetPage(myDocumentRef, index + 1);
        CGAffineTransform transform = aspectFit(CGPDFPageGetBoxRect(page, kCGPDFMediaBox), CGContextGetClipBoundingBox(ctx));
        CGContextConcatCTM(ctx, transform);
        CGContextDrawPDFPage(ctx, page);
    }


- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
    {
        CGContextSetRGBFillColor(ctx, 1.0, 1.0, 1.0, 1.0);
        CGContextFillRect(ctx, CGContextGetClipBoundingBox(ctx));
        CGContextTranslateCTM(ctx, 0.0, layer.bounds.size.height);
        CGContextScaleCTM(ctx, 1.0, -1.0);
        CGContextConcatCTM(ctx, CGPDFPageGetDrawingTransform(myPageRef, kCGPDFCropBox, layer.bounds, 0, true));
        CGContextDrawPDFPage(ctx, myPageRef);
    } 

Or if there's another approach to accomplishing the same thing, that would also be welcome.

Thanks!

0

There are 0 answers