Exemple #1
0
        public void ZIPtoPDFTimeLoggingTest()
        {
            // Prepare path to a source zip file
            string documentPath = Path.Combine(DataDir, "test.zip");

            // Prepare path for converted file saving
            string savePath = Path.Combine(OutputDir, "zip-to-pdf-duration.pdf");

            // Create an instance of the Configuration class
            using var configuration = new Configuration();
            var service  = configuration.GetService <INetworkService>();
            var handlers = service.MessageHandlers;

            // Custom Schema: ZIP. Add ZipFileSchemaMessageHandler to the end of the pipeline
            handlers.Add(new ZipFileSchemaMessageHandler(new Archive(documentPath)));

            // Duration Logging. Add the StartRequestDurationLoggingMessageHandler at the first place in the pipeline
            handlers.Insert(0, new StartRequestDurationLoggingMessageHandler());

            // Add the StopRequestDurationLoggingMessageHandler to the end of the pipeline
            handlers.Add(new StopRequestDurationLoggingMessageHandler());

            // Initialize an HTML document with specified configuration
            using var document = new HTMLDocument("zip-file:///test.html", configuration);

            // Create the PDF Device
            using var device = new PdfDevice(savePath);

            // Render ZIP to PDF
            document.RenderTo(device);

            Assert.True(File.Exists(savePath));
        }
        public void ConvertZIPtoPDFTest()
        {
            // Prepare path to a source zip file
            string documentPath = Path.Combine(DataDir, "test.zip");

            // Prepare path for converted file saving
            string savePath = Path.Combine(OutputDir, "zip-to-pdf.pdf");

            // Create an instance of ZipArchiveMessageHandler
            using var zip = new ZipArchiveMessageHandler(documentPath);

            // Create an instance of the Configuration class
            using var configuration = new Configuration();

            // Add ZipArchiveMessageHandler to the chain of existing message handlers
            configuration
            .GetService <INetworkService>()
            .MessageHandlers.Add(zip);

            // Initialize an HTML document with specified configuration
            using var document = new HTMLDocument("zip:///test.html", configuration);

            // Create the PDF Device
            using var device = new PdfDevice(savePath);

            // Render ZIP to PDF
            document.RenderTo(device);

            Assert.True(File.Exists(savePath));
        }
        public void ExternalCSSTest()
        {
            // Create an instance of HTML document with specified content
            var htmlContent = "<link rel=\"stylesheet\" href=\"https://docs.aspose.com/html/net/editing-a-document/external.css\" type=\"text/css\" />\r\n" +
                              "<div class=\"rect1\" ></div>\r\n" +
                              "<div class=\"rect2\" ></div>\r\n" +
                              "<div class=\"frame\">\r\n" +
                              "<p style=\"font-size:2.5em; color:#ae4566;\"> External CSS </p>\r\n" +
                              "<p class=\"rect3\"> An external CSS can be created once and applied to multiple web pages</p></div>\r\n";

            using (var document = new HTMLDocument(htmlContent, "."))
            {
                // Save the HTML document to a file
                document.Save(Path.Combine(OutputDir, "external-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, "external-css.pdf")))
                {
                    // Render HTML to PDF
                    document.RenderTo(device);
                }

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

            Assert.True(File.Exists(Path.Combine(OutputDir, "external-css.html")));
        }
        public void EditInlineCSSTest()
        {
            // Create an instance of an HTML document with specified content
            var content = "<p>InlineCSS </p>";

            using (var document = new HTMLDocument(content, "."))
            {
                // Find the paragraph element to set a style
                var paragraph = (HTMLElement)document.GetElementsByTagName("p").First();

                // Set the style attribute
                paragraph.SetAttribute("style", "font-size:250%; font-family:verdana; color:#cd66aa");

                // Save the HTML document to a file
                document.Save(Path.Combine(OutputDir, "edit-inline-css.html"));

                // Create an instance of PDF output device and render the document into this device
                using (var device = new PdfDevice(Path.Combine(OutputDir, "edit-inline-css.pdf")))
                {
                    // Render HTML to PDF
                    document.RenderTo(device);
                }

                Assert.True(File.Exists(Path.Combine(OutputDir, "edit-inline-css.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")));
            }
        }
Exemple #6
0
        public static void AddTitleAndPageNumber()
        {
            //ExStart: AddTitleAndPageNumber
            //  Initialize configuration object and set up the page-margins for the document
            using (Configuration configuration = new Configuration())
            {
                // Get the User Agent service
                var userAgent = configuration.GetService <Aspose.Html.Services.IUserAgentService>();

                // Set the style of custom margins and create marks on it
                userAgent.UserStyleSheet = @"@page 
                                            {
                                                /* Page margins should be not empty in order to write content inside the margin-boxes */
                                                margin-top: 1cm;
                                                margin-left: 2cm;
                                                margin-right: 2cm;
                                                margin-bottom: 2cm;
                                                /* Page counter located at the bottom of the page */
                                                @bottom-right
                                                {
                                                    -aspose-content: ""Page "" currentPageNumber() "" of "" totalPagesNumber();
                                                    color: green;
                                                }

                                                /* Page title located at the top-center box */
                                                @top-center
                                                {
                                                    -aspose-content: ""Hello World Document Title!!!"";
                                                    vertical-align: bottom;
                                                    color: blue;
                                                }    
                                            }";

                //  Initialize an HTML document
                using (HTMLDocument document = new HTMLDocument("<div>Hello World!!!</div>", ".", configuration))
                {
                    //  Initialize an output device
                    using (Aspose.Html.Rendering.Xps.XpsDevice device = new Aspose.Html.Rendering.Xps.XpsDevice("output.xps"))
                    {
                        // Send the document to the output device
                        document.RenderTo(device);
                    }
                }
            }
            //ExEnd: AddTitleAndPageNumber
        }
Exemple #7
0
        public static void Run()
        {
            // ExStart:1
            string dataDir = RunExamples.GetDataDir_Data();

            //  Initialize configuration object and set up the page-margins for the document
            Configuration configuration = new Configuration();

            configuration.GetService <IUserAgentService>().UserStyleSheet = @"
                                                                            @page 
                                                                            {
                                                                                /* Page margins should be not empty in order to write content inside the margin-boxes */
                                                                                margin-top: 1cm;
                                                                                margin-left: 2cm;
                                                                                margin-right: 2cm;
                                                                                margin-bottom: 2cm;

                                                                                /* Page counter located at the bottom of the page */
                                                                                @bottom-right
                                                                                {
                                                                                    -aspose-content: ""Page "" currentPageNumber() "" of "" totalPagesNumber();
                                                                                    color: green;
                                                                                }
    
                                                                                /* Page title located at the top-center box */
                                                                                @top-center
                                                                                {
                                                                                    -aspose-content: ""Document's title"";
                                                                                    vertical-align: bottom;
                                                                                }    
                                                                            }";

            //  Initialize an empty document
            using (HTMLDocument document = new HTMLDocument(configuration))
            {
                //  Initialize an output device
                using (XpsDevice device = new XpsDevice(dataDir + "output_out.xps"))
                {
                    // Send the document to the output device
                    document.RenderTo(device);
                }
            }
            // ExEnd:1
        }
        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 ConvertHTMLtoPDFTest()
        {
            // Prepare path to a source HTML file
            string documentPath = Path.Combine(DataDir, "spring.html");

            // Prepare path for converted file saving
            string savePath = Path.Combine(OutputDir, "spring-output.pdf");

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

            // Create an instance of the PdfRenderingOptions class
            var pdfOptions = new PdfRenderingOptions();

            // Create the PDF Device and specify the output file to render
            using var device = new PdfDevice(pdfOptions, savePath);

            // Render HTML to PDF
            document.RenderTo(device);


            Assert.True(File.Exists(Path.Combine(OutputDir, "spring-output.pdf")));
        }
Exemple #10
0
        public void ConvertZIPtoJPGTest()
        {
            // Prepare path to a source zip file
            string documentPath = Path.Combine(DataDir, "test.zip");

            // Prepare path for converted file saving
            string savePath = Path.Combine(OutputDir, "zip-to-jpg.jpg");

            // Create an instance of ZipArchiveMessageHandler
            using var zip = new ZipArchiveMessageHandler(documentPath);

            // Create an instance of the Configuration class
            using var configuration = new Configuration();

            // Add ZipArchiveMessageHandler to the chain of existing message handlers
            configuration
            .GetService <INetworkService>()
            .MessageHandlers.Add(zip);

            // Initialize an HTML document with specified configuration
            using var document = new HTMLDocument("zip:///test.html", configuration);

            // Create an instance of Rendering Options
            var options = new ImageRenderingOptions()
            {
                Format = ImageFormat.Jpeg
            };

            // Create an instance of Image Device
            using var device = new ImageDevice(options, savePath);

            // Render ZIP to JPG
            document.RenderTo(device);

            Assert.True(File.Exists(Path.Combine(OutputDir, "zip-to-jpg_1.jpg")));
        }