andruhovski icon

Task985

andruhovski | PRO | 10/02/17 12:29:37 PM UTC | 0 ⭐ | 857 👁️ | Never ⏰ | []
C# |

10.08 KB

|

None

|

0 👍

/

0 👎

using System;
using Aspose.Html;
using Aspose.Html.Dom.Css;
using Aspose.Html.Drawing;
using Aspose.Html.Rendering;
using Aspose.Html.Rendering.Pdf;
 
namespace Task985
{
    class Program
    {
        private static HTMLDocument _document;        
        private static readonly Length Height = Unit.FromMillimeters(297);
        private static readonly Length Width= Unit.FromMillimeters(210);
        private static readonly Size PageSizeA4 = new Size(Width, Height);
        private static void Main()
        {
            _document = new HTMLDocument();
            GenerateSampleHtml5Document();            
           // RenderSimplePdf();
           // RenderAdvancedPdf();
           // RenderMirrorPages();
            RenderWithCoverPage();
        }
 
        private static void RenderWithCoverPage()
        {
            Console.WriteLine(_document.DocumentElement.InnerHTML);
 
            var margin = new Margin(Unit.FromMillimeters(30)); //all margins are 30 mm
 
            var options = new PdfRenderingOptions();
            options.PageSetup.FirstPage=new Page(PageSizeA4, margin);
 
            const string outputfile = @"C:\aspose\pdf\output04.pdf";
            Console.WriteLine("Render PDF file started...");
            var device = new PdfDevice(options, outputfile);
            var renderer = new HtmlRenderer();
            renderer.Render(device, _document);
            Console.WriteLine("Render PDF file finished.");
 
        }
 
        private static void RenderMirrorPages()
        {            
            var marginsLeft = new Margin(
                Unit.FromMillimeters(30), // left
                Unit.FromMillimeters(10), // top
                Unit.FromMillimeters(15), // right
                Unit.FromMillimeters(20));// bottom
            var marginsRight = new Margin(
                Unit.FromMillimeters(15), // left
                Unit.FromMillimeters(10), // top
                Unit.FromMillimeters(30), // right
                Unit.FromMillimeters(20));// bottom
 
            var options = new PdfRenderingOptions();
            options.PageSetup.SetLeftRightPage(
                new Page(PageSizeA4, marginsLeft),
                new Page(PageSizeA4, marginsRight));
 
            const string outputfile = @"C:\aspose\pdf\output03.pdf";
            Console.WriteLine("Render PDF file started...");
            var device = new PdfDevice(options, outputfile);
            var renderer = new HtmlRenderer();
            renderer.Render(device, _document);
            Console.WriteLine("Render PDF file finished.");
        }
 
        private static void RenderAdvancedPdf()
        {
            var width = Unit.FromMillimeters(210);
            var height = Unit.FromMillimeters(297);
            var pageSizeA4 = new Size(width, height);
            var margins = new Margin(
                Unit.FromMillimeters(15), // left
                Unit.FromMillimeters(10), // top
                Unit.FromMillimeters(15), // right
                Unit.FromMillimeters(20));// bottom
            var options = new PdfRenderingOptions { PageSetup = { AnyPage = new Page(pageSizeA4, margins) } };
            const string outputfile = @"C:\aspose\pdf\output02.pdf";
            Console.WriteLine("Render PDF file started...");
            var device = new PdfDevice(options, outputfile);
            var renderer = new HtmlRenderer();
            renderer.Render(device, _document);
            Console.WriteLine("Render PDF file finished.");
        }
 
        private static void RenderSimplePdf()
        {
            var options = new PdfRenderingOptions();
            Console.WriteLine(options.PageSetup.AnyPage.Margin.Top.IsAuto);
            Console.WriteLine(options.PageSetup.AnyPage.Margin.Bottom.IsAuto);
            Console.WriteLine(options.PageSetup.AnyPage.Margin.Left.IsAuto);
            Console.WriteLine(options.PageSetup.AnyPage.Margin.Right.IsAuto);
            Console.WriteLine(options.PageSetup.AnyPage.Size.Width);
            Console.WriteLine(options.PageSetup.AnyPage.Size.Height);
            const string outputfile = @"C:\aspose\pdf\output01.pdf";            
            Console.WriteLine("Render PDF file started...");
            var device = new PdfDevice(options, outputfile);
            var renderer = new HtmlRenderer();
            renderer.Render(device, _document);
            Console.WriteLine("Render PDF file finished.");
        }
 
