HtmlSettings.vb
''
'' This code is part of Document Solutions for PDF demos.
'' Copyright (c) MESCIUS inc. All rights reserved.
''
Imports System.IO
Imports System.Drawing
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Html

'' This sample shows how to render a web page specified by a URL
'' to a PDF. Here we render the C# source of the WordIndex sample.
'' Similar to HtmlRenderPage0, this sample directly uses GcHtmlRenderer,
'' but shows how to specify options such as page orientation, margins,
'' headers and footers.
''
'' For reference, the following markup extensions can be used in the
'' header and title templates (all except title are used in this sample):
'' - <span class="date">       - Formatted date
'' - <span class="title">      - Document title
'' - <span class="url">        - Document location
'' - <span class="pageNumber"> - Current page number
'' - <span class="totalPages"> - Total number of pages
''
'' Note that in headers/footers images cannot be specified as links,
'' but base64 encoded data is supported as shown in this sample.
''
'' Also note that for background colors in headers/footers to appear,
'' the following webkit CSS extension should be specified:
'' -webkit-print-color-adjust: exact;
''
'' Please see notes in comments at the top of HelloWorldHtml
'' sample code for details on adding DsHtml to your projects.
Public Class HtmlSettings
    Sub CreatePDF(ByVal stream As Stream)
        '' Get a temporary file where the web page will be rendered:
        Dim tmp = Path.GetTempFileName()
        '' The Uri of the web page to render:
        Dim uri = New Uri("https://developer.mescius.com/document-solutions/dot-net-pdf-api/demos/view-source-cs/word-index/")
        '' Image used in the footer template:
        Dim image = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFAAAABQCAYAAACOEfKtAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAF8WlUWHRYTUw6Y29tLmFkb2JlLnhtcAAAAAAAPD94cGFja2V0IGJlZ2luPSLvu78iIGlkPSJXNU0wTXBDZWhpSHpyZVN6TlRjemtjOWQiPz4gPHg6eG1wbWV0YSB4bWxuczp4PSJhZG9iZTpuczptZXRhLyIgeDp4bXB0az0iQWRvYmUgWE1QIENvcmUgNS42LWMxNDggNzkuMTY0MDM2LCAyMDE5LzA4LzEzLTAxOjA2OjU3ICAgICAgICAiPiA8cmRmOlJ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XofouTcKybJzwWrEQ7iLjOEm5mRbgGuAGYCn5moJxO+RBS5vQQRopgCK1L6HyKqWF5DgRN2luhGBMHjgC1mK7NyT+QFwI+oU2UWHuvV8ZSJOhJg9aaShqiCNAxYV0S5G4FRiGrnud0JKxB2VZk8CFyEqBBSV4CcBkTFtbkFyskbdpfpfBdhOUvIzXI0RM2/GuBtE/qVNewm8DieQCypnsii7TOAD/i1if3JGgOFQIAwwhLHAU8isj89vf/qEMRNAm5AVJQbEPgfbZlv3OD+pJUAAAAASUVORK5CYII="

        '' Create an instance of GcHtmlBrowser that is used to render HTML:
        Using browser = Util.NewHtmlBrowser()
            '' PdfOptions specifies options for HTML to PDF conversion:
            '' - PageRanges specifies which pages to include.
            '' - PageWidth/PageHeight allow customizing page size (defaults are used here for demo).
            '' - Margins specify page margins (the default is no margins).
            '' - Landscape allows changing page orientation.
            '' - Scale allows enlarging or reducing the render size (default is 1).
            '' - To add custom headers, DisplayHeaderFooter needs to be set to true.
            '' - HeaderTemplate/FooterTemplate allow you to specify custom page headers.
            Dim pdfOptions = New PdfOptions() With
            {
                .PageRanges = "2-100",
                .PageWidth = 8.5F,
                .PageHeight = 11.0F,
                .Margins = New PdfMargins(0.2F, 1, 0.2F, 1),
                .Landscape = True,
                .DisplayHeaderFooter = True,
                .HeaderTemplate = "<div style='-webkit-print-color-adjust:exact;background-color:#395daa;color:white;" +
                    "padding:0.1in;font-size:12em;width:1000px;margin-left:0.2in;margin-right:0.2in'>" +
                    "<span style='float:left'>Page <span class='pageNumber'></span> of <span class='totalPages'></span></span>" +
                    "<span style='float:right'>Document created on <span class='date'></span>" +
                    "</div>",
                .FooterTemplate = "<div style='font-size:12em;width:1000px;margin-left:0.2in;margin-right:0.2in'>" +
                    $"<span>Document location: <span class='url'></span><img style='float:right;' width='40' height='40' src='{image}'></img></div>"
            }
            '' Render the source Web page to the temporary file:
            Using htmlPage = browser.NewPage(uri)
                htmlPage.SaveAsPdf(tmp, pdfOptions)
            End Using
        End Using
        '' Copy the created PDF from the temp file to target stream:
        Using ts = File.OpenRead(tmp)
            ts.CopyTo(stream)
        End Using
        '' Clean up:
        File.Delete(tmp)
        '' Done.
    End Sub
End Class