Partial Page Conversion with Html to Pdf Converter | |
Select.Pdf can render only a specific section of a web page - an image, a table, a table row, a div - instead of the whole page. The section is identified by a CSS selector, set through the VisibleWebElementSelector property. The property is available on every object that drives the rendering engine:
Any selector the browser understands can be used - #invoice for an element id, .infobox for a class, table.wikitable tbody for a nested element. The first match is rendered.
A selector that matches nothing, or that matches an element with no size, fails the conversion with HtmlToPdfException, rather than quietly producing the whole page. |
VisibleWebElementId and the matching properties on the other objects are obsolete. They accept an element id only and are kept so that existing code keeps compiling: setting the id x is the same as setting the selector #x. Reading the property returns the id when the selector is a plain id, and null otherwise - a selector such as .infobox has no id to report. Use the selector property in new code. |
The engine lifts the selected element out of the page and prints it on its own. The text stays real text - it is selectable and searchable in the produced pdf.
The generated document keeps the page size the converter was asked for (PdfPageSize / PdfPageCustomSize, A4 by default), so the section is rendered at the top of a normal page rather than on a sheet trimmed to its height. Margins, orientation and the header/footer bands keep working as usual. To get a document exactly as tall as the section, set PdfPageCustomSize to the size you need. |
// instantiate a html to pdf converter object HtmlToPdf converter = new HtmlToPdf(); // render only the element matched by this css selector converter.Options.VisibleWebElementSelector = "#invoice"; // create a new pdf document converting an url PdfDocument doc = converter.ConvertUrl(url); // save pdf document doc.Save("Sample.pdf"); // close pdf document doc.Close();
On the image path the resulting picture is exactly as large as the selected element.
// instantiate a html to image converter object HtmlToImage converter = new HtmlToImage(); // specify the section that will be rendered converter.VisibleWebElementSelector = "table.wanted"; // render the section - the png contains only that element byte[] image = converter.ConvertUrl(url); System.IO.File.WriteAllBytes("Section.png", image);
PdfHtmlElement renders the section and draws it at a chosen position on a page of a pdf document, keeping the text real. HtmlToImageElement does the same but draws the section as a picture, so its text is not selectable or searchable in the produced pdf.
// create a new pdf document PdfDocument doc = new PdfDocument(); PdfPage page = doc.AddPage(); // render only the given section of the page and draw it at (0, 0) PdfHtmlElement section = new PdfHtmlElement(0, 0, url); section.VisibleWebElementSelector = "#invoice"; page.Add(section); // save pdf document doc.Save("Sample.pdf"); // close pdf document doc.Close();
To keep the page but drop some of its parts, use HiddenWebElements, which hides the elements you name before the page is printed - see Exclude Elements from Conversion.