The Document property of the HiQPdf.HtmlToPdf class having the PdfDocumentControl type is used to control most of the properties of the generated PDF document like page size, page orientation, page margins, header and footer, outlines, font embedding, the document standard and the document security.
The PageSize property of the HiQPdf.PdfDocumentControl class controls the generated PDF document page size. The default value of this property is A4. The final width and height of the PDF pages is also influenced by the PageOrientation property. For example, if the initial page orientation was portrait and then it is set to landscape then the width of and height sizes are swapped, the width becoming bigger than the height.
The PageOrientation property of the HiQPdf.PdfDocumentControl class controls the generated PDF document page orientation. The default value of this property is Portrait.
The Margins property of the HiQPdf.PdfDocumentControl class controls the generated PDF document page margins. By default the generated document margins are all 0.
The DestX, DestY, DestWidth, DestWidth properties of the HiQPdf.PdfDocumentControl class control the location and the size of the HTML content in PDF document. By default the HTML content is rendered in top left corner of the PDF page and uses the entire available PDF page width.
There are a few properties controlling how the rendered HTML is resized to fit the PDF page dimensions or how the PDF pages are automatically resized to display the HTML content.
The FitPageWidth property of the HiQPdf.PdfDocumentControl class controls if the content is scaled down to fit the PDF page width. This property is true by default.
The HTML width is given by the BrowserWidth property of the HiQPdf.HtmlToPdf class which is 1200 pixels by default. At a default DPI of 96 the HTML width is 12.5 inch which is larger than the default A4 portrait page width and the HTML is scaled down to fit the PDF page which means that the text and images might appear smaller than they are in HTML.
If the FitPageWidth property is set to false the HTML is not scaled down and by default only a part of the 1200 pixels of HTML page would be displayed in a A4 portrait page. But when FitPageWidth is false another property ResizePageWidth comes into play. By default the ResizePageWidth is true and the PDF page width can be resized to display the whole HTML without scaling.
The FitPageHeight property of the HiQPdf.PdfDocumentControl class controls if the content is scaled down to fit the PDF page height. This property is false by default.
If the FitPageWidth property is false, the PostCardMode property of the HiQPdf.PdfDocumentControl class controls if the content will be rendered in one PDF page resized to display the entire HTML. This property is false by default.
There are also a few properties of the HTML converters controlling the internal browser behavior. The most important are described in the sections below.
The BrowserWidth property of the HiQPdf.HtmlToPdf class is a property having a very high influence against the HTML content rendering. Changing this property is basically the equivalent of resizing a web browser window when viewing a web page. By default the Browser Width is 1200 pixels and this is suitable for displaying most of the web pages.
The TrimToBrowserWidth controls if the browser window width is forced to have the width given by the BrowserWidth property. If the HTML document cannot be entirely displayed in BrowserWidth pixels and the TrimToBrowserWidth is false then the browser window width will be automatically set to display the entire HTML document. If the HTML document cannot be entirely displayed in BrowserWidth pixels and the TrimToBrowserWidth is true then the browser window width will not be automatically set to display the entire HTML document and the HTML document will be trimmed. The default value of this property is false.
In this demo you can see how to convert an URL, a local file or a HTML code to PDF. You can control the PDF page size and orientation, PDF page margins, browser width and height, HTML content location, HTML content fitting and scaling in PDF page.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using HiQPdf;
namespace HiQPdf_Demo.Controllers
{
public class HtmlFittingAndScalingOptionsController : Controller
{
IFormCollection m_formCollection;
// GET: HtmlFittingAndScalingOptions
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult ConvertToPdf(IFormCollection collection)
{
m_formCollection = collection;
// create the HTML to PDF converter
HtmlToPdf htmlToPdfConverter = new HtmlToPdf();
// set a demo serial number
htmlToPdfConverter.SerialNumber = "YCgJMTAE-BiwJAhIB-EhlWTlBA-UEBRQFBA-U1FOUVJO-WVlZWQ==";
// set browser width
htmlToPdfConverter.BrowserWidth = int.Parse(collection["textBoxBrowserWidth"]);
// set browser height if specified, otherwise use the default
if (collection["textBoxBrowserHeight"][0].Length > 0)
htmlToPdfConverter.BrowserHeight = int.Parse(collection["textBoxBrowserHeight"]);
// set HTML Load timeout
htmlToPdfConverter.HtmlLoadedTimeout = int.Parse(collection["textBoxLoadHtmlTimeout"]);
// optionally wait an additional time before starting the conversion
// it is not necessary to set this property if the HTML page is not updated after initial load
htmlToPdfConverter.WaitBeforeConvert = 2;
// set PDF page size and orientation
htmlToPdfConverter.Document.PageSize = GetSelectedPageSize();
htmlToPdfConverter.Document.PageOrientation = GetSelectedPageOrientation();
// set PDF page size and orientation
htmlToPdfConverter.Document.PageSize = GetSelectedPageSize();
htmlToPdfConverter.Document.PageOrientation = GetSelectedPageOrientation();
// set PDF page margins
htmlToPdfConverter.Document.Margins = new PdfMargins(
int.Parse(collection["textBoxLeftMargin"]), int.Parse(collection["textBoxRightMargin"]),
int.Parse(collection["textBoxTopMargin"]), int.Parse(collection["textBoxBottomMargin"]));
// set HTML location and size in PDF page
if (collection["textBoxHtmlLeftLocation"][0].Length > 0)
htmlToPdfConverter.Document.DestX = float.Parse(collection["textBoxHtmlLeftLocation"]);
if (collection["textBoxHtmlTopLocation"][0].Length > 0)
htmlToPdfConverter.Document.DestY = float.Parse(collection["textBoxHtmlTopLocation"]);
if (collection["textBoxHtmlWidth"][0].Length > 0)
htmlToPdfConverter.Document.DestWidth = float.Parse(collection["textBoxHtmlWidth"]);
if (collection["textBoxHtmlHeight"][0].Length > 0)
htmlToPdfConverter.Document.DestHeight = float.Parse(collection["textBoxHtmlHeight"]);
// control if the HTML can be scaled to fit the PDF page width
htmlToPdfConverter.Document.FitPageWidth = collection["checkBoxFitPageWidth"].Count > 0;
// control if the HTML can be enlarged to fit the PDF page width when FitPageWidth is true
htmlToPdfConverter.Document.ForceFitPageWidth = collection["checkBoxForceFitPageWidth"].Count > 0;
// control if the PDF page can be resized to display the whole HTML content when FitPageWidth is false
htmlToPdfConverter.Document.ResizePageWidth = collection["checkBoxResizePdfPage"].Count > 0;
// control if the HTML content can be scaled to fit the PDF page height
htmlToPdfConverter.Document.FitPageHeight = collection["checkBoxFitPageHeight"].Count > 0;
// control if the whole HTML content will be rendered in one PDF page without scaling
htmlToPdfConverter.Document.PostCardMode = collection["checkBoxPostCardMode"].Count > 0;
// convert HTML to PDF
byte[] pdfBuffer = null;
if (collection["UrlOrHtmlCode"] == "radioButtonConvertUrl")
{
// convert URL to a PDF memory buffer
string url = collection["textBoxUrl"];
pdfBuffer = htmlToPdfConverter.ConvertUrlToMemory(url);
}
else
{
// convert HTML code
string htmlCode = collection["textBoxHtmlCode"];
string baseUrl = collection["textBoxBaseUrl"];
// convert HTML code to a PDF memory buffer
pdfBuffer = htmlToPdfConverter.ConvertHtmlToMemory(htmlCode, baseUrl);
}
FileResult fileResult = new FileContentResult(pdfBuffer, "application/pdf");
fileResult.FileDownloadName = "HtmlFittingAndScaling.pdf";
return fileResult;
}
private PdfPageSize GetSelectedPageSize()
{
switch (m_formCollection["dropDownListPageSizes"])
{
case "A0":
return PdfPageSize.A0;
case "A1":
return PdfPageSize.A1;
case "A10":
return PdfPageSize.A10;
case "A2":
return PdfPageSize.A2;
case "A3":
return PdfPageSize.A3;
case "A4":
return PdfPageSize.A4;
case "A5":
return PdfPageSize.A5;
case "A6":
return PdfPageSize.A6;
case "A7":
return PdfPageSize.A7;
case "A8":
return PdfPageSize.A8;
case "A9":
return PdfPageSize.A9;
case "ArchA":
return PdfPageSize.ArchA;
case "ArchB":
return PdfPageSize.ArchB;
case "ArchC":
return PdfPageSize.ArchC;
case "ArchD":
return PdfPageSize.ArchD;
case "ArchE":
return PdfPageSize.ArchE;
case "B0":
return PdfPageSize.B0;
case "B1":
return PdfPageSize.B1;
case "B2":
return PdfPageSize.B2;
case "B3":
return PdfPageSize.B3;
case "B4":
return PdfPageSize.B4;
case "B5":
return PdfPageSize.B5;
case "Flsa":
return PdfPageSize.Flsa;
case "HalfLetter":
return PdfPageSize.HalfLetter;
case "Ledger":
return PdfPageSize.Ledger;
case "Legal":
return PdfPageSize.Legal;
case "Letter":
return PdfPageSize.Letter;
case "Letter11x17":
return PdfPageSize.Letter11x17;
case "Note":
return PdfPageSize.Note;
default:
return PdfPageSize.A4;
}
}
private PdfPageOrientation GetSelectedPageOrientation()
{
return (m_formCollection["dropDownListPageOrientations"] == "Portrait") ?
PdfPageOrientation.Portrait : PdfPageOrientation.Landscape;
}
}
}