HTML to Image Converter Element |
The HtmlToImageElement graphic element renders a web page or an HTML string to an image with the Chromium engine and draws that image into a page or a template of a PDF document. Where an HTML to PDF element produces selectable text, links and vector graphics, this element produces a picture of the page. You can choose the position and the size of the image in the PDF page and add many HTML to image conversions to the same document.
When the image spans several pages, the AddElementResult object returned after the element is added to a page or template tells the index of the last page and the rectangle the image occupies on it.
The HtmlToImageElement offers many constructors that call the following two constructors with default values for converting a URL or an HTML string to an image:
public HtmlToImageElement(float x, float y, float width, float height, string urlToConvert, int htmlViewerWidth, int htmlViewerHeight); public HtmlToImageElement(float x, float y, float width, float height, string htmlStringToConvert, string htmlStringBaseURL, int htmlViewerWidth, int htmlViewerHeight);
The first constructor creates a URL to image element at the specified x and y coordinates with the specified width and height. The width and height in pixels of the browser window the page is laid out in are given by the htmlViewerWidth and htmlViewerHeight parameters.
x - The x position in points where the image is placed.
y - The y position in points where the image is placed.
width - The destination width in points for the image. If the width is negative, the destination width is the width available in the page or template.
height - The destination height in points for the image. If the height is negative, the height is determined automatically from the width and the proportions of the image.
urlToConvert - The URL to convert.
htmlViewerWidth - The browser window width in pixels. The default value is 1024 pixels. The effect of this parameter is the same as viewing the HTML page in a browser window with the specified width.
htmlViewerHeight - The browser window height in pixels. A negative value, the default, captures the whole page; a positive value captures only that height of the page.
The second constructor creates an HTML string to image element at the specified x and y coordinates with the specified width and height.
htmlStringToConvert - The HTML string to convert.
htmlStringBaseURL - The full URL of the page the string was taken from, used to resolve the images and CSS files referenced by a relative URL in the HTML string. When this parameter is null, no base URL is used.
FitWidth (true by default) shrinks the image to the available width when it is wider, StretchToFit also enlarges a narrower image to that width, and FitHeight shrinks the image to the available height. A tall image continues on the next pages while Paginate is true (the default). GetImage returns the image as the bytes of a PNG file.
The code below draws the image of an HTML fragment, 300 points wide, at the top of a new page:
using (Document document = new Document()) { PdfPage page = document.AddPage(); // the HTML is laid out in a 600 pixels wide window and drawn 300 points wide HtmlToImageElement card = new HtmlToImageElement(0, 0, 300, "<div style='padding:20px; background:#eef; font-size:24px'>" + "Account summary</div>", null, 600); page.AddElement(card); document.Save("card.pdf"); }