2011-03-11 5 views

답변

-1

PDF 원본을 Base64 문자열로 사용하는 UIImage로 PDF를 보려면이 간단한 클래스를 사용하십시오. (모노 터치)

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