Click or drag to resize
Pdf Library for .NET

Partial Page Conversion with Html to Pdf Converter

Select.Pdf Html to Pdf Converter for .NET can be used to convert only a specific section of a web page to pdf.

The section that will be converted to pdf is specified by a CSS selector, set with the VisibleWebElementSelector property of the HtmlToPdfOptions object. The element can be anything (image, table, table row, div, text, etc), and any selector the rendering engine understands can be used - "#content", ".infobox", "table.data tbody".

VisibleWebElementId selects the element by its id instead. Setting it is the same as setting the selector to that id, and it is the form every rendering engine honors - the WebKit engines match the id themselves and cannot take a selector, so a page converted with those has to use the id.

Note  Note

All four rendering engines apply this property, for HTML to PDF and for HTML to image alike. Support for the Chromium and Blink engines was added in v26.4 - with older versions those two engines ignored the property for HTML to PDF and converted the whole page. The selected section is rendered alone, at the top left corner of the first page, and the rest of the page is left empty; a section taller than one page continues on the following pages. When the page has no element with the specified id, Chromium and Blink report an error, while the WebKit engines convert the whole page. For HTML to image conversion, the equivalent properties on HtmlToImage are supported by all four rendering engines.

A CSS selector that is not a plain element id is honored by the Chromium and Blink engines. The WebKit engines match an element id themselves and cannot take a selector: they report an error naming the selector instead of converting the whole page.

Sample Code

This sample code shows how the html to pdf converter can be used to convert only a section of a web page using Select.Pdf Pdf Library for .NET.

// instantiate a html to pdf converter object
HtmlToPdf converter = new HtmlToPdf();

// specify the id of the section that will be converted to pdf
converter.Options.VisibleWebElementId = elementId;

// create a new pdf document converting an url
PdfDocument doc = converter.ConvertUrl(url);

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

// close pdf document
doc.Close();
Partial Conversion with the Chromium and Blink Rendering Engines

The code is the same when the page is rendered with the Chromium or the Blink engine - only the rendering engine selection is added. The section identified by the element id is rendered alone, at the top of the page.

// instantiate a html to pdf converter object
HtmlToPdf converter = new HtmlToPdf();

// render the page with the Chromium engine (RenderingEngine.Blink works the same way)
converter.Options.RenderingEngine = RenderingEngine.Chromium;

// specify the id of the section that will be converted to pdf
converter.Options.VisibleWebElementId = elementId;

// create a new pdf document converting an url
PdfDocument doc = converter.ConvertUrl(url);

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

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