Rendering a CGPDFPage into a UIImage
Asked Answered
D

1

6

I'm trying to render a CGPDFPage (selected from a CGPDFDocument) into a UIImage to display on a view.

I have the following code in MonoTouch which gets me part way there.

RectangleF PDFRectangle = new RectangleF(0, 0, UIScreen.MainScreen.Bounds.Width, UIScreen.MainScreen.Bounds.Height);

    public override void ViewDidLoad ()
    {
        UIGraphics.BeginImageContext(new SizeF(PDFRectangle.Width, PDFRectangle.Height));
        CGContext context = UIGraphics.GetCurrentContext();
        context.SaveState();

        CGPDFDocument pdfDoc = CGPDFDocument.FromFile("test.pdf");
        CGPDFPage pdfPage = pdfDoc.GetPage(1);  

        context.DrawPDFPage(pdfPage);
        UIImage testImage = UIGraphics.GetImageFromCurrentImageContext();

        pdfDoc.Dispose();
        context.RestoreState();

        UIImageView imageView = new UIImageView(testImage);
        UIGraphics.EndImageContext();

        View.AddSubview(imageView);
    }

A section of the CGPDFPage is displayed but rotated back-to-front and upside down. My question is, how do I select the full pdf page and flip it round to display correctly. I have seen a few examples using ScaleCTM and TranslateCTM but couldn't seem to get them working.

Any examples in ObjectiveC are fine, I'll take all the help I can get :)

Thanks

Diastrophism answered 9/1, 2011 at 14:48 Comment(0)
C
16

I haven't worked with MonoTouch. However, in objective-C you would get an image for a PDF page like this (notice the CTM transforms):

-(UIImage *)getThumbForPage:(int)page_number{
 CGFloat width = 60.0;

    // Get the page
 CGPDFPageRef myPageRef = CGPDFDocumentGetPage(myDocumentRef, page);
 // Changed this line for the line above which is a generic line
 //CGPDFPageRef page = [self getPage:page_number];

 CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
 CGFloat pdfScale = width/pageRect.size.width;
 pageRect.size = CGSizeMake(pageRect.size.width*pdfScale, pageRect.size.height*pdfScale);
 pageRect.origin = CGPointZero;


 UIGraphicsBeginImageContext(pageRect.size);

 CGContextRef context = UIGraphicsGetCurrentContext();

 // White BG
 CGContextSetRGBFillColor(context, 1.0,1.0,1.0,1.0);
 CGContextFillRect(context,pageRect);

 CGContextSaveGState(context);

    // ***********
 // Next 3 lines makes the rotations so that the page look in the right direction
    // ***********
 CGContextTranslateCTM(context, 0.0, pageRect.size.height);
 CGContextScaleCTM(context, 1.0, -1.0);
 CGContextConcatCTM(context, CGPDFPageGetDrawingTransform(page, kCGPDFMediaBox, pageRect, 0, true));

 CGContextDrawPDFPage(context, page);
 CGContextRestoreGState(context);

 UIImage *thm = UIGraphicsGetImageFromCurrentImageContext();

 UIGraphicsEndImageContext();
 return thm;

}
Catechu answered 9/1, 2011 at 15:1 Comment(4)
Works great, thanks! Ported to MonoTouch it looks something like this gist.github.com/771759Diastrophism
Hi Felz, I'm using your code to get PDF thumbnails. It seems like CGContextConcatCTM(context, CGPDFPageGetDrawingTransform(page, kCGPDFMediaBox, pageRect, 0, true)) line produces a memory bottleneck, I found out that if I leave this line out, then it's OK. But I can't leave it out because I need this for a proper thumbnail. Do you have an idea about how can we improve this? Thx btw...Isolating
Just one question: CGPDFPageRef page = [self getPage:page_number]; throws out an error. Which kind of viewController is the code embedded in?Fugacity
You are right! Replaced the line. You will still need to get ahold of a CGPDFDocumentRef object. Cheers!Catechu

© 2022 - 2024 — McMap. All rights reserved.