'' This code is part of GrapeCity Documents for PDF samples.
'' Copyright (c) GrapeCity, 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
'' Please see notes in comments at the top of HelloWorldHtml
'' sample code for details on adding GcHtml to your projects.
Public Class RenderPage1
    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("")

        '' Create a GcHtmlRenderer with the source Uri
        '' (note that GcHtmlRenderer ctor and other HTML rendering methods accept either a Uri
        '' specifying the HTML page to render, or a string which represents the actual HTML):
        Using re = New GcHtmlRenderer(uri)
            '' PdfSettings allow to provide options for HTML to PDF conversion:
            '' - PageRanges allows to skip the first page which is empty in this case.
            '' - PageWidth/PageHeight allow to customize page size (defaults are used here for demo).
            '' - Margins specify page margins (the default is no margins).
            '' - IgnoreCSSPageSize makes sure page size specified here is used.
            '' - Landscape allows to change page orientation.
            '' - Scale allows to enlarge or reduce the render size (default is 1).
            '' - To add custom headers, DisplayHeaderFooter needs to be set to true.
            '' - HeaderTemplate/FooterTemplate allow to specify custom page headers.
            Dim pdfSettings = New PdfSettings() With
                .PageRanges = "2-100",
                .PageWidth = 8.5F,
                .PageHeight = 11.0F,
                .Margins = New Margins(0.2F, 1, 0.2F, 1),
                .IgnoreCSSPageSize = True,
                .Landscape = True,
                .DisplayHeaderFooter = True,
                .HeaderTemplate = "<div style='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>" +
                .FooterTemplate = "<div style='font-size:12em;width:1000px;margin-left:0.2in;margin-right:0.2in'>" +
                    "<span>Document location: <span class='url'></span></div>"
            '' Render the source Web page to the temporary file:
            re.RenderToPdf(tmp, pdfSettings)
        End Using
        '' Copy the created PDF from the temp file to target stream:
        Using ts = File.OpenRead(tmp)
        End Using
        '' Clean up:
        '' Done.
    End Sub
End Class