/// <summary> /// Gets the view size for the opened PDF page. /// </summary> /// <param name="pageNumber">The page number,</param> /// <returns>The page view rect.</returns> public static RectangleF GetPageViewSize(int pageNumber) { RectangleF pageRect = RectangleF.Empty; if (PDFDocument.DocumentHasLoaded) { // Check the lower page bounds if (pageNumber < 1) { pageNumber = 1; } // Check the upper page bounds if (pageNumber > PDFDocument.PageCount) { pageNumber = PDFDocument.PageCount; } using (CGPDFPage pdfPage = PDFDocument.GetPage(pageNumber)) { if (pdfPage != null) { // Calc effective rect RectangleF cropBoxRect = pdfPage.GetBoxRect(CGPDFBox.Crop); RectangleF mediaBoxRect = pdfPage.GetBoxRect(CGPDFBox.Media); RectangleF effectiveRect = RectangleF.Intersect(cropBoxRect, mediaBoxRect); // Calc page width and height, accoring by rotation switch (pdfPage.RotationAngle) { default: case 0: case 180: pageRect.Width = effectiveRect.Size.Width; pageRect.Height = effectiveRect.Size.Height; break; case 90: case 270: pageRect.Height = effectiveRect.Size.Width; pageRect.Width = effectiveRect.Size.Height; break; } if (pageRect.Width % 2 > 0) { pageRect.Width--; } if (pageRect.Height % 2 > 0) { pageRect.Height--; } } } } return(pageRect); }
public static CGRect GetPageViewSize(nint pageNumber) { CGRect pageRect = CGRect.Empty; if (PDFDocument.DocumentHasLoaded) { if (pageNumber < 1) { pageNumber = 1; } if (pageNumber > PDFDocument.PageCount) { pageNumber = PDFDocument.PageCount; } using (CGPDFPage pdfPage = PDFDocument.GetPage(pageNumber)) { if (pdfPage != null) { CGRect cropBoxRect = pdfPage.GetBoxRect(CGPDFBox.Crop); CGRect mediaBoxRect = pdfPage.GetBoxRect(CGPDFBox.Media); CGRect effectiveRect = CGRect.Intersect(cropBoxRect, mediaBoxRect); switch (pdfPage.RotationAngle) { default: case 0: case 180: pageRect.Width = effectiveRect.Size.Width; pageRect.Height = effectiveRect.Size.Height; break; case 90: case 270: pageRect.Height = effectiveRect.Size.Width; pageRect.Width = effectiveRect.Size.Height; break; } if (pageRect.Width % 2 > 0) { pageRect.Width--; } if (pageRect.Height % 2 > 0) { pageRect.Height--; } } } } return(pageRect); }
private static UIImage RenderImage(CGPDFPage page) { var rect = page.GetBoxRect(CGPDFBox.Crop); var pageRotation = page.RotationAngle; var size = rect.Size; UIGraphics.BeginImageContextWithOptions(size, false, (nfloat)(1)); var context = UIGraphics.GetCurrentContext(); context.SaveState(); context.TranslateCTM((nfloat)0.0, size.Height); context.ScaleCTM(One, (nfloat)(-1.0)); context.SetFillColor(One, One); context.FillRect(rect); var transform = page.GetDrawingTransform(CGPDFBox.Crop, rect, 0, true); context.ConcatCTM(transform); context.DrawPDFPage(page); var image = UIGraphics.GetImageFromCurrentImageContext(); context.RestoreState(); UIGraphics.EndImageContext(); return(image); }
static UIImage imageWithPDFPage(CGPDFPage page, float scale, CGAffineTransform t) { if (page == null) { return(null); } RectangleF box = page.GetBoxRect(CGPDFBox.Crop); t.Scale(scale, scale); box = new RectangleF(box.Location, new SizeF(box.Size.Width * scale, box.Size.Height * scale)); var pixelWidth = box.Size.Width; CGColorSpace cs = CGColorSpace.CreateDeviceRGB(); //DebugAssert( cs ) ; var _buffer = Marshal.AllocHGlobal((int)(box.Width * box.Height)); UIGraphics.BeginImageContext(box.Size); CGContext c = UIGraphics.GetCurrentContext(); c.SaveState(); c.TranslateCTM(0f, box.Height); c.ScaleCTM(1f, -1f); cs.Dispose(); c.ConcatCTM(t); c.DrawPDFPage(page); c.RestoreState(); var image = UIGraphics.GetImageFromCurrentImageContext(); return(image); }
public PdfViewController (NSUrl url) : base() { Url = url; View = new UIView (); View.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleTopMargin | UIViewAutoresizing.FlexibleBottomMargin | UIViewAutoresizing.FlexibleLeftMargin | UIViewAutoresizing.FlexibleRightMargin; View.AutosizesSubviews = true; PdfDocument = CGPDFDocument.FromUrl (Url.ToString ()); // For demo purposes, show first page only. PdfPage = PdfDocument.GetPage (1); PdfPageRect = PdfPage.GetBoxRect (CGPDFBox.Crop); // Setup tiled layer. TiledLayer = new CATiledLayer (); TiledLayer.Delegate = new TiledLayerDelegate (this); TiledLayer.TileSize = new SizeF (1024f, 1024f); TiledLayer.LevelsOfDetail = 5; TiledLayer.LevelsOfDetailBias = 5; TiledLayer.Frame = PdfPageRect; ContentView = new UIView (PdfPageRect); ContentView.Layer.AddSublayer (TiledLayer); // Prepare scroll view. ScrollView = new UIScrollView (View.Frame); ScrollView.AutoresizingMask = View.AutoresizingMask; ScrollView.Delegate = new ScrollViewDelegate (this); ScrollView.ContentSize = PdfPageRect.Size; ScrollView.MaximumZoomScale = 10f; ScrollView.MinimumZoomScale = 1f; ScrollView.ScrollEnabled = true; ScrollView.AddSubview (ContentView); View.AddSubview (ScrollView); }
static UIImage imageWithPDFPage(CGPDFPage page, float scale ,CGAffineTransform t ) { if (page == null) { return null ; } RectangleF box = page.GetBoxRect(CGPDFBox.Crop); t.Scale(scale,scale); box = new RectangleF(box.Location,new SizeF(box.Size.Width * scale, box.Size.Height * scale)); var pixelWidth = box.Size.Width ; CGColorSpace cs = CGColorSpace.CreateDeviceRGB() ; //DebugAssert( cs ) ; var _buffer = Marshal.AllocHGlobal((int)(box.Width * box.Height)); UIGraphics.BeginImageContext(box.Size); CGContext c = UIGraphics.GetCurrentContext(); cs.Dispose(); c.ConcatCTM(t); c.DrawPDFPage(page); var image = UIGraphics.GetImageFromCurrentImageContext(); return image ; }
public void SetPDFPage (CGPDFPage pdfPage) { page = pdfPage; if (page == null) { pageRect = Bounds; } else { pageRect = page.GetBoxRect (CGPDFBox.Media); PdfScale = Frame.Size.Width / pageRect.Size.Width; pageRect = new CGRect (pageRect.X, pageRect.Y, pageRect.Width * PdfScale, pageRect.Height * PdfScale); } ReplaceTiledPDFView (pageRect); }
void RestoreScale() { CGRect pageRect = page.GetBoxRect(CGPDFBox.Media); nfloat yScale = View.Frame.Height / pageRect.Height; nfloat xScale = View.Frame.Width / pageRect.Width; myScale = NMath.Min(xScale, yScale); ScrollView.Bounds = View.Bounds; ScrollView.ZoomScale = 1f; ScrollView.PdfScale = myScale; ScrollView.TiledPDFView.Bounds = View.Bounds; ScrollView.TiledPDFView.MyScale = myScale; ScrollView.TiledPDFView.Layer.SetNeedsDisplay(); }
public void SetPDFPage(CGPDFPage pdfPage) { page = pdfPage; if (page == null) { pageRect = Bounds; } else { pageRect = page.GetBoxRect(CGPDFBox.Media); PdfScale = Frame.Size.Width / pageRect.Size.Width; pageRect = new CGRect(pageRect.X, pageRect.Y, pageRect.Width * PdfScale, pageRect.Height * PdfScale); } ReplaceTiledPDFView(pageRect); }
private void RestoreScale() { // Called on orientation change. // We need to zoom out and basically reset the scrollview to look right in two-page spline view. var pageRect = page.GetBoxRect(CGPDFBox.Media); var yScale = View.Frame.Height / pageRect.Height; var xScale = View.Frame.Width / pageRect.Width; myScale = NMath.Min(xScale, yScale); ScrollView.Bounds = View.Bounds; ScrollView.ZoomScale = 1f; ScrollView.PdfScale = myScale; ScrollView.TiledPdfView.Bounds = View.Bounds; ScrollView.TiledPdfView.MyScale = myScale; ScrollView.TiledPdfView.Layer.SetNeedsDisplay(); }
public static UIImage PDF2Image(CGPDFPage page, nfloat width, nfloat scale) { UIImage img = new UIImage(); try { CGRect pageRect = page.GetBoxRect(CGPDFBox.Media); nfloat pdfScale = width / pageRect.Size.Width; pageRect.Size = new CGSize(pageRect.Size.Width * pdfScale, pageRect.Size.Height * pdfScale); UIGraphics.BeginImageContextWithOptions(pageRect.Size, true, scale); CGContext context = UIGraphics.GetCurrentContext(); // White BG context.SetFillColor(1.0f, 1.0f, 1.0f, 1f); context.FillRect(pageRect); context.SaveState(); //border context.SetStrokeColor(0f, 0f, 0f, 0.5f); context.StrokeRect(pageRect); // Next 3 lines makes the rotations so that the page look in the right direction context.TranslateCTM(0.0f, pageRect.Size.Height); context.ScaleCTM(1.0f, -1.0f); CGAffineTransform transform = page.GetDrawingTransform(CGPDFBox.Media, pageRect, 0, true); context.ConcatCTM(transform); context.DrawPDFPage(page); context.RestoreState(); img = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); context.Dispose(); } catch (Exception ex) { } return(img); }
public void SetPDFPage(CGPDFPage pdfPage) { page = pdfPage; // PDFPage is null if we're requested to draw a padded blank page by the parent UIPageViewController if (page == null) { pageRect = Bounds; } else { pageRect = page.GetBoxRect(CGPDFBox.Media); PdfScale = Frame.Size.Width / pageRect.Size.Width; pageRect = new CGRect(pageRect.X, pageRect.Y, pageRect.Width * PdfScale, pageRect.Height * PdfScale); } // Create the TiledPDFView based on the size of the PDF page and scale it to fit the view. ReplaceTiledPdfViewWithFrame(pageRect); }
public UIImage GetPDFImageForPage() { CGPDFPage pdfPg = _pdf.GetPage(PageNumber); nfloat scale; PDFpageRect = pdfPg.GetBoxRect(CGPDFBox.Media); if (PDFpageRect.Height > PDFpageRect.Width) { scale = (this.View.Frame.Width - 80.0f) / PDFpageRect.Width; } else { scale = this.View.Frame.Height / PDFpageRect.Height; } PDFpageRect.Size = new CGSize(PDFpageRect.Width * scale, PDFpageRect.Height * scale); UIGraphics.BeginImageContext(PDFpageRect.Size); CGContext context = UIGraphics.GetCurrentContext(); context.SetFillColor((nfloat)1.0, (nfloat)1.0, (nfloat)1.0, (nfloat)1.0); context.FillRect(PDFpageRect); context.SaveState(); context.TranslateCTM(0, PDFpageRect.Size.Height); context.ScaleCTM(1, -1); context.ConcatCTM(CGAffineTransform.MakeScale(scale, scale)); context.DrawPDFPage(pdfPg); context.RestoreState(); UIImage thm = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); return(thm); }
public static UIImage FromPdf(string name, float width, float height) { CGPDFDocument doc = CreatePDFDocumentWithName(name); if (doc == null) { return(null); } // PDF pages are numbered starting at page 1 CGPDFPage page = doc.GetPage(1); RectangleF box = page.GetBoxRect(CGPDFBox.Crop); var xScale = (width * UIScreen.MainScreen.Scale) / box.Width; var yScale = (height * UIScreen.MainScreen.Scale) / box.Height; var result = imageWithPDFPage(page, Math.Max(xScale, yScale), CGAffineTransform.MakeIdentity()); return(result); }
internal void ConvertPagetoImage(int index) { if (this.PdfDocument == null || this.Element.ImageStreams.ContainsKey(index)) { return; } float scaleFactor = 1; UIImage image = null; using (CGPDFPage pdfPage = this.PdfDocument.GetPage(index + 1)) { if (pdfPage != null) { CGRect rect = pdfPage.GetBoxRect(CGPDFBox.Media); nfloat factor = (nfloat)scaleFactor; CGRect bounds = new CGRect(rect.X * factor, rect.Y * factor, rect.Width * factor, rect.Height * factor); UIGraphics.BeginImageContext(bounds.Size); CGContext context = UIGraphics.GetCurrentContext(); context.SetFillColor(1.0f, 1.0f, 1.0f, 1.0f); context.FillRect(bounds); context.TranslateCTM(0, bounds.Height); context.ScaleCTM(factor, -factor); context.ConcatCTM(pdfPage.GetDrawingTransform(CGPDFBox.Crop, rect, 0, true)); context.SetRenderingIntent(CGColorRenderingIntent.Default); context.InterpolationQuality = CGInterpolationQuality.Default; context.DrawPDFPage(pdfPage); image = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); } } if (image != null) { this.Element.ImageStreams.Add(index, image); this.Element.PDFViewModel.Items[index].PageData = image; } }
public PdfScrollView (CGRect frame) : base (frame) { ShowsVerticalScrollIndicator = false; ShowsHorizontalScrollIndicator = false; BouncesZoom = true; DecelerationRate = UIScrollView.DecelerationRateFast; BackgroundColor = UIColor.Gray; MaximumZoomScale = 5.0f; MinimumZoomScale = 0.25f; // open the PDF file (default directory is the bundle path) pdf = CGPDFDocument.FromFile ("Tamarin.pdf"); // select the first page (the only one we'll use) page = pdf.GetPage (1); // make the initial view 'fit to width' CGRect pageRect = page.GetBoxRect (CGPDFBox.Media); scale = Frame.Width / pageRect.Width; pageRect.Size = new CGSize (pageRect.Width * scale, pageRect.Height * scale); // create bitmap version of the PDF page, to be used (scaled) // when no other (tiled) view are visible UIGraphics.BeginImageContext (pageRect.Size); CGContext context = UIGraphics.GetCurrentContext (); // fill with white background context.SetFillColor (1.0f, 1.0f, 1.0f, 1.0f); context.FillRect (pageRect); context.SaveState (); // flip page so we render it as it's meant to be read context.TranslateCTM (0.0f, pageRect.Height); context.ScaleCTM (1.0f, -1.0f); // scale page at the view-zoom level context.ScaleCTM (scale, scale); context.DrawPDFPage (page); context.RestoreState (); UIImage backgroundImage = UIGraphics.GetImageFromCurrentImageContext (); UIGraphics.EndImageContext (); backgroundImageView = new UIImageView (backgroundImage); backgroundImageView.Frame = pageRect; backgroundImageView.ContentMode = UIViewContentMode.ScaleAspectFit; AddSubview (backgroundImageView); SendSubviewToBack (backgroundImageView); // Create the TiledPDFView based on the size of the PDF page and scale it to fit the view. pdfView = new TiledPdfView (pageRect, (float)scale); pdfView.Page = page; AddSubview (pdfView); // no need to have (or set) a UIScrollViewDelegate with MonoTouch this.ViewForZoomingInScrollView = delegate { // return the view we'll be using while zooming return pdfView; }; // when zooming starts we remove (from view) and dispose any // oldPdfView and set pdfView as our 'new' oldPdfView, it will // stay there until a new view is available (when zooming ends) this.ZoomingStarted += delegate { if (oldPdfView != null) { oldPdfView.RemoveFromSuperview (); oldPdfView.Dispose (); } oldPdfView = pdfView; AddSubview (oldPdfView); }; // when zooming ends a new TiledPdfView is created (and shown) // based on the updated 'scale' and 'frame' ZoomingEnded += delegate (object sender, ZoomingEndedEventArgs e) { scale *= e.AtScale; CGRect rect = page.GetBoxRect (CGPDFBox.Media); rect.Size = new CGSize (rect.Width * scale, rect.Height * scale); pdfView = new TiledPdfView (rect, (float)scale); pdfView.Page = page; AddSubview (pdfView); }; }
private static UIImage RenderImage (CGPDFPage page) { var rect = page.GetBoxRect (CGPDFBox.Crop); var pageRotation = page.RotationAngle; var size = rect.Size; UIGraphics.BeginImageContextWithOptions (size, false, (nfloat)(1)); var context = UIGraphics.GetCurrentContext (); context.SaveState (); context.TranslateCTM ((nfloat)0.0, size.Height); context.ScaleCTM (One, (nfloat)(-1.0)); context.SetFillColor (One, One); context.FillRect (rect); var transform = page.GetDrawingTransform (CGPDFBox.Crop, rect, 0, true); context.ConcatCTM (transform); context.DrawPDFPage (page); var image = UIGraphics.GetImageFromCurrentImageContext (); context.RestoreState (); UIGraphics.EndImageContext (); return image; }
bool GenerateImage(Dictionary <Page, eBriefingMobile.Annotation> dict1, Dictionary <Page, List <eBriefingMobile.Note> > dict2) { try { if (pageList != null) { nuint totalImageSize = 0; List <Note> notes = new List <eBriefingMobile.Note> (); foreach (var page in pageList) { String localPath = DownloadedFilesCache.BuildCachedFilePath(page.URL); var printItemDict = new Dictionary <UIImage, List <Note> >(); if (!String.IsNullOrEmpty(localPath)) { CGPDFDocument pdfDoc = CGPDFDocument.FromFile(localPath); if (pdfDoc != null) { CGPDFPage pdfPage = pdfDoc.GetPage(1); if (pdfPage != null) { CGRect pageRect = pdfPage.GetBoxRect(CGPDFBox.Media); UIImage pdfImg = ImageHelper.PDF2Image(pdfPage, pageRect.Width, scale); // Add annotation if option selected if (dict1.ContainsKey(page)) { Annotation annotation = dict1 [page]; if (annotation != null) { Dictionary <String, PSPDFInkAnnotation> coordinateDict = AnnotationsDataAccessor.GenerateAnnDictionary((UInt32)page.PageNumber - 1, annotation); if (coordinateDict != null) { foreach (KeyValuePair <String, PSPDFInkAnnotation> item in coordinateDict) { // Create full size annotation UIImage annImg = ImageHelper.DrawPSPDFAnnotation(item.Key, item.Value); if (annImg != null) { // Scale down the annotation image annImg = annImg.Scale(new CGSize(pdfImg.Size.Width, pdfImg.Size.Height)); // Overlap pdfImg and annImg pdfImg = ImageHelper.Overlap(pdfImg, annImg, CGPoint.Empty, CGPoint.Empty, scale); } } } } } // Create image from text bool printNote = false; UIImage noteImg = null; if (dict2.ContainsKey(page) && dict2 [page] != null) { printNote = true; notes = dict2 [page]; // Create image from text //noteImg = ImageHelper.Text2Image(_notesText, pdfImg.Size); } else { notes = null; } // Scale down and add to canvas // Used 900 and 1200 because couldn't control the paper margin // if (Orientation == ORIENTATION.PORTRAIT) // { // //if (printNote) // { // //pdfImg = ImageHelper.Scale(pdfImg, 500); // //pdfImg=ImageHelper.MaxResizeImage(pdfImg,1000,scale); // //pdfImg = ImageHelper.Add2Canvas(pdfImg, new CGPoint(0, (1024 / 2) - (pdfImg.Size.Height / 2)), scale); // // // Overlap pdfImg and noteImg // //pdfImg = ImageHelper.Overlap(pdfImg, noteImg, CGPoint.Empty, new CGPoint(500, 0), scale); // } // //else // { // //pdfImg=ImageHelper.MaxResizeImage(pdfImg,1000,scale); // //pdfImg = ImageHelper.Scale(pdfImg, 900); // //pdfImg = ImageHelper.Add2Canvas(pdfImg, new CGPoint((768 / 2) - (pdfImg.Size.Width / 2), (1024 / 2) - (pdfImg.Size.Height / 2)), scale); // } // } // else // { // //if (printNote) // { // //pdfImg=ImageHelper.MaxResizeImage(pdfImg,500,scale); // //pdfImg = ImageHelper.Scale(pdfImg, 500); // // pdfImg = ImageHelper.Add2Canvas(pdfImg, new CGPoint(0,0), scale*2, UIInterfaceOrientation.LandscapeLeft); // // Overlap pdfImg and noteImg // //pdfImg = ImageHelper.Overlap(pdfImg, noteImg, CGPoint.Empty, new CGPoint(756, 0), scale); // } // //else // { // //pdfImg=ImageHelper.MaxResizeImage(pdfImg,1000,scale); // //pdfImg = ImageHelper.Scale(pdfImg, 500); // ///pdfImg = ImageHelper.Add2Canvas(pdfImg, new CGPoint((1024 / 2) - (pdfImg.Size.Width / 2), (768 / 2) - (pdfImg.Size.Height / 2)), scale*2, UIInterfaceOrientation.LandscapeLeft); // } // // // Rotate canvas // //pdfImg = ImageHelper.Rotate(pdfImg); // } // Save // if (printItems == null) // { // printItems = new List<UIImage>(); // } // printItems.Add(pdfImg); if (dict == null) { dict = new Dictionary <int, Dictionary <UIImage, List <Note> > > (); } if (pdfImg != null) { printItemDict.Add(pdfImg, notes); dict.Add(page.PageNumber, printItemDict); var pngImage = pdfImg.AsPNG(); totalImageSize = pngImage.Length + totalImageSize; Console.WriteLine("Img : " + totalImageSize.ToString()); //image dispose pdfImg = null; if (CheckReachMemoryLimit(totalImageSize)) { PagesNum = dict.Count - 1; dict.Clear(); dict = null; return(false); } } } } } } PagesNum = dict.Count; return(true); } return(false); } catch (Exception ex) { return(false); } }
private List <UIImage> ConvertToImages( ) { Images = new List <UIImage>(); if (Doc == null) { throw new Exception("Could not load document"); } for (Int32 i = 1; i <= Count; i++) { using (CGPDFPage Page = Doc.GetPage(i)) { CGRect PageRect = Page.GetBoxRect(CGPDFBox.Media); //nfloat Scale = 1;//View.Frame.Height / PageRect.Height; //PageRect.Size = new CGSize(PageRect.Height * Scale, PageRect.Width * Scale); if (PageRect.Height > PageRect.Width) { PageRect.Size = new CGSize(PageRect.Height, PageRect.Width); } CGRect MediaBox = Page.GetBoxRect(CGPDFBox.Media); CGRect CropBox = Page.GetBoxRect(CGPDFBox.Crop); nfloat TopMargin = CropBox.GetMinY() - MediaBox.GetMinY(); nfloat BottomMargin = MediaBox.GetMaxY() - CropBox.GetMaxY(); nfloat LeftMargin = CropBox.GetMinX() - MediaBox.GetMinX(); nfloat RightMargin = MediaBox.GetMaxX() - CropBox.GetMaxX(); if (TopMargin + BottomMargin + LeftMargin + RightMargin > 0) { PageRect = new CGRect( PageRect.Location, new CGSize( PageRect.Size.Width - (LeftMargin + RightMargin), PageRect.Size.Height - (TopMargin + BottomMargin) ) ); } UIGraphics.BeginImageContext(PageRect.Size); using (CGContext context = UIGraphics.GetCurrentContext()) { context.SaveState(); context.TranslateCTM(0, PageRect.Size.Height); context.ScaleCTM(1, -1); context.ConcatCTM( Page.GetDrawingTransform(CGPDFBox.Crop, PageRect, 0, true) ); context.DrawPDFPage(Page); context.RestoreState(); Images.Add(UIGraphics.GetImageFromCurrentImageContext()); } UIGraphics.EndImageContext(); } } return(Images); }
public PdfScrollView(CGRect frame) : base(frame) { ShowsVerticalScrollIndicator = false; ShowsHorizontalScrollIndicator = false; BouncesZoom = true; DecelerationRate = UIScrollView.DecelerationRateFast; BackgroundColor = UIColor.White; MaximumZoomScale = 5.0f; MinimumZoomScale = 0.25f; // open the PDF file (default directory is the bundle path) pdf = CGPDFDocument.FromFile("Tamarin.pdf"); // select the first page (the only one we'll use) page = pdf.GetPage(1); // make the initial view 'fit to width' CGRect pageRect = page.GetBoxRect(CGPDFBox.Media); scale = Frame.Width / pageRect.Width; pageRect.Size = new CGSize(pageRect.Width * scale, pageRect.Height * scale); // create bitmap version of the PDF page, to be used (scaled) // when no other (tiled) view are visible UIGraphics.BeginImageContext(pageRect.Size); CGContext context = UIGraphics.GetCurrentContext(); // fill with white background context.SetFillColor(1.0f, 1.0f, 1.0f, 1.0f); context.FillRect(pageRect); context.SaveState(); // flip page so we render it as it's meant to be read context.TranslateCTM(0.0f, pageRect.Height); context.ScaleCTM(1.0f, -1.0f); // scale page at the view-zoom level context.ScaleCTM(scale, scale); context.DrawPDFPage(page); context.RestoreState(); UIImage backgroundImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); backgroundImageView = new UIImageView(backgroundImage); backgroundImageView.Frame = pageRect; backgroundImageView.ContentMode = UIViewContentMode.ScaleAspectFit; AddSubview(backgroundImageView); SendSubviewToBack(backgroundImageView); // Create the TiledPDFView based on the size of the PDF page and scale it to fit the view. pdfView = new TiledPdfView(pageRect, (float)scale); pdfView.Page = page; AddSubview(pdfView); // no need to have (or set) a UIScrollViewDelegate with MonoTouch this.ViewForZoomingInScrollView = delegate { // return the view we'll be using while zooming return(pdfView); }; // when zooming starts we remove (from view) and dispose any // oldPdfView and set pdfView as our 'new' oldPdfView, it will // stay there until a new view is available (when zooming ends) this.ZoomingStarted += delegate { if (oldPdfView != null) { oldPdfView.RemoveFromSuperview(); oldPdfView.Dispose(); } oldPdfView = pdfView; AddSubview(oldPdfView); }; // when zooming ends a new TiledPdfView is created (and shown) // based on the updated 'scale' and 'frame' ZoomingEnded += delegate(object sender, ZoomingEndedEventArgs e) { scale *= e.AtScale; CGRect rect = page.GetBoxRect(CGPDFBox.Media); rect.Size = new CGSize(rect.Width * scale, rect.Height * scale); pdfView = new TiledPdfView(rect, (float)scale); pdfView.Page = page; AddSubview(pdfView); }; }