Learn how to create a .NET component for generating HTML files programmatically with text and image support. Includes design, implementation, testing, and documentation.
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation
Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement.Téléchargez la présentation en cliquant sur ce lien.Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.
类设计-Paragraphs • 集合类,Paragraph对象的集合 • 继承CollectionBase • 方法 • public void Add(Paragraph paragraph) • public Paragraph this[int index] • public Paragraph this[string paragraphID] • public int IndexOf(Paragraph paragraph)
类设计-Document • 属性 • public Paragraphs Paragraphs • Public string Title • 方法 • public void Save(string path) • public void Save(stream outStream)
类设计-Text • 继承Paragraph • 属性 • public string Content • public string FontName • public int FontSize • public string Color • 方法 • public Text(string content)
类设计-Image • 继承Paragraph • 属性 • public string Src • public int Width • public int Height • 方法 • public Image(string src) • public Image(string src,int width,int height)
代码实现-Paragraph /// <summary> /// Represents a paragraph in the HTML document. /// </summary> public abstract class Paragraph { /// <summary> /// Initializes a new instance of the <see cref="Paragraph"/> class. /// </summary> public Paragraph() { }
代码实现-Paragraph /// <summary> /// The ID of the paragraph. /// </summary> private string paraID; /// <summary> /// Gets or sets a string that indicates the ID of the paragraph. /// </summary> public string ID { get { return this.paraID; } set { this.paraID = value; } }
代码实现-Paragraphs /// <summary> /// Represents a collection of <see cref="Paragraph"/> objects. /// </summary> public class Paragraphs : CollectionBase { /// <summary> /// Initializes a new instance of the <see cref="Paragraphs"/> class. /// </summary> public Paragraphs() { }
代码实现-Paragraphs /// <summary> /// Adds a new <see cref="Paragraph"/> object into the collection. /// </summary> /// <param name="paragraph">The <see cref="Paragraph"/> object to be added.</param> public void Add(Paragraph paragraph) { this.List.Add(paragraph); }
代码实现-Paragraphs /// <summary> /// Gets or sets a <see cref="Paragraph"/> object from the collection according to Paragraph index. /// </summary> public Paragraph this[int index] { get { if (index >= 0 && index < this.Count) return (Paragraph)this.List[index]; else throw new ArgumentException("Invalid index in Paragraphs indexer: " + index.ToString()); }
代码实现-Paragraphs set { if (index >= 0 && index < this.Count) this.List[index] = value; else throw new ArgumentException("Invalid index in Paragraphs indexer: " + index.ToString()); } }
代码实现-Paragraphs /// <summary> /// Gets or sets a <see cref="Paragraph"/> object from the collection according to Paragraph ID. /// </summary> public Paragraph this[string paragraphID] { get { Paragraph resultParagraph = null; foreach (Paragraph curParagraph in this) { if (String.Compare(curParagraph.ID, paragraphID,false) == 0) { resultParagraph = curParagraph; break; } }
代码实现-Paragraphs if (resultParagraph != null) return resultParagraph; else throw new ArgumentException("Paragraph ID not found in Paragraphs indexer: " + paragraphID); }
代码实现-Paragraphs set { Paragraph resultParagraph = null; foreach (Paragraph curParagraph in this) { if (String.Compare(curParagraph.ID, paragraphID, false) == 0) { resultParagraph = curParagraph; break; } } if (resultParagraph != null) resultParagraph = value; else throw new ArgumentException("Paragraph ID not found in Paragraphs indexer: " + paragraphID); } }
代码实现-Paragraphs /// <summary> /// Gets the index of a specified <see cref="Paragraph"/> object in the collection. /// </summary> /// <param name="paragraph">The specified paragraph.</param> /// <returns>The index value.</returns> public int IndexOf(Paragraph paragraph) { return this.List.IndexOf(paragraph); }
代码实现-Document /// <summary> /// Represents a HTML document. /// </summary> public class Document { /// <summary> /// Initializes a new instance of the <see cref="Document"/> class. /// </summary> public Document() { }
代码实现-Document /// <summary> /// The title of the HTML. /// </summary> private string htmlTitle; /// <summary> /// Gets or sets a string that indicates the Title of the HTML. /// </summary> public string Title { get { return htmlTitle; } set { htmlTitle = value; } }
代码实现-Document /// <summary> /// The paragraphs in the HTML. /// </summary> private Paragraphs parasInHTML; /// <summary> /// Gets or sets a <see cref="Paragraphs"/> object that indicates the paragraphs in the HTML. /// </summary> public Paragraphs Paragraphs { get { return parasInHTML; } set { parasInHTML = value; } }
代码实现-Document /// <summary> /// Save the HTML into file. /// </summary> /// <param name="path">The path of the HTML file.</param> public void Save(string path) { } /// <summary> /// Save the HTML into stream. /// </summary> /// <param name="outStream">The stream that the HTML is saved to.</param> public void Save(Stream outStream) { }
代码实现-Text /// <summary> /// Represents a text in HTML document. /// </summary> public class Text : Paragraph { /// <summary> /// Initializes a new instance of the <see cref="Text"/> class. /// </summary> public Text() { } /// <summary> /// Initializes a new instance of the <see cref="Text"/> class. /// </summary> public Text(string content) { textContent = content; }
代码实现-Text /// <summary> /// The content of the Text. /// </summary> private string textContent; /// <summary> /// Gets or sets a string that indicates the content of the Text. /// </summary> public string Content { get { return textContent; } set { textContent = value; } }
代码实现-Text /// <summary> /// The font name of the Text. /// </summary> private string textFontName; /// <summary> /// Gets or sets a string that indicates the font name of the Text. /// </summary> public string FontName { get { return textFontName; } set { textFontName = value; } }
代码实现-Text /// <summary> /// The font size of the Text. /// </summary> private int textFontSize= 0; /// <summary> /// Gets or sets a string that indicates the font size of the Text. /// </summary> public int FontSize { get { return textFontSize; } set { textFontSize = value; } }
代码实现-Text /// <summary> /// The color of the Text. /// </summary> private string textColor; /// <summary> /// Gets or sets a string that indicates the color of the Text. /// </summary> public string Color { get { return textColor; } set { textColor = value; } }
代码实现-Image /// <summary> /// Represents an image in HTML document. /// </summary> public class Image : Paragraph { /// <summary> /// Initializes a new instance of the <see cref="Image"/> class. /// </summary> public Image() { }
代码实现-Image /// <summary> /// The source of the Image. /// </summary> private string source; /// <summary> /// Gets or sets a string that indicates the source of the Image. /// </summary> public string Src { get { return source; } set { source = value; } }
代码实现-Image /// <summary> /// The width of the image. /// </summary> private int imageWidth= 0; /// <summary> /// Gets or sets a string that indicates the width of the image. The unit is pixel. /// </summary> public int Width { get { return imageWidth; } set { imageWidth = value; } }
代码实现-Image /// <summary> /// The height of the image. /// </summary> private int imageHeight= 0; /// <summary> /// Gets or sets a string that indicates the height of the image. The unit is pixel. /// </summary> public int Height { get { return imageHeight; } set { imageHeight = value; } }
代码实现-Image /// <summary> /// Initializes a new instance of the <see cref="Image"/> class. /// </summary> /// <param name="src">The source of the image.</param> public Image(string src) { this.source = src; }
代码实现-Image /// <summary> /// Initializes a new instance of the <see cref="Image"/> class. /// </summary> /// <param name="src">The source of the image.</param> /// <param name="width">The width of the image</param> /// <param name="height">The height of the image.</param> public Image(string src,int width,int height) { this.source = src; this.Width = width; this.Height = height; }
代码实现-DocumentGenerator private void WriteParagraphs() { sw.Write("<body>\n"); foreach(Paragraph para in this.htmlDoc.Paragraphs) { if (para is HTMLWriter.Text) { HTMLWriter.Text text = para as HTMLWriter.Text; TextGenerator tg = new TextGenerator(); sw.Write(tg.GenerateTextTag(text)); } else if (para is HTMLWriter.Image) { HTMLWriter.Image img = para as HTMLWriter.Image; ImageGenerator ig = new ImageGenerator(); sw.Write(ig.GenerateImageTag(img)); } sw.Write("<br />"); } sw.Write("</body>\n"); }