Example #1
0
        public static void RendererTimeoutExample()
        {
            //ExStart: RendererTimeoutExample
            // Prepare an HTML code
            var code = @"
            <script>
                var count = 0;
                setInterval(function()
                    {
                        var element = document.createElement('div');
                        var message = (++count) + '. ' + 'Hello World!!';
                        var text = document.createTextNode(message);
                        element.appendChild(text);
                        document.body.appendChild(element);
                    }, 1000);
            </script>
            ";

            // Initialize an HTML document based on prepared HTML code
            using (var document = new Aspose.Html.HTMLDocument(code, "."))
            {
                // Create an instance of HTML Renderer
                using (Aspose.Html.Rendering.HtmlRenderer renderer = new Aspose.Html.Rendering.HtmlRenderer())
                {
                    // Create an instance of PDF device
                    using (var device = new Aspose.Html.Rendering.Pdf.PdfDevice("output.pdf"))
                    {
                        // Render HTML to PDF
                        renderer.Render(device, System.TimeSpan.FromSeconds(5), document);
                    }
                }
            }
            //ExEnd: RendererTimeoutExample
        }
Example #2
0
        public static void CombineMultipleHTMLToPDF()
        {
            //ExStart: CombineMultipleHTMLToPDF
            // Prepare an HTML code
            var code1 = @"<br><span style='color: green'>Hello World!!</span>";
            var code2 = @"<br><span style='color: blue'>Hello World!!</span>";
            var code3 = @"<br><span style='color: red'>Hello World!!</span>";

            // Create three HTML documents to merge later
            using (var document1 = new Aspose.Html.HTMLDocument(code1, "."))
                using (var document2 = new Aspose.Html.HTMLDocument(code2, "."))
                    using (var document3 = new Aspose.Html.HTMLDocument(code3, "."))
                    {
                        // Create an instance of HTML Renderer
                        using (Aspose.Html.Rendering.HtmlRenderer renderer = new Aspose.Html.Rendering.HtmlRenderer())
                        {
                            // Create an instance of PDF device
                            using (var device = new Aspose.Html.Rendering.Pdf.PdfDevice("output.pdf"))
                            {
                                // Merge all HTML documents into PDF
                                renderer.Render(device, document1, document2, document3);
                            }
                        }
                    }
            //ExEnd: CombineMultipleHTMLToPDF
        }
Example #3
0
        private static byte[] GetPdf(string html)
        {
            var stream = new MemoryStream();

            Aspose.Html.Drawing.Page page = new Aspose.Html.Drawing.Page();
            page.Size = new Aspose.Html.Drawing.Size(1200, 800);
            Aspose.Html.Rendering.Pdf.PdfRenderingOptions pdf_options = new Aspose.Html.Rendering.Pdf.PdfRenderingOptions();
            pdf_options.PageSetup.AnyPage = page;
            using (Aspose.Html.Rendering.Pdf.PdfDevice pdf_device = new Aspose.Html.Rendering.Pdf.PdfDevice(pdf_options, stream))
                using (Aspose.Html.Rendering.HtmlRenderer renderer = new Aspose.Html.Rendering.HtmlRenderer())
                    using (Aspose.Html.HTMLDocument html_document = new Aspose.Html.HTMLDocument(html, ""))
                    {
                        renderer.Render(pdf_device, html_document);
                        return(stream.ToArray());
                    }
        }
Example #4
0
        public static void Run()
        {
            // ExStart:HtmlToXPS
            // The path to the documents directory.
            string dataDir = RunExamples.GetDataDir_Data();

            String InputHtml = dataDir + "FirstFile.html";

            using (FileStream fs = File.Create(InputHtml))
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    sw.Write(
                        @"<style>
                    .st
                    {
                    color: green;
                    }
                    </style>
                    <div id=id1>Aspose.Html rendering Text in Black Color</div>
                    <div id=id2 class=''st''>Aspose.Html rendering Text in Green Color</div>
                    <div id=id3 class=''st'' style='color: blue;'>Aspose.Html rendering Text in Blue Color</div>
                    <div id=id3 class=''st'' style='color: red;'>Aspose.Html rendering Text in Red Color</div>
                    ");
                }

            // File name for resultant XPS file
            string Resultnat_output = dataDir + "simple-any-page_out.xps";

            // Create XpxRendering Options object
            Aspose.Html.Rendering.Xps.XpsRenderingOptions xps_options = new Aspose.Html.Rendering.Xps.XpsRenderingOptions();
            // The PageSetup also provides different properties i.e. FirstPage, LastPage, LeftPage, RightPage and they are used to setup (PageSize, Margin) for every page.
            // In most cases, usage of setup any page is enough, but in some complicated cases, you may need to fine tune page settings. It can be done either by CSS styles or by using rendering options.
            // the size for drawing is in pixels
            xps_options.PageSetup.AnyPage = new Aspose.Html.Drawing.Page(new Aspose.Html.Drawing.Size(200, 60));
            // Instantiate XPSDevice object while passing XPSRenderingOptions and resultant file path as arguments
            using (Aspose.Html.Rendering.Xps.XpsDevice device = new Aspose.Html.Rendering.Xps.XpsDevice(xps_options, Resultnat_output))
                // Create HtmlRenderer object
                using (Aspose.Html.Rendering.HtmlRenderer renderer = new Aspose.Html.Rendering.HtmlRenderer())
                    // Create HtmlDocument instnace while passing path of already created HTML file
                    using (Aspose.Html.HTMLDocument html_document = new Aspose.Html.HTMLDocument(InputHtml))
                    {
                        // Render the output using HtmlRenderer
                        renderer.Render(device, html_document);
                    }
            // ExEnd:HtmlToXPS
        }
