The converter can be configured to automatically generate outlines for a selected list of HTML elements using the PdfDocumentControlOutlines property of the HiQPdfClientPdfAutoOutlines type. A reference to an object of HiQPdfClient.PdfDocumentControl type is given by the Document property of the HiQPdfClient.HtmlToPdf class.
In order to select the HTML elements for which to create outlines automatically you have to set the PdfAutoOutlinesOutlinedElements property with a list of CSS selectors. For example it is possible to select all the HTML elements of a type having a given CSS class.
If your HTML is structured using the heading tags H1 to H6 then you can use the special option PdfAutoOutlinesOutlineHeadingTags to automatically create a hierarchy of outlines based on document structure.
The converter transforms by default all the HTTP links and the internal links found in the HTML document to URI links and internal links in the generated PDF document. To disable the automatically generated links you can set the PdfDocumentControlConvertHttpLinks and PdfDocumentControlConvertInternalLinks properties on false.
The HTML document below contains both internal and HTTP links and the HTML to PDF converter will be configured to automatically generate a table of contents with outlines for chapters.
<html>
<head>
<title>Auto Outlines and Links</title>
</head>
<body>
<h1 class="pdf_outlines">Contents</h1>
<a href="#Chapter1">Go To Chapter 1</a>
<br />
<a href="#Chapter2">Go To Chapter 2</a>
<br />
<a href="#Chapter3">Go To Chapter 3</a>
<br />
<a href="http://www.hiqpdf.com">Visit Website</a>
<h2 class="pdf_outlines" style="page-break-before: always" id="Chapter1">Chapter 1</h2>
This is the chapter 1 content.
<h2 class="pdf_outlines" style="page-break-before: always" id="Chapter2">Chapter 2</h2>
This is the chapter 2 content.
<h2 class="pdf_outlines" style="page-break-before: always" id="Chapter3">Chapter 3</h2>
This is the chapter 3 content.
</body>
</html>
In this demo you learn how to automatically create outlines, HTTP and internal links in PDF and how to force a HTML element to start on a new page in PDF. The demo creates a simple table of contents with internal links to chapters and a HTTP link to HiQPdf website. For each chapter there is also an outline in document outlines and each chapter is forced to start on a new PDF page using the page-break-before : always style.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using HiQPdfClient;
namespace HiQPdf_Demo.Controllers
{
public class AutoOutlinesAndLinksController : Controller
{
// GET: AutoOutlinesAndLinks
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult ConvertToPdf(IFormCollection collection)
{
string serverIP = collection["textBoxServerIP"];
uint serverPort = uint.Parse(collection["textBoxServerPort"]);
string serverPassword = collection["textBoxServerPassword"];
// create the HTML to PDF converter
HtmlToPdf htmlToPdfConverter = new HtmlToPdf(serverIP, serverPort);
// use server password if necessary
if (serverPassword.Length > 0)
htmlToPdfConverter.ServerPassword = serverPassword;
// set a demo serial number
htmlToPdfConverter.SerialNumber = "YCgJMTAE-BiwJAhIB-EhlWTlBA-UEBRQFBA-U1FOUVJO-WVlZWQ==";
if (collection["checkBoxHierarchical"].Count > 0)
{
// automatically outline the H1 to H6 tags in a hierarchy
htmlToPdfConverter.Document.Outlines.OutlineHeadingTags = true;
}
else
{
// create outlines for each chapter and for table of contents
// the CSS selectors mean all the H1 and H2 elements with the CSS class name 'pdf_outlines'
htmlToPdfConverter.Document.Outlines.OutlinedElements = new string[] { "H1[class=\"pdf_outlines\"]", "H2[class=\"pdf_outlines\"]" };
}
// make the outlines visible in viewer
htmlToPdfConverter.Document.Viewer.PageMode = PdfPageMode.Outlines;
// convert the HTML code to PDF
byte[] pdfBuffer = htmlToPdfConverter.ConvertHtmlToMemory(collection["textBoxHtmlCode"], null);
FileResult fileResult = new FileContentResult(pdfBuffer, "application/pdf");
fileResult.FileDownloadName = "AutoOutlines.pdf";
return fileResult;
}
}
}