public void EditDocumentTreeTest() { // Create an instance of an HTML document using (var document = new HTMLDocument()) { var body = document.Body; // Create a paragraph element var p = (HTMLParagraphElement)document.CreateElement("p"); // Set a custom attribute p.SetAttribute("id", "my-paragraph"); // Create a text node var text = document.CreateTextNode("my first paragraph"); // Attach the text to the paragraph p.AppendChild(text); // Attach paragraph to the document body body.AppendChild(p); // Save the HTML document to a file document.Save(Path.Combine(OutputDir, "edit-document-tree.html")); Assert.True(File.Exists(Path.Combine(OutputDir, "edit-document-tree.html"))); } }
public void CreateElementInUppercase() { var x = document.CreateElement("P"); Assert.IsNotNull(x); Assert.IsTrue(x is HTMLParagraphElement); Assert.AreEqual(document, x.OwnerDocument); }
private HTMLAnchorElement GetAnchorElement(HTMLDocument htmlDocument, string buttonText) { HTMLAnchorElement anchorElement = (HTMLAnchorElement)htmlDocument.CreateElement("a"); Text backwardText = htmlDocument.CreateTextNode(buttonText); anchorElement.SetAttribute("class", "button"); anchorElement.AppendChild(backwardText); return(anchorElement); }
private HTMLParagraphElement GetParagraphElement(HTMLDocument htmlDocument, string content) { HTMLParagraphElement paragraphElement = (HTMLParagraphElement)htmlDocument.CreateElement("p"); paragraphElement.SetAttribute("class", "title"); Text text = htmlDocument.CreateTextNode(content); paragraphElement.AppendChild(text); return(paragraphElement); }
public static void Run() { // ExStart:CreateSimpleDocument string dataDir = RunExamples.GetDataDir_Data(); String outputHtml = dataDir + "SimpleDocument.html"; // Create an instance of HTMLDocument var document = new HTMLDocument(); // Add image if (document.CreateElement("img") is HTMLImageElement img) { img.Src = "http://via.placeholder.com/400x200"; img.Alt = "Placeholder 400x200"; img.Title = "Placeholder image"; document.Body.AppendChild(img); } // Add ordered list var orderedListElement = document.CreateElement("ol") as HTMLOListElement; for (int i = 0; i < 10; i++) { var listItem = document.CreateElement("li") as HTMLLIElement; listItem.TextContent = $" List Item {i + 1}"; orderedListElement.AppendChild(listItem); } document.Body.AppendChild(orderedListElement); // Add table 3x3 var table = document.CreateElement("table") as HTMLTableElement; var tBody = document.CreateElement("tbody") as HTMLTableSectionElement; for (var i = 0; i < 3; i++) { var row = document.CreateElement("tr") as HTMLTableRowElement; row.Id = "trow_" + i; for (var j = 0; j < 3; j++) { var cell = document.CreateElement("td") as HTMLTableCellElement; cell.Id = $"cell{i}_{j}"; cell.TextContent = "Cell " + j; row.AppendChild(cell); } tBody.AppendChild(row); } table.AppendChild(tBody); document.Body.AppendChild(table); //Save the document to disk document.Save(outputHtml); // ExEnd:CreateSimpleDocument }
public void UsingDOMTest() { // Create an instance of an HTML document using (var document = new HTMLDocument()) { // Create a style element and assign the green color for all elements with class-name equals 'gr'. var style = document.CreateElement("style"); style.TextContent = ".gr { color: green }"; // Find the document header element and append style element to the header var head = document.GetElementsByTagName("head").First(); head.AppendChild(style); // Create a paragraph element with class-name 'gr'. var p = (HTMLParagraphElement)document.CreateElement("p"); p.ClassName = "gr"; // Create a text node var text = document.CreateTextNode("Hello World!!"); // Append the text node to the paragraph p.AppendChild(text); // Append the paragraph to the document body element document.Body.AppendChild(p); // Save the HTML document to a file document.Save(Path.Combine(OutputDir, "using-dom.html")); // Create an instance of the PDF output device and render the document into this device using (var device = new PdfDevice(Path.Combine(OutputDir, "using-dom.pdf"))) { // Render HTML to PDF document.RenderTo(device); } Assert.True(File.Exists(Path.Combine(OutputDir, "using-dom.pdf"))); } }
public void EditInternalCSSTest() { // Create an instance of an HTML document with specified content var content = "<div><p>Internal CSS</p><p>An internal CSS is used to define a style for a single HTML page</p></div>"; using (var document = new HTMLDocument(content, ".")) { var style = document.CreateElement("style"); style.TextContent = ".frame1 { margin-top:50px; margin-left:50px; padding:20px; width:360px; height:90px; background-color:#a52a2a; font-family:verdana; color:#FFF5EE;} \r\n" + ".frame2 { margin-top:-90px; margin-left:160px; text-align:center; padding:20px; width:360px; height:100px; background-color:#ADD8E6;}"; // Find the document header element and append the style element to the header var head = document.GetElementsByTagName("head").First(); head.AppendChild(style); // Find the first paragraph element to inspect the styles var paragraph = (HTMLElement)document.GetElementsByTagName("p").First(); paragraph.ClassName = "frame1"; // Find the last paragraph element to inspect the styles var lastParagraph = (HTMLElement)document.GetElementsByTagName("p").Last(); lastParagraph.ClassName = "frame2"; // Set a color to the first paragraph paragraph.Style.FontSize = "250%"; paragraph.Style.TextAlign = "center"; // Set a font-size to the last paragraph lastParagraph.Style.Color = "#434343"; lastParagraph.Style.FontSize = "150%"; lastParagraph.Style.FontFamily = "verdana"; // Save the HTML document to a file document.Save(Path.Combine(OutputDir, "edit-internal-css.html")); // Create the instance of the PDF output device and render the document into this device using (var device = new PdfDevice(Path.Combine(OutputDir, "edit-internal-css.pdf"))) { // Render HTML to PDF document.RenderTo(device); } Assert.True(document.QuerySelectorAll("style").Length > 0); Assert.True(File.Exists(Path.Combine(OutputDir, "edit-internal-css.pdf"))); Assert.True(File.Exists(Path.Combine(OutputDir, "edit-internal-css.html"))); } }
public static void Run() { // ExStart:MutationObserver // The path to the documents directory. string dataDir = RunExamples.GetDataDir_Data(); // The MutationObserver interface provides the ability to watch for changes being made to the DOM tree. // Create an empty document using (var document = new HTMLDocument()) { // Create a WaitHandle for purpose described below var @event = new ManualResetEvent(false); // Create an observer instance var observer = new MutationObserver((mutations, mutationObserver) => { var mutation = mutations[0]; Console.WriteLine(mutation.AddedNodes[0]); @event.Set(); }); // Options for the observer (which mutations to observe) var config = new MutationObserverInit { ChildList = true, Subtree = true }; // Start observing the target node observer.Observe(document.DocumentElement, config); // An example of user modifications var p = document.CreateElement("p"); document.DocumentElement.AppendChild(p); // Since, mutations are working in the async mode you should wait a bit. We use WaitHandle for this purpose. @event.WaitOne(); // Later, you can stop observing observer.Disconnect(); } // ExEnd:MutationObserver }
public static void Run() { // ExStart:ManipulateCanvas // The path to the documents directory. string dataDir = RunExamples.GetDataDir_Data(); // Create an empty document using (HTMLDocument document = new HTMLDocument()) { // Create a Canvas element var canvas = (HTMLCanvasElement)document.CreateElement("canvas"); // Set the canvas size canvas.Width = 300; canvas.Height = 150; // Append created element to the document document.Body.AppendChild(canvas); // Initialize a canvas 2D context var context = (ICanvasRenderingContext2D)canvas.GetContext("2d"); // Prepare a gradient brush var gradient = context.CreateLinearGradient(0, 0, canvas.Width, 0); gradient.AddColorStop(0, "magenta"); gradient.AddColorStop(0.5, "blue"); gradient.AddColorStop(1.0, "red"); // Set the previously prepared brush to the fill and stroke properties context.FillStyle = gradient; context.StrokeStyle = gradient; // Fill a rectange context.FillRect(0, 95, 300, 20); // Write a text context.FillText("Hello World!", 10, 90, 500); // Create an instance of HTML renderer and XPS output device using (var renderer = new HtmlRenderer()) using (var device = new XpsDevice(dataDir + "canvas.xps")) { // Render the document to the specified device renderer.Render(device, document); } } // ExEnd:ManipulateCanvas }
public static void ObserveHowNodesAreAdded() { //ExStart: ObserveHowNodesAreAdded // Create an empty HTML document using (var document = new HTMLDocument()) { // Create a mutation observer instance var observer = new Aspose.Html.Dom.Mutations.MutationObserver((mutations, mutationObserver) => { foreach (var record in mutations) { foreach (var node in record.AddedNodes) { Console.WriteLine("The '" + node + "' node was added to the document."); } } }); // configuration of the observer var config = new Aspose.Html.Dom.Mutations.MutationObserverInit { ChildList = true, Subtree = true, CharacterData = true }; // pass in the target node to observe with the specified configuration observer.Observe(document.Body, config); // Now, we are going to modify DOM tree to check // Create an paragraph element and append it to the document body var p = document.CreateElement("p"); document.Body.AppendChild(p); // Create a text and append it to the paragraph var text = document.CreateTextNode("Hello World"); p.AppendChild(text); Console.WriteLine("Waiting for mutation. Press any key to continue..."); Console.ReadLine(); } //ExEnd: ObserveHowNodesAreAdded }
public void Merge(string[] htmlFiles, string outPath) { _htmlOutputPath = Path.GetDirectoryName(outPath); var temp = Path.Combine(_htmlOutputPath, "Output2"); Directory.CreateDirectory(temp); for (var i = 0; i < htmlFiles.Length; i++) { var htmlFile = htmlFiles[i]; var fileName = Path.GetFileName(htmlFile); var tempOutputFilePath = Path.Combine(temp, fileName); File.Copy(htmlFile, tempOutputFilePath); htmlFiles[i] = tempOutputFilePath; using (var htmlDocument = new HTMLDocument(htmlFiles[i])) { HTMLDivElement anchorDivContainer = (HTMLDivElement)htmlDocument.CreateElement("div"); HTMLBodyElement htmlDocumentBody = (HTMLBodyElement)htmlDocument.Body; HTMLHeadElement head = (HTMLHeadElement)htmlDocument.GetElementsByTagName("head")[0]; HTMLStyleElement htmlStyleElement = (HTMLStyleElement)htmlDocument.CreateElement("style"); var resetEvent = new AutoResetEvent(false); htmlStyleElement.OnLoad += (sender, e) => { resetEvent.Set(); }; htmlStyleElement.OnError += (sender, e) => { resetEvent.Set(); }; var cssContent = htmlDocument.CreateTextNode(_content); htmlStyleElement.AppendChild(cssContent); head.AppendChild(htmlStyleElement); resetEvent.WaitOne(); anchorDivContainer.SetAttribute("class", "container"); anchorDivContainer.SetAttribute("align", "center"); var paragraphElement = GetParagraphElement(htmlDocument, "Navigation"); anchorDivContainer.AppendChild(paragraphElement); if (i != 0) { var anchorBackward = GetAnchorElement(htmlDocument, "Backward"); anchorBackward.Href = Path.GetFileName(htmlFiles[i - 1]); anchorDivContainer.AppendChild(anchorBackward); } else { HTMLAnchorElement anchorBackward = GetAnchorElement(htmlDocument, "Backward"); anchorBackward.Href = Path.GetFileName(htmlFiles[htmlFiles.Length - 1]); anchorDivContainer.AppendChild(anchorBackward); } if (i != htmlFiles.Length - 1) { HTMLAnchorElement anchorForward = GetAnchorElement(htmlDocument, "Forward"); anchorForward.Href = Path.GetFileName(htmlFiles[i + 1]); anchorDivContainer.AppendChild(anchorForward); } else { HTMLAnchorElement anchorForward = GetAnchorElement(htmlDocument, "Forward"); anchorForward.Href = Path.GetFileName(htmlFiles[0]); anchorDivContainer.AppendChild(anchorForward); } Node firstChild = htmlDocumentBody.FirstChild; htmlDocumentBody.InsertBefore(anchorDivContainer, firstChild); var outputPath = Path.Combine(_htmlOutputPath, fileName); htmlDocument.Save(outputPath); htmlDocument.Dispose(); htmlFiles[i] = outputPath; ClearDirectory(temp); } } var name = Path.GetFileNameWithoutExtension(outPath); SaveDocument($"{name}.mhtml", htmlFiles[0], htmlFiles.Length); }