Advertisement
csharpbrasil

PdfEngine

Oct 21st, 2014
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.82 KB | None | 0 0
  1. using System;
  2. using System.Web;
  3.  
  4. /// Autor: Raphael A. F. Cardoso - www.csharpbrasil.com.br
  5. namespace Br.CsharpBrasil
  6. {
  7.  
  8.     public class PdfEngine
  9.     {
  10.         private iTextSharp.text.Rectangle rectangle = iTextSharp.text.PageSize.A4;
  11.  
  12.         #region Enumeration
  13.  
  14.         public enum Orientation
  15.         {
  16.             Portrait = 0,
  17.             Landscape = 0
  18.         }
  19.  
  20.         public enum PageSize
  21.         {
  22.             A4 = 0,
  23.             LETTER = 1
  24.         }
  25.  
  26.         #endregion
  27.  
  28.         #region Constructor
  29.        
  30.         public PdfEngine()
  31.         {
  32.             this.rectangle = new iTextSharp.text.Rectangle(iTextSharp.text.PageSize.A4);
  33.         }
  34.  
  35.         public PdfEngine(PageSize pageSize)
  36.         {
  37.             var pSize = getPageSize(pageSize);
  38.             this.rectangle = new iTextSharp.text.Rectangle(pSize);
  39.         }
  40.  
  41.         public PdfEngine(Orientation orientation)
  42.         {
  43.             this.rectangle = new iTextSharp.text.Rectangle(iTextSharp.text.PageSize.A4);
  44.             if (orientation == Orientation.Landscape) this.rectangle = this.rectangle.Rotate();
  45.         }
  46.  
  47.         public PdfEngine(Orientation orientation, PageSize pageSize)
  48.         {
  49.             var pSize = getPageSize(pageSize);
  50.             this.rectangle = new iTextSharp.text.Rectangle(pSize);
  51.             if (orientation == Orientation.Landscape) this.rectangle = this.rectangle.Rotate();
  52.         }
  53.  
  54.         #endregion
  55.  
  56.         #region Private Methods
  57.        
  58.         private iTextSharp.text.Rectangle getPageSize(PageSize pageSize)
  59.         {
  60.             var pSize = iTextSharp.text.PageSize.A4;
  61.  
  62.             switch (pageSize)
  63.             {
  64.                 case PageSize.A4:
  65.                     pSize = iTextSharp.text.PageSize.A4;
  66.                     break;
  67.                 case PageSize.LETTER:
  68.                     pSize = iTextSharp.text.PageSize.LETTER;
  69.                     break;
  70.                 default:
  71.                     pSize = iTextSharp.text.PageSize.A4;
  72.                     break;
  73.             }
  74.  
  75.             return pSize;
  76.         }
  77.  
  78.         private string getImage(string input)
  79.         {
  80.             if (input == null) return string.Empty;
  81.  
  82.             string tempInput = input;
  83.             string pattern = @"<img(.|\n)+?>";
  84.             string src = string.Empty;
  85.             HttpContext context = HttpContext.Current;
  86.  
  87.             foreach (System.Text.RegularExpressions.Match m in System.Text.RegularExpressions.Regex.Matches(input, pattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Multiline | System.Text.RegularExpressions.RegexOptions.RightToLeft))
  88.             {
  89.                 if (m.Success)
  90.                 {
  91.                     string tempM = m.Value;
  92.                     string pattern1 = "src=[\'|\"](.+?)[\'|\"]";
  93.                     System.Text.RegularExpressions.Regex reImg = new System.Text.RegularExpressions.Regex(pattern1, System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Multiline);
  94.                     System.Text.RegularExpressions.Match mImg = reImg.Match(m.Value);
  95.  
  96.                     if (mImg.Success)
  97.                     {
  98.                         src = mImg.Value.ToLower().Replace("src=", "").Replace("\"", "");
  99.  
  100.                         if (src.ToLower().Contains("http://") == false)
  101.                         {
  102.                             src = "src=\"" + context.Request.Url.Scheme + "://" + System.IO.Path.Combine(context.Request.Url.Authority, src).Replace("\\", "/") + "\"";
  103.                             try
  104.                             {
  105.                                 tempM = tempM.Remove(mImg.Index, mImg.Length);
  106.                                 tempM = tempM.Insert(mImg.Index, src);
  107.  
  108.                                 tempInput = tempInput.Remove(m.Index, m.Length);
  109.                                 tempInput = tempInput.Insert(m.Index, tempM);
  110.                             }
  111.                             catch (Exception e)
  112.                             {
  113.  
  114.                             }
  115.                         }
  116.                     }
  117.                 }
  118.             }
  119.             return tempInput;
  120.         }
  121.  
  122.         private string getHtmlSourceCode(string htmlPath)
  123.         {
  124.             string htmlText = string.Empty;
  125.  
  126.             if (htmlPath.Contains("http://") || htmlPath.Contains("https://"))
  127.             {
  128.                 HtmlAgilityPack.HtmlWeb web = new HtmlAgilityPack.HtmlWeb();
  129.                 HtmlAgilityPack.HtmlDocument html = web.Load(htmlPath);
  130.                 html.OptionOutputAsXml = true;
  131.                 htmlText = html.DocumentNode.InnerHtml;
  132.             }
  133.             else
  134.             {
  135.                 using (var htmlStream = System.IO.File.OpenText(htmlPath))
  136.                 {
  137.                     htmlText = htmlStream.ReadToEnd();
  138.                     htmlStream.Close();
  139.                 }
  140.             }
  141.  
  142.             return htmlText;
  143.         }
  144.  
  145.         private string getCssSourceCode(string cssPath)
  146.         {
  147.             string cssText = string.Empty;
  148.  
  149.             if (!string.IsNullOrEmpty(cssPath))
  150.             {
  151.                 using (var cssStream = System.IO.File.OpenText(cssPath))
  152.                 {
  153.                     cssText = cssStream.ReadToEnd();
  154.                     cssStream.Close();
  155.                 }
  156.             }
  157.  
  158.             return cssText;
  159.         }
  160.  
  161.         #endregion
  162.  
  163.         #region Public Methods
  164.  
  165.         public void CreatePDF(string htmlFilePath, string pdfFilePath, string cssFilePath)
  166.         {
  167.             byte[] pdf = new byte[0];
  168.  
  169.             if (!string.IsNullOrEmpty(pdfFilePath))
  170.             {
  171.                 pdf = CreatePDF(htmlFilePath, cssFilePath);
  172.  
  173.                 using (var pdfStream = new System.IO.FileStream(pdfFilePath, System.IO.FileMode.OpenOrCreate))
  174.                 {
  175.                     pdfStream.Write(pdf, 0, pdf.Length);
  176.                     pdfStream.Close();
  177.                 }
  178.             }
  179.         }
  180.  
  181.         public byte[] CreatePDF(string htmlFilePath, string cssFilePath)
  182.         {
  183.             if (string.IsNullOrEmpty(htmlFilePath)) return new byte[0];
  184.  
  185.             byte[] pdf = new byte[0];
  186.             string htmlText = getHtmlSourceCode(htmlFilePath);
  187.             string cssText = getCssSourceCode(cssFilePath);
  188.  
  189.             if (!string.IsNullOrEmpty(htmlText))
  190.             {
  191.                 htmlText = getImage(htmlText);
  192.  
  193.                 System.IO.MemoryStream msHtml = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(htmlText));
  194.                 System.IO.MemoryStream msCss = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(cssText));
  195.  
  196.                 using (var pdfStream = new System.IO.MemoryStream())
  197.                 {
  198.                     using (iTextSharp.text.Document doc = new iTextSharp.text.Document(this.rectangle))
  199.                     {
  200.                         using (var pdfWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(doc, pdfStream))
  201.                         {
  202.                             doc.Open();
  203.  
  204.                             if (msCss.Length == 0)
  205.                             {
  206.                                 iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(pdfWriter, doc, msHtml, System.Text.Encoding.UTF8);
  207.                             }
  208.                             else
  209.                             {
  210.                                 iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(pdfWriter, doc, msHtml, msCss, System.Text.Encoding.UTF8);
  211.                             }
  212.  
  213.                             doc.Close();
  214.                             pdfWriter.Close();
  215.                         }
  216.                     }
  217.  
  218.                     pdf = pdfStream.ToArray();
  219.                 }
  220.             }
  221.  
  222.             return pdf;
  223.         }
  224.  
  225.         #endregion
  226.     }
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement