using System;
using Aspose.Html;
using Aspose.Html.Rendering;
using Aspose.Html.Rendering.Pdf;
namespace Task1047
{
class Program
{
private static HTMLDocument _document;
private static void Main()
{
_document = new HTMLDocument();
GenerateSampleHtml5Document();
RenderEncryptedPdf();
}
private static void RenderEncryptedPdf()
{
var options = new PdfRenderingOptions
{
JpegQuality = 10
};
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\output06.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=Lato";
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: 'Lato', sans-serif;}" +
"#coverPage {page-break-after: always; display: table}";
head.AppendChild(metaCharSet);
head.AppendChild(metaViewPort);
head.AppendChild(linkElement1);
head.AppendChild(linkElement2);
head.AppendChild(style);
const string coverPageHtmlContent = "<div id=\"coverPage\"style=\"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 categories = new[] {"dog", "cat", "bird", "cow"};
for (var i = 0; i < 10; i++)
{
var img = (HTMLImageElement) _document.CreateElement("img");
img.Src = $"http://loremflickr.com/400/200/{categories[i%4]}";
img.Alt = "Placeholder 400x200";
img.Title = "Placeholder image";
_document.DocumentElement.LastElementChild.AppendChild(img);
}
}
}
}
Comments