コード例 #1
0
        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")));
            }
        }
コード例 #2
0
        public static void Run()
        {
            // ExStart:1
            // The path to the documents directory
            string dataDir = RunExamples.GetDataDir_Data();

            // Create a custom StreamProvider based on ICreateStreamProvider interface
            using (MemoryStreamProvider streamProvider = new MemoryStreamProvider())
            {
                // Create a simple HTML document
                using (HTMLDocument document = new HTMLDocument())
                {
                    // Add your first 'hello world' to the document.
                    document.Body.AppendChild(document.CreateTextNode("Hello world!!!"));

                    // Convert HTML document to XPS by using the custom StreamProvider
                    Aspose.Html.Converters.Converter.ConvertHTML(document, new XpsSaveOptions(), streamProvider);

                    // Get access to the memory stream that contains the result data
                    var memory = streamProvider.Streams[0];
                    memory.Seek(0, SeekOrigin.Begin);

                    // Flush the result data to the output file
                    using (FileStream fs = File.Create(dataDir + "output.xps"))
                    {
                        memory.CopyTo(fs);
                    }
                }
            }
            // ExEnd:1
        }
コード例 #3
0
        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);
        }
コード例 #4
0
        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);
        }
コード例 #5
0
ファイル: Quirksmode.cs プロジェクト: zhlm119/AngleSharp
        public void CreateTextNode()
        {
            var text   = " textNode";
            var test   = document.CreateTextNode(text);
            var testEl = document.GetElementById("test");

            for (var i = testEl.ChildNodes.Length - 1; i >= 0; i--)
            {
                testEl.RemoveChild(testEl.ChildNodes[i]);
            }

            Assert.AreEqual(0, testEl.Children.Length);
            testEl.AppendChild(test);
            Assert.AreEqual(text, testEl.InnerHTML);
            Assert.AreEqual(document, test.OwnerDocument);
        }
コード例 #6
0
        public void CreateNewDocumentTest()
        {
            // Prepare an output path for a document saving
            string documentPath = Path.Combine(OutputDir, "create-new-document.html");

            // Initialize an empty HTML Document
            using (var document = new HTMLDocument())
            {
                // Create a text element and add it to the document
                var text = document.CreateTextNode("Hello World!");
                document.Body.AppendChild(text);

                // Save the document to a disk
                document.Save(documentPath);
            }

            Assert.True(File.Exists(documentPath));
        }
コード例 #7
0
        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
        }
コード例 #8
0
        public void SaveHTMLToFileTest()
        {
            // Prepare an output path for a document saving
            string documentPath = Path.Combine(OutputDir, "save-to-file.html");

            // Initialize an empty HTML document
            using (var document = new HTMLDocument())
            {
                // Create a text element and add it to the document
                var text = document.CreateTextNode("Hello World!");
                document.Body.AppendChild(text);

                // Save the HTML document to the file on a disk
                document.Save(documentPath);

                Assert.True(document.QuerySelectorAll("body").Length > 0);
            }

            Assert.True(File.Exists(documentPath));
        }
コード例 #9
0
        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")));
            }
        }
コード例 #10
0
        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);
        }