Search code examples
ipadpdfuibuilder

ipad - HelloWorld How To View a PDF


I need to write a simple app which will view a PDF stored locally on the phone as part of the app bundle.

Is there a control in UIBuilder that I can use to display a PDF?


Solution

  • To view a PDF as a UIImage, where the source of the PDF is a Base64 string, use this simple class. (MonoTouch)

    using System;
    using MonoTouch.UIKit;
    using MonoTouch.Foundation;
    using System.Drawing;
    using MonoTouch.CoreGraphics;
    using System.IO;
    namespace Common
    {
        public class PDF
        {
    
            public static readonly string MyDocuments = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
    
            public static string SavePDFReturnPath (string Base64PDFString, string FileName)
            {
    
                string path = Path.Combine (MyDocuments, "pdfs");
                if (!Directory.Exists (path))
                    Directory.CreateDirectory (path);
    
                FileName = FileName.ToLower ();
                if (!FileName.EndsWith ("pdf"))
                    FileName = FileName + ".pdf";
    
                path = Path.Combine (path, FileName);
                if (File.Exists (path))
                    File.Delete (path);
    
                using (var f = System.IO.File.Create (path))
                {
                    byte[] encodedDataAsBytes = System.Convert.FromBase64String (Base64PDFString);
                    f.Write (encodedDataAsBytes, 0, encodedDataAsBytes.Length);
                }
    
                return path;
            }
    
            public static UIImage GetImageFromPDFThatIsInBase64 (string Base64PDFString)
            {
                RectangleF PDFRectangle = new RectangleF (0, 0, UIScreen.MainScreen.Bounds.Width, UIScreen.MainScreen.Bounds.Height);
                return GetImageFromPDFThatIsInBase64 (Base64PDFString, PDFRectangle);
            }
    
    
            public static UIImage GetImageFromPDFThatIsInBase64 (string Base64PDFString, RectangleF PDFRectangle)
            {
                UIImage img = null;
    
                try
                {
                    UIGraphics.BeginImageContext (new SizeF (PDFRectangle.Width, PDFRectangle.Height));
                    CGContext context = UIGraphics.GetCurrentContext ();
                    context.SaveState ();
    
                    CGPDFDocument pdfDoc = CGPDFDocument.FromFile (SavePDFReturnPath (Base64PDFString, "temp.pdf"));
                    CGPDFPage pdfPage = pdfDoc.GetPage (1);
    
                    img = TransformPDFToImage (pdfPage, PDFRectangle.Width);
    
                    pdfDoc.Dispose ();
                    context.RestoreState ();
    
                    UIGraphics.EndImageContext ();
                }
                catch (Exception e)
                {
                    Console.WriteLine ("GetImageFromBase64PDFImage: {0}", e.Message);
                }
                return img;
    
            }
    
            private static UIImage TransformPDFToImage (CGPDFPage page, Single width)
            {
                RectangleF pageRect = page.GetBoxRect (CGPDFBox.Media);
    
                Single pdfScale = width / pageRect.Size.Width;
                pageRect.Size = new SizeF (pageRect.Size.Width * pdfScale, pageRect.Size.Height * pdfScale);
                //pageRect.Origin = CGPointZero;
    
                UIGraphics.BeginImageContext (pageRect.Size);
                CGContext context = UIGraphics.GetCurrentContext ();
    
                //White BG
                context.SetRGBFillColor (1.0f, 1.0f, 1.0f, 1.0f);
                context.FillRect (pageRect);
                context.SaveState ();
    
                // 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 ();
    
                UIImage img = UIGraphics.GetImageFromCurrentImageContext ();
                UIGraphics.EndImageContext ();
    
                return img;
            }
        }
    }