Click or drag to resize
Pdf Library for .NET

Pdf Html Element

HTML content can be added to a pdf document in several ways. If a complex document is created that will contain several types of pdf elements and the html content that will be added is only a small part of the total content, PdfHtmlElement objects can be used to convert web pages or raw html content and add the conversion result to the pdf document.

If the pdf is generated mostly as a result of an html to pdf conversion, the HtmlToPdf object should be used instead. This is described in details in the "Html to Pdf Converter" section of our documentation that starts here.

Element properties

Both full web pages and html strings can be converted to pdf using PdfHtmlElement with the appropriate constructor. PdfHtmlElement(String) can be used to convert a full web page specified by an url or local file to pdf. PdfHtmlElement(String, String) can be used to convert a raw html string to pdf.

Properties

Authentication - Handles authentication options if the web page being converted requires authentication.

CssMediaType - Indicates what css styles are used when the web page is rendered.

DrawBackground - Gets or sets a flag indicating if the web page background is rendered in pdf.

HiddenWebElements - Gets a reference to the object that controls the visibility of some web elements in the generated pdf document.

HttpCookies - Gets the collection of custom HTTP cookies used for the conversion.

HttpHeaders - Get the collection of custom HTTP headers used for the conversion.

JavaScriptEnabled - Enable scripts when rendering the url or html string.

NavigationTimeout - The time in seconds the converter waits for the web page to load.

ConversionDelay - An additional time in seconds the converter waits after the web page has loaded, before it is rendered.

PdfBookmarkOptions - Gets a reference to the object that controls the bookmarks creation for the generated pdf document.

ProxyOptions - Gets a reference to an object containing the proxy settings used to access the web page that is being converted.

StartupMode - Use this property to specify how the conversion starts.

VisibleWebElementSelector - A css selector. When it is set, only the matched element of the page is rendered by this element, instead of the whole page - see Partial Page Conversion. VisibleWebElementId is the obsolete form of the same option and accepts an element id only.

WebPageWidth - Gets or sets the width of the converted web page as it would appear in the internal browser used to render the html.

WebPageHeight - Gets or sets the height of the converted web page as it would appear in the internal browser used to render the html.

WebPageFixedSize - Controls whether the content below WebPageHeight is left out.

WebPageInformation - Gets an object populated after conversion with the title, keywords and description of the converted web page.

Size, pagination and the pages the element creates

Width and Height default to -1, which means "as much as there is": the element fills the width available to the right of its X and the height available below its Y. Give either of them a value to place the render in a rectangle of your own.

An element with an automatic Height renders the whole html, wherever it starts: its first page holds what fits in the room left below Y, the pages after it are full pages, and the last one ends where the content ends - so an element added under it can start right there. Set AllowContentHeightResize to false to draw every page of the render at its full height instead.

Where the content ends is measured by the rendering engine after it prints the page (Ink, the default), so everything the html paints is kept - a shadow, a positioned decoration - together with the space its last element leaves below itself. Set ManagedContentTrimmingMode to Marker to end the last page where the page's layout ends, the way earlier versions did, or ManagedContentTrimming to false to let the engine shorten its own page instead.

A fixed Height that does not fit in what is left of the page continues on the pages that follow and stops once that much height has been drawn.

Content that continues uses the page that follows when the document already has one, and a new page is added only when it does not - so two elements that both overflow the same page share the pages the first one created instead of each adding a run of its own. A page added this way carries the features of the page it continues: size, orientation, margins and rotation.

After the element has been added, PdfPagesRectangles reports where each piece of the render was drawn, and the PdfRenderingResult returned by Add(PdfPageElement) gives the last page and rectangle - which is what you chain the next element off.

Note  Note

The html is rendered in a virtual browser WebPageWidth pixels wide (1024 by default) and the result is scaled to fit Width. For a narrow rectangle - a card, a column - that scale makes body text unreadably small, so set WebPageWidth to roughly Width * 4 / 3 instead (for example 400 for a 250pt wide rectangle).

A PdfHtmlElement can also be added to a PdfTemplate - a header, a footer or a stamp - in which case it renders once and the band is repeated on every page the template covers. Nothing paginates there, since a template is one piece of content stamped on many pages. See Pdf Templates.

Sample Code

Sample code that shows how to use PdfHtmlElement object to convert a web page to pdf.

// create a new pdf document
PdfDocument doc = new PdfDocument();

// add a new page to the document
PdfPage page = doc.AddPage();

// create html element 
PdfHtmlElement html = new PdfHtmlElement(url);

// add the html element to the document
page.Add(html);

// save pdf document
doc.Save("Sample.pdf");

// close pdf document
doc.Close();
See Also