        private static void GenerateSampleHtml5Document()
        {
            _document.InsertBefore(_document.CreateDocumentType("html", "", "", ""), _document.DocumentElement);
            _document.DocumentElement.SetAttribute("lang", "en");
            _document.Title = "Page for PDF rendering demo";
 
            var head = _document.GetElementsByTagName("head")[0];
            var metaCharSet = (HTMLMetaElement)_document.CreateElement("meta");
            metaCharSet.SetAttribute("charset", "UTF-8");
 
            var metaViewPort = (HTMLMetaElement)_document.CreateElement("meta");
            metaViewPort.Name = "viewport";
            metaViewPort.Content = "width=device-width, initial-scale=1";
 
            var linkElement1 = (HTMLLinkElement)_document.CreateElement("link");
            linkElement1.Href = "http://cdn.myunv.com/css/uecore.css";
            linkElement1.Rel = "stylesheet";
 
            var linkElement2 = (HTMLLinkElement)_document.CreateElement("link");
            linkElement2.Href = "https://fonts.googleapis.com/css?family=Germania+One";
            linkElement2.Rel = "stylesheet";
 
            var style = (HTMLStyleElement)_document.CreateElement("style");
            //style.TextContent = "h1 {color: red;} h2 {color:green} table { border: 1px dashed black}" +
            //    "h2 {page-break-after: always; } p {font-family: 'Germania One', cursive;}" + "@page { size: 7in 9.25in; margin: 27mm 16mm 27mm 16mm; }";
            style.TextContent = "h1 {color: red;} " +
                                "h2 {color:green} " +
                                "table { border: 1px dashed black}" +
                                "p {font-family: 'Germania One', cursive;}"+
                                "#coverPage {page-break-after: always; }";
 
            
 
            head.AppendChild(metaCharSet);
            head.AppendChild(metaViewPort);
            head.AppendChild(linkElement1);
            head.AppendChild(linkElement2);
            head.AppendChild(style);
 
 
            const string coverPageHtmlContent = "<div id=\"coverPage\"style=\"display: table; height: 15cm; overflow: hidden;\">\r\n" +
                                                "<div style=\"display: table-cell; vertical-align: middle;\">\r\n" +
                                                "<div>\r\n" +
                                                "<h1>Cover Page<h1>\r\n" +
                                                "</div>\r\n" +
                                                "</div>\r\n" +
                                                "</div>";
 
            var coverPageElement = _document.CreateElement("div");
            coverPageElement.InnerHTML = coverPageHtmlContent;
            
 
            var header1 = (HTMLHeadingElement)_document.CreateElement("h1");
            header1.TextContent = "Heading 1";
            var header2 = (HTMLHeadingElement)_document.CreateElement("h2");
            header2.TextContent = "Heading 2";
 
            _document.DocumentElement.LastElementChild.AppendChild(coverPageElement);
            _document.DocumentElement.LastElementChild.AppendChild(header1);
            _document.DocumentElement.LastElementChild.AppendChild(header2);
 
            var colors = new[] { "red", "orange", "black", "green", "blue", "indigo", "violet" };
            var alignment = new[] { "left", "right", "center" };
            for (var i = 0; i < 10; i++)
            {
                var paragraph = (HTMLParagraphElement)_document.CreateElement("p");
                paragraph.Id = $"par{i}";
                paragraph.Style.SetProperty("font-weight", i % 2 == 0 ? "700" : "400", "");
                paragraph.Style.SetProperty("color", colors[i % 7], "");
                paragraph.Style.SetProperty("text-align", alignment[i % 3], "");
                paragraph.TextContent = LoremNET.Lorem.Paragraph(10, 5);
                _document.DocumentElement.LastElementChild.AppendChild(paragraph);
            }
 
            HTMLImageElement img = (HTMLImageElement)_document.CreateElement("img");
            img.Src = "http://lorempixel.com/400/200/sports/1";
            img.Alt = "Placeholder 400x200";
            img.Title = "Placeholder image";
            _document.DocumentElement.LastElementChild.AppendChild(img);
            //Adding an ordered list
            HTMLOListElement orderedListElement = (HTMLOListElement) _document.CreateElement("ol");
            for (var i = 0; i < 10; i++)
            {
                HTMLLIElement listItem = (HTMLLIElement) _document.CreateElement("li");
                listItem.TextContent = $"Item {i + 1}";
                orderedListElement.AppendChild(listItem);
            }
            _document.DocumentElement.LastElementChild.AppendChild(orderedListElement);
 
            // Adding simple table 4x3 
            var table = (HTMLTableElement) _document.CreateElement("table");
            var tBody = (HTMLTableSectionElement) _document.CreateElement("tbody");
            for (var i = 0; i < 4; i++)
            {
                var row = (HTMLTableRowElement) _document.CreateElement("tr");
                row.Id = "trow_" + i;
                for (var j = 0; j < 3; j++)
                {
                    var cell = (HTMLTableCellElement) _document.CreateElement("td") ;
                    cell.Id = $"cell{i}_{j}";
                    cell.TextContent = "Cell " + j;
                    row.AppendChild(cell);
                }
                tBody.AppendChild(row);
            }
            table.AppendChild(tBody);
            _document.DocumentElement.LastElementChild.AppendChild(table);
 
        }
    }
}

Comments