Example #5
0
        public static void Run()
        {
            // ExStart:HTMLToTIFF
            // The path to the documents directory
            string dataDir = RunExamples.GetDataDir_Data();

            Aspose.Html.Rendering.Image.ImageRenderingOptions pdf_options = new Aspose.Html.Rendering.Image.ImageRenderingOptions();

            // Instantiate PdfDevice object while passing PdfRenderingOptions and resultant file path as arguments
            using (Aspose.Html.Rendering.Image.ImageDevice pdf_device = new Aspose.Html.Rendering.Image.ImageDevice(pdf_options, dataDir + "Aspose_HTML.tiff"))
                // Create HtmlRenderer object
                using (Aspose.Html.Rendering.HtmlRenderer renderer = new Aspose.Html.Rendering.HtmlRenderer())
                    // Create HtmlDocument instance while passing path of already created HTML file
                    using (Aspose.Html.HTMLDocument html_document = new Aspose.Html.HTMLDocument(dataDir + "input.html"))
                    {
                        // Render the output using HtmlRenderer
                        renderer.Render(pdf_device, html_document);
                    }
            // ExEnd:HTMLToTIFF
        }
Example #6
0
        protected override void Render(HtmlTextWriter writer)
        {
            if (RenderToPDF)
            {
                // setup a TextWriter to capture the current page HTML code
                TextWriter     tw  = new StringWriter();
                HtmlTextWriter htw = new HtmlTextWriter(tw);

                // render the HTML markup into the TextWriter
                base.Render(htw);

                // get the current page HTML code
                var htmlCode = tw.ToString();

                // Load HTML file
                var baseUri   = Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath;
                var _document = new Aspose.Html.HTMLDocument(htmlCode, baseUri);

                // Save output as PDF format
                using (var streamOut = new System.IO.MemoryStream())
                {
                    var device   = new Aspose.Html.Rendering.Pdf.PdfDevice(options, streamOut);
                    var renderer = new Aspose.Html.Rendering.HtmlRenderer();
                    renderer.Render(device, _document);
                    HttpContext.Current.Response.Clear();
                    HttpContext.Current.Response.ContentType = "application/pdf";
                    HttpContext.Current.Response.AppendHeader("Content-Disposition", $"attachment; filename={OutputFileName}");
                    HttpContext.Current.Response.BinaryWrite(streamOut.ToArray());
                    HttpContext.Current.Response.End();
                }
            }
            else
            {
                // render web page in browser
                base.Render(writer);
            }
        }
        public static void Run()
        {
            // ExStart:AdjustXPSPageSize
            // The path to the documents directory.
            string dataDir = RunExamples.GetDataDir_Data();

            // Set input file name.
            String SimpleStyledFilePath = dataDir + "FirstFile.html";

            using (FileStream fs = File.Create(SimpleStyledFilePath))
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    sw.Write(
                        @"<style>
                    .st
                    {
                    color: green;
                    }
                    </style>
                    <div id=id1>Aspose.Html rendering Text in Black Color</div>
                    <div id=id2 class=''st''>Aspose.Html rendering Text in Green Color</div>
                    <div id=id3 class=''st'' style='color: blue;'>Aspose.Html rendering Text in Blue Color</div>
                    <div id=id3 class=''st'' style='color: red;'>Aspose.Html rendering Text in Red Color</div>
                    ");
                }

            // Create HtmlRenderer object
            using (Aspose.Html.Rendering.HtmlRenderer renderer = new Aspose.Html.Rendering.HtmlRenderer())
                // Create HtmlDocument instnace while passing path of already created HTML file
                using (Aspose.Html.HTMLDocument html_document = new Aspose.Html.HTMLDocument(SimpleStyledFilePath))
                {
                    // Set the page size less than document min-width. The content in the resulting file will be cropped becuase of element with width: 200px
                    Aspose.Html.Rendering.Xps.XpsRenderingOptions xps_options = new Aspose.Html.Rendering.Xps.XpsRenderingOptions
                    {
                        PageSetup =
                        {
                            AnyPage            = new Aspose.Html.Drawing.Page(new Aspose.Html.Drawing.Size(100, 100)),
                            AdjustToWidestPage = false
                        },
                    };
                    string output = dataDir + "not-adjusted-to-widest-page_out.xps";
                    using (Aspose.Html.Rendering.Xps.XpsDevice device = new Aspose.Html.Rendering.Xps.XpsDevice(xps_options, output))
                    {
                        // Render the output
                        renderer.Render(device, html_document);
                    }


                    // Set the page size less than document min-width and enable AdjustToWidestPage option
                    // The page size of the resulting file will be changed according to content width
                    xps_options = new Aspose.Html.Rendering.Xps.XpsRenderingOptions
                    {
                        PageSetup =
                        {
                            AnyPage            = new Aspose.Html.Drawing.Page(new Aspose.Html.Drawing.Size(100, 100)),
                            AdjustToWidestPage = true
                        },
                    };
                    output = dataDir + "adjusted-to-widest-page_out.xps";
                    using (Aspose.Html.Rendering.Xps.XpsDevice device = new Aspose.Html.Rendering.Xps.XpsDevice(xps_options, output))
                    {
                        // render the output
                        renderer.Render(device, html_document);
                    }
                }
            // ExEnd:AdjustXPSPageSize
        }