Click or drag to resize

Exclude Elements, Convert a Part of the Page

Parts of a page that make no sense on paper - menus, buttons, banners, forms - can be left out of the PDF, and a single part of a page can be converted on its own.

Excluding elements

With the Chromium and Blink engines, hide the elements with CSS: the CustomCSS property adds styles to the page before the conversion (a print style sheet of the page, used with media print, works too).

PdfConverter converter = new PdfConverter();
converter.RenderingEngine = RenderingEngine.Chromium;
converter.CustomCSS = "nav, .no-pdf, #comments { display: none !important; }";
converter.SavePdfFromUrlToFile("https://www.example.com/article", "article.pdf");

With the WebKit engine, list the elements to exclude in HtmlElementSelectors (property HtmlExcludedRegionsOptions): "img" selects all images, "*.myclass" the elements with the CSS class myclass, "*#myid" the element with the id myid.

PdfConverter converter = new PdfConverter();
converter.RenderingEngine = RenderingEngine.WebKit;
converter.HtmlExcludedRegionsOptions.HtmlElementSelectors = new string[] { "*#menu", "*.no-pdf", "form" };
converter.SavePdfFromUrlToFile("https://www.example.com/article", "article.pdf");
Converting a part of the page

RenderElementSelector converts only the element matched by a CSS selector - an article, an invoice, a table (Chromium and Blink engines). RenderElementId does the same for an element id, and is also supported by the WebKit engine. When no element matches, or the element has no visible size, the Chromium and Blink engines fail the conversion with an error instead of converting the whole page.

PdfConverter converter = new PdfConverter();
converter.RenderingEngine = RenderingEngine.Chromium;
converter.RenderElementSelector = "main article";
converter.SavePdfFromUrlToFile("https://www.example.com/article", "article-only.pdf");
See Also