Welcome to Aspose.HTML
"; using (HTMLDocument doc = new HTMLDocument(htmlCode, ".")) { doc.Save("from-string.html"); } // Load HTML document from a file using (HTMLDocument doc = new HTMLDocument("input.html")) { Console.WriteLine(doc.DocumentElement.OuterHTML); } // Load HTML document from a URL using (HTMLDocument doc = new HTMLDocument("https://example.com")) { Console.WriteLine(doc.Title); } // Load HTML document from a stream using (MemoryStream stream = new MemoryStream()) using (StreamWriter sw = new StreamWriter(stream)) { sw.Write("Hello from stream!
"); sw.Flush(); stream.Seek(0, SeekOrigin.Begin); using (HTMLDocument doc = new HTMLDocument(stream, ".")) { doc.Save("from-stream.html"); } } ``` -------------------------------- ### Render HTML to PDF with Custom Devices in C# Source: https://context7.com/aspose-html/aspose.html-for-.net/llms.txt Demonstrates how to render an HTML document to a PDF file using the PdfDevice. This includes creating styled HTML content and rendering it directly or from an existing HTML file. It utilizes the Aspose.Html library for document manipulation and rendering. ```csharp using Aspose.Html; using Aspose.Html.Dom; using Aspose.Html.Rendering.Pdf; using System.Linq; // Create HTML document with styled content using (HTMLDocument document = new HTMLDocument()) { // Add CSS styles Element style = document.CreateElement("style"); style.TextContent = ".highlight { color: green; font-size: 24px; }"; Element head = document.GetElementsByTagName("head").First(); head.AppendChild(style); // Add content HTMLParagraphElement p = (HTMLParagraphElement)document.CreateElement("p"); p.ClassName = "highlight"; p.AppendChild(document.CreateTextNode("Rendered with PdfDevice!")); document.Body.AppendChild(p); // Save HTML document.Save("styled-document.html"); // Render directly to PDF using PdfDevice using (PdfDevice device = new PdfDevice("rendered-output.pdf")) { document.RenderTo(device); } } // Render from existing HTML file using (HTMLDocument document = new HTMLDocument("input.html")) { // Create PDF device with custom output path using (PdfDevice device = new PdfDevice(new PdfRenderingOptions(), "custom-render.pdf")) { document.RenderTo(device); } } ``` -------------------------------- ### Populate HTML Templates with Data using Aspose.HTML for .NET Source: https://context7.com/aspose-html/aspose.html-for-.net/llms.txt This snippet shows how to use Aspose.HTML for .NET to populate HTML templates with data from JSON or XML sources. It covers creating templates, providing data, and converting them into output HTML files. Dependencies include Aspose.Html and System.IO. ```csharp using Aspose.Html; using Aspose.Html.Converters; using Aspose.Html.Loading; using System.IO; // Create template and JSON data string template = @"| Person | Address |
|---|---|
| {{FirstName}} {{LastName}} | {{Address.Street}} {{Address.Number}}, {{Address.City}} |
Hello, {{FirstName}} {{LastName}}!
", string.Empty, new TemplateData(new TemplateContentOptions(xmlData, TemplateContent.XML)), new TemplateLoadOptions())) { doc.Save("xml-template-output.html"); } // Template with attribute control (checkbox example) string checkboxTemplate = ""; string checkedData = "World!