コード例 #1
0
        protected override void Render(RenderingWorkUnit workUnit, ConversionApplicationOptions options, string outputFilePath)
        {
            var wi = ((UrlWorkUnit)workUnit);

            using (var document = new HTMLDocument(wi.Data, GetConfiguration(options)))
            {
                document.Save(outputFilePath, HTMLSaveFormat.MHTML);
            }
        }
コード例 #2
0
        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
        }
コード例 #3
0
        public override void Export(RadDocument document, Stream output)
        {
            //export as HTML
            using (output)
            {
                htmlProvider.Export(document, output);
            }
            //using Aspose.HTML to convert html to markdown
            var documentap = new HTMLDocument(outPath + "\\hide.html");
            var path       = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

            documentap.Save(Path.GetFullPath(outPath + "\\MarkDownFile.md"), HTMLSaveFormat.Markdown);
        }
コード例 #4
0
        private void SaveDocument(string name, String htmlPath, int handlingDepth)
        {
            var outPath = Path.Combine(_htmlOutputPath, name);

            Console.WriteLine(Path.GetFullPath(outPath));
            MHTMLSaveOptions mhtmlSaveOptions = new MHTMLSaveOptions();

            mhtmlSaveOptions.ResourceHandlingOptions.UrlRestriction   = UrlRestriction.SameHost;
            mhtmlSaveOptions.ResourceHandlingOptions.MaxHandlingDepth = handlingDepth;
            using (var htmlContainer = new HTMLDocument(htmlPath))
            {
                htmlContainer.Save(outPath, mhtmlSaveOptions);
            }
        }
コード例 #5
0
        public void CreateDocumentFromContentStringTest()
        {
            // Prepare HTML code
            var html_code = "<p>Hello World!</p>";

            // Initialize a document from the string variable
            using (var document = new HTMLDocument(html_code, "."))
            {
                // Save the document to a disk
                document.Save(Path.Combine(OutputDir, "create-from-string.html"));
            }

            Assert.True(File.Exists(Path.Combine(OutputDir, "create-from-string.html")));
        }
コード例 #6
0
        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")));
            }
        }
コード例 #7
0
        public void LoadDocumentFromFileTest1()
        {
            // Prepare a path to a file
            string documentPath = Path.Combine(DataDir, "Sprite.html");

            // Initialize an HTML document from the file
            using (var document = new HTMLDocument(documentPath))
            {
                // Work with the document

                // Save the document to a disk
                document.Save(Path.Combine(OutputDir, "Sprite_out.html"));
            }

            Assert.True(File.Exists(Path.Combine(OutputDir, "Sprite_out.html")));
        }
コード例 #8
0
        public void CreateEmptyDocumentTest()
        {
            // Prepare an output path for a document saving
            string documentPath = Path.Combine(OutputDir, "create-empty-document.html");

            // Initialize an empty HTML Document
            using (var document = new HTMLDocument())
            {
                // Work with the document

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

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

            // Prepare HTML code
            var html_code = "<H2>Hello World!</H2>";

            // Initialize a document from a string variable
            using (var document = new HTMLDocument(html_code, "."))
            {
                // Save the document as a Markdown file
                document.Save(documentPath, HTMLSaveFormat.Markdown);

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

            Assert.True(File.Exists(documentPath));
        }
コード例 #11
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));
        }
コード例 #12
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")));
            }
        }
コード例 #13
0
        public void LoadDocumentFromStreamTest()
        {
            // Create a memory stream object
            using (var mem = new MemoryStream())
                using (var sw = new StreamWriter(mem))
                {
                    // Write the HTML Code into memory object
                    sw.Write("<p>Hello World! I love HTML!</p>");

                    // It is important to set the position to the beginning, since HTMLDocument starts the reading exactly from the current position within the stream
                    sw.Flush();
                    mem.Seek(0, SeekOrigin.Begin);

                    // Initialize a document from the string variable
                    using (var document = new HTMLDocument(mem, "."))
                    {
                        // Save the document to disk
                        document.Save(Path.Combine(OutputDir, "load-from-stream.html"));

                        Assert.True(document.QuerySelectorAll("p").Length > 0);
                    }
                }
        }
コード例 #14
0
        public void SaveHTMLToMHTMLTest()
        {
            // Prepare an output path for a document saving
            string savePath = Path.Combine(OutputDir, "save-to-MHTML.mht");

            // Prepare a simple HTML file with a linked document
            File.WriteAllText("save-to-MHTML.html", "<p>Hello World!</p>" +
                              "<a href='linked-file.html'>linked file</a>");

            // Prepare a simple linked HTML file
            File.WriteAllText("linked-file.html", "<p>Hello linked file!</p>");

            // Load the "save-to-MTHML.html" into memory
            using (var document = new HTMLDocument("save-to-MHTML.html"))
            {
                // Save the document to MHTML format
                document.Save(savePath, HTMLSaveFormat.MHTML);

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

            Assert.True(File.Exists(savePath));
        }
コード例 #15
0
        public void EditExternalCSSTest()
        {
            // Prepare content of a CSS file
            var styleContent = ".flower1 { width:120px; height:40px; border-radius:20px; background:#4387be; margin-top:50px; } \r\n" +
                               ".flower2 { margin-left:0px; margin-top:-40px; background:#4387be; border-radius:20px; width:120px; height:40px; transform:rotate(60deg); } \r\n" +
                               ".flower3 { transform:rotate(-60deg); margin-left:0px; margin-top:-40px; width:120px; height:40px; border-radius:20px; background:#4387be; }\r\n" +
                               ".frame { margin-top:-50px; margin-left:310px; width:160px; height:50px; font-size:2em; font-family:Verdana; color:grey; }\r\n";

            // Prepare a linked CSS file
            File.WriteAllText("flower.css", styleContent);

            // Create an instance of HTML document with specified content
            var htmlContent = "<link rel=\"stylesheet\" href=\"flower.css\" type=\"text/css\" /> \r\n" +
                              "<div style=\"margin-top: 80px; margin-left:250px; transform: scale(1.3);\" >\r\n" +
                              "<div class=\"flower1\" ></div>\r\n" +
                              "<div class=\"flower2\" ></div>\r\n" +
                              "<div class=\"flower3\" ></div></div>\r\n" +
                              "<div style = \"margin-top: -90px; margin-left:120px; transform:scale(1);\" >\r\n" +
                              "<div class=\"flower1\" style=\"background: #93cdea;\"></div>\r\n" +
                              "<div class=\"flower2\" style=\"background: #93cdea;\"></div>\r\n" +
                              "<div class=\"flower3\" style=\"background: #93cdea;\"></div></div>\r\n" +
                              "<div style =\"margin-top: -90px; margin-left:-80px; transform: scale(0.7);\" >\r\n" +
                              "<div class=\"flower1\" style=\"background: #d5effc;\"></div>\r\n" +
                              "<div class=\"flower2\" style=\"background: #d5effc;\"></div>\r\n" +
                              "<div class=\"flower3\" style=\"background: #d5effc;\"></div></div>\r\n" +
                              "<p class=\"frame\">External</p>\r\n" +
                              "<p class=\"frame\" style=\"letter-spacing:10px; font-size:2.5em \">  CSS </p>\r\n";

            using (var document = new HTMLDocument(htmlContent, "."))
            {
                // Save the HTML document to a file
                document.Save(Path.Combine(OutputDir, "edit-external-css.html"));
            }

            Assert.True(File.Exists(Path.Combine(OutputDir, "edit-external-css.html")));
        }
コード例 #16
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);
        }