HiQPdf Documentation

Convert Many HTML Pages into a Single PDF

HiQPdf Client for .NET Core

The HiQPdfClient.HtmlToPdf class can be used to quickly create and save a PDF document to a file, to a stream or in a memory buffer but it will convert only one HTML document to a PDF document. Sometimes it is necessary to add multiple HTML documents to the same PDF document and eventualy overlapping the HTML content in multiple layers. This is possible with HiQPdf software using In Place HTML to PDF Object. The HTML objects are added to a PDF document which can be created by one of the methods described in PDF Documents topic.

A very interesting feature producing very special effects is that the content from a HTML document which doesn't have a background color or image will not have a background when converted to PDF. This makes possible to overlap multiple HTML documents in PDF while leaving visible the content underneath them.

Convert Many HTML Documents into a Single PDF Demo

In this demo you can see how to layout and overlay multiple HTML objects in the same PDF document. The HTML from URL 1 is laid out at the beginning of the PDF document and the URL 2 is laid out immediately after first HTML or on a new page if 'Layout on New Page' option is on. If the second HTML is laid out on a new page then there is the option to change the orientation of the new pages. The HTML code from the text box is finally overlaid in the first page of the PDF document at the given location.

Demo Source Code

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Hosting;

using HiQPdfClient;

namespace HiQPdf_Demo.Controllers
{
    public class ConvertManyHtmlToPdfController : Controller
    {
        IFormCollection m_formCollection;
        IWebHostEnvironment m_hostingEnvironment;
        public ConvertManyHtmlToPdfController(IWebHostEnvironment hostingEnvironment)
        {
            m_hostingEnvironment = hostingEnvironment;
        }

        // GET: ConvertManyHtmlToPdf
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult ConvertToPdf(IFormCollection collection)
        {
            m_formCollection = collection;

            string serverIP = collection["textBoxServerIP"];
            uint serverPort = uint.Parse(collection["textBoxServerPort"]);
            string serverPassword = collection["textBoxServerPassword"];

            // create an empty PDF document
            PdfDocument document = new PdfDocument(serverIP, serverPort);

            // use server password if necessary
            if (serverPassword.Length > 0)
                document.ServerPassword = serverPassword;

            // set a demo serial number
            document.SerialNumber = "YCgJMTAE-BiwJAhIB-EhlWTlBA-UEBRQFBA-U1FOUVJO-WVlZWQ==";

            // add a page to document
            PdfPage page1 = document.AddPage(PdfPageSize.A4, new PdfMargins(5), PdfPageOrientation.Portrait);

            try
            {
                // set the document header and footer before adding any objects to document
                SetHeader(document);
                SetFooter(document);

                // layout the HTML from URL 1
                PdfHtml html1 = new PdfHtml(collection["textBoxUrl1"]);
                html1.WaitBeforeConvert = 2;
                page1.Layout(html1);

                if (collection["checkBoxNewPage"].Count > 0)
                {
                    // URL 2 is laid out on a new page with the selected orientation
                    PdfPage page2 = document.AddPage(PdfPageSize.A4, new PdfMargins(5), GetSelectedPageOrientation());

                    // layout the HTML from URL 2
                    PdfHtml html2 = new PdfHtml(collection["textBoxUrl2"]);
                    html2.WaitBeforeConvert = 2;
                    page2.Layout(html2);
                }
                else
                {
                    // URL 2 is laid out immediately after URL 1
                    // using the document realtive flow layout
                    PdfHtml html2 = new PdfHtml(collection["textBoxUrl2"]);
                    html2.WaitBeforeConvert = 2;
                    document.Layout(html2);
                }

                // write the PDF document to a memory buffer
                byte[] pdfBuffer = document.WriteToMemory();

                FileResult fileResult = new FileContentResult(pdfBuffer, "application/pdf");
                fileResult.FileDownloadName = "ConvertManyHtmlToPdf.pdf";

                return fileResult;
            }
            finally
            {
                document.Close();
            }
        }

        private void SetHeader(PdfDocument document)
        {
            if (m_formCollection["checkBoxAddHeader"].Count == 0)
                return;

            // create the document header
            document.CreateHeaderCanvas(50);

            // add PDF objects to the header canvas
            string headerImageFile = m_hostingEnvironment.WebRootPath + "/DemoFiles/Images/HiQPdfLogo.png";
            PdfImage logoHeaderImage = new PdfImage(5, 5, 40, headerImageFile);
            document.Header.Layout(logoHeaderImage);

            // layout HTML in header
            PdfHtml headerHtml = new PdfHtml(50, 5, @"<span style=""color:Navy; font-family:Times New Roman; font-style:italic"">
                            Quickly Create High Quality PDFs with </span><a href=""http://www.hiqpdf.com"">HiQPdf</a>", null);
            headerHtml.FitDestHeight = true;
            headerHtml.FontEmbedding = true;
            document.Header.Layout(headerHtml);

            PdfPage startPage = document.Pages[0];
            float headerWidth = startPage.Size.Width - startPage.Margins.Left - startPage.Margins.Right;
            float headerHeight = document.Header.Height;

            // create a border for header
            PdfRectangle borderRectangle = new PdfRectangle(1, 1, headerWidth - 2, headerHeight - 2);
            borderRectangle.LineStyle.LineWidth = 0.5f;
            borderRectangle.ForeColor = PdfColor.Navy;
            document.Header.Layout(borderRectangle);
        }

        private void SetFooter(PdfDocument document)
        {
            if (m_formCollection["checkBoxAddFooter"].Count == 0)
                return;

            //create the document footer
            document.CreateFooterCanvas(50);

            // layout HTML in footer
            PdfHtml footerHtml = new PdfHtml(5, 5, @"<span style=""color:Navy; font-family:Times New Roman; font-style:italic"">
                            Quickly Create High Quality PDFs with </span><a href=""http://www.hiqpdf.com"">HiQPdf</a>", null);
            footerHtml.FitDestHeight = true;
            footerHtml.FontEmbedding = true;
            document.Footer.Layout(footerHtml);

            PdfPage startPage = document.Pages[0];
            float footerWidth = startPage.Size.Width - startPage.Margins.Left - startPage.Margins.Right;
            float footerHeight = document.Footer.Height;

            // add page numbering
            PdfFont pageNumberingFont = new PdfFont("Times New Roman", 8, true);
            PdfText pageNumberingText = new PdfText(5, footerHeight - 12, "Page {CrtPage} of {PageCount}", pageNumberingFont);
            pageNumberingText.HorizontalAlign = PdfTextHAlign.Center;
            pageNumberingText.EmbedSystemFont = true;
            pageNumberingText.ForeColor = PdfColor.DarkGreen;
            document.Footer.Layout(pageNumberingText);

            string footerImageFile =m_hostingEnvironment.WebRootPath + "/DemoFiles/Images/HiQPdfLogo.png";
            PdfImage logoFooterImage = new PdfImage(footerWidth - 40 - 5, 5, 40, footerImageFile);
            document.Footer.Layout(logoFooterImage);

            // create a border for footer
            PdfRectangle borderRectangle = new PdfRectangle(1, 1, footerWidth - 2, footerHeight - 2);
            borderRectangle.LineStyle.LineWidth = 0.5f;
            borderRectangle.ForeColor = PdfColor.DarkGreen;
            document.Footer.Layout(borderRectangle);
        }

        private PdfPageOrientation GetSelectedPageOrientation()
        {
            return m_formCollection["dropDownListPageOrientations"] == "Portrait" ?
                PdfPageOrientation.Portrait : PdfPageOrientation.Landscape;
        }
    }
}
See Also

Other Resources