RenderPage1.cs
//
// This code is part of GrapeCity Documents for PDF samples.
// Copyright (c) GrapeCity, Inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using GrapeCity.Documents.Pdf;
using GrapeCity.Documents.Text;
using GrapeCity.Documents.Html;

namespace GcPdfWeb.Samples
{
    // 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
    {
        public void CreatePDF(Stream stream)
        {
            // Get a temporary file where the web page will be rendered:
            var tmp = Path.GetTempFileName();
            // The Uri of the web page to render:
            var uri = new Uri(@"https://www.grapecity.com/documents-api-pdf/demos/view-source-cs/WordIndex/");

            // 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 (var 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.
                var pdfSettings = new PdfSettings()
                {
                    PageRanges = "2-100",
                    PageWidth = 8.5f,
                    PageHeight = 11f,
                    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>" +
                        "</div>",
                    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);
            }
            // Copy the created PDF from the temp file to target stream:
            using (var ts = File.OpenRead(tmp))
                ts.CopyTo(stream);
            // Clean up:
            File.Delete(tmp);
            // Done.
        }
    }
}