Providing CGPDFPage in UIImage

I am trying to display a CGPDFPage (selected from CGPDFDocument) in a UIImage for display in a view.

I have the following code in MonoTouch that gives me part of the way.

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); } 

The CGPDFPage section is displayed, but rotated back and forth. My question is: how do I select the full pdf page and turn it around to display it correctly. I saw several examples of using ScaleCTM and TranslateCTM, but could not get them to work.

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

thanks

+5
ios iphone uiimage
source share
1 answer

I did not work with MonoTouch. However, in objective-C you will get an image for the PDF page similar to this (note CTM transformations):

 -(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; } 
+14
source share

All Articles