Html Utilities : HTML « Network « C# / C Sharp






Html Utilities

 
/****************** Copyright Notice *****************
 
This code is licensed under Microsoft Public License (Ms-PL). 
You are free to use, modify and distribute any portion of this code. 
The only requirement to do that, you need to keep the developer name, as provided below to recognize and encourage original work:


   
Architecture Designed and Implemented By:
Mohammad Ashraful Alam
Microsoft Most Valuable Professional, ASP.NET 2007 ? 2011
Twitter: http://twitter.com/AshrafulAlam | Blog: http://blog.ashraful.net | Portfolio: http://www.ashraful.net
   
*******************************************************/

namespace Eisk.Helpers
{
    using System;
    /// <summary>
  /// This class is used to provide pre-defined html formats and texts that are required to implement on simple mail and print functionalities.
  /// </summary>
  public class HtmlUtilities
  {
    /// <summary>
    /// Initializes a new instance of the <see cref="HTMLUtils"/> class.
    /// </summary>
        public HtmlUtilities()
    {
      sb = new System.Text.StringBuilder();
    }
    
    #region Data
    
    System.Text.StringBuilder sb;
    
    const string tableStartText = "<Table cellSpacing=0 cellPadding=3 width=100% border=0>";
    const string tableEndText = "</Table>";
    
    /// <summary>
    /// Gets the formatted HTML text.
    /// </summary>
    /// <value>The formatted HTML text.</value>
    public string FormattedHtmlText
    {
      get
      {
        sb.Insert(0, tableStartText);
        sb.Append(tableEndText);
        return sb.ToString();
      }
    }
    
    #endregion
        
    #region Predefined Text Size Area

    /// <summary>
    /// Formats to a html row with the text with 4 size text
    /// </summary>
    /// <param name="text">The text.</param>
    public void AppendHeader (string text)
    {
      FormatToRow(text, 4, true); 
    }
    
    /// <summary>
    /// Formats to a html row with the text with 3 size text
    /// </summary>
    /// <param name="text">The text.</param>
    public void AppendSubHeader (string text)
    {
      FormatToRow(text, 3, true); 
    }
    
    /// <summary>
    /// Formats to a html row with the text with 2 size text
    /// </summary>
    /// <param name="text">The text.</param>
    public void AppendPara (string text)
    {
      FormatToRow(text, 2, false);         
    }
    
    /// <summary>
    /// Appends the blank row.
    /// </summary>
    public void AppendBlankRow ()
    {
      FormatToBlankRow();
    }
    
    #endregion
    
    #region Format Area

    /// <summary>
    /// Formats a blank HTML row.
    /// </summary>
    public void FormatToBlankRow()
    {
      sb.Append("<TR> <TD height=20> </TD> </TR>");    
    }

    /// <summary>
    /// Formats to row.
    /// </summary>
    /// <param name="text">The text.</param>
    public void FormatToRow(string text)
    {
      FormatToRow(text, 2, false);
    }

    /// <summary>
    /// Formats to row.
    /// </summary>
    /// <param name="text">The text.</param>
    /// <param name="fontSize">Size of the font.</param>
    public void FormatToRow(string text, int fontSize)
    {
      FormatToRow(text, fontSize, false);
    }

    /// <summary>
    /// Formats to row.
    /// </summary>
    /// <param name="text">The text.</param>
    /// <param name="fontSize">Size of the font.</param>
    /// <param name="bold">if set to <c>true</c> [bold].</param>
    public void FormatToRow(string text, int fontSize, bool bold)
    {
      string boldMarker = ( bold? "bold": "normal");
      string fontFace = "verdana";
      sb.Append("<TR> <TD align=left><font size=" + fontSize.ToString(System.Globalization.CultureInfo.CurrentCulture.NumberFormat) + " face=" + fontFace + " style=\"FONT-WEIGHT: " + boldMarker + "\"  > " + text + "</font></TD> </TR>");
    }
    
    /// <summary>
    /// Formats the provided key/values by separating them thru a colon, and considers the smallest text size '2'.
    /// </summary>
    /// <param name="strLabel"></param>
    /// <param name="strValue"></param>
    public void FormatToRowInColonSeparatedText(string label, string value)
    {
      FormatToRowInColonSeparatedText ( label, value, false);  
    }

    /// <summary>
    /// Formats the provided key/values by separating them thru a colon, and considers the smallest text size '2'.
    /// </summary>
    /// <param name="strLabel"></param>
    /// <param name="strValue"></param>
    /// <param name="bold"></param>
    public void FormatToRowInColonSeparatedText(string label, string value, bool bold)
    {
      label = "<b>" + label + " : </b>";
      if(bold) value = "<b>" + value + "</b>";
      FormatToRow(label +  " " + value, 2, bold);
    }
    
    #endregion    
    
  }
}

   
  








Related examples in the same category

1.Get Links From HTML
2.Parses the value information from any INPUT tag in an HTML string where the name="" attribute matched the tagID parameter
3.Convert HTML To Text
4.Converts a FontUnit to a size for the HTML FONT tag
5.Strip HTML
6.Remove tags from a html string
7.Sanitize any potentially dangerous tags from the provided raw HTML input using a whitelist based approach
8.Get Type As Html
9.HTML-encodes a string and returns the encoded string.
10.Strips all HTML tags from the specified string.
11.Removes the HTML whitespace.
12.Array To Html Breaked String
13.Show Html Page in String with Process