Click or drag to resize

Advanced Post Convert Customization of the Generated PDF Document

ExpertPdf Anywhere converts web pages to PDF, and it is also a PDF library: the document produced by a conversion can be changed before it is saved, existing PDF documents can be loaded and edited, several documents can be merged into one, and pages, texts, images and shapes can be added to a document.

The PdfConverter class offers a set of methods that return the result of the HTML to PDF conversion as a Document object instead of the bytes of a PDF file:

public Document GetPdfDocumentObjectFromUrl(string url);
public Document GetPdfDocumentObjectFromUrl(string url, string internalLinksDocUrl);
public Document GetPdfDocumentObjectFromHtmlFile(string htmlFilePath);
public Document GetPdfDocumentObjectFromHtmlFile(string htmlFilePath,
    string internalLinksDocUrl);
public Document GetPdfDocumentObjectFromHtmlString(string htmlString);
public Document GetPdfDocumentObjectFromHtmlString(string htmlString,
    string urlBase);
public Document GetPdfDocumentObjectFromHtmlString(string htmlString,
    string urlBase, string internalLinksDocUrl);
public Document GetPdfDocumentObjectFromHtmlStream(System.IO.Stream htmlStream,
    Encoding streamEncoding);
public Document GetPdfDocumentObjectFromHtmlStream(System.IO.Stream htmlStream,
    Encoding streamEncoding, string urlBase);
public Document GetPdfDocumentObjectFromHtmlStream(System.IO.Stream htmlStream,
    Encoding streamEncoding, string urlBase, string internalLinksDocUrl);

GetPdfDocumentObjectFromUrlAsync and GetPdfDocumentObjectFromHtmlStringAsync do the same without blocking the calling thread, and take a cancellation token.

The returned object is an instance of the Document class, which gives access to the pages of the PDF document. You can iterate through the document pages, add new pages to the document, append other PDF documents, or add new elements like texts and images to the document pages. After the changes, the document is saved to a file, to a stream or to a byte array with one of the Save methods of the Document class. The document implements IDisposable: dispose it, or call its Close method, when you no longer need it. The classes used to change the generated PDF document are in the ExpertPdf.Anywhere.PdfDocument namespace.

When the Document object is returned by one of the methods above, it already contains the PDF pages generated by the converter from the URL or from the HTML string. The pages are in the Pages collection. New pages with the desired size, orientation and margins can be added to the collection and new elements can be added to any page in the collection.

The following sections describe the elements that can be added to a page of a PDF document. A HtmlToPdfElement object makes possible several conversions in the same PDF document, a HtmlToImageElement object embeds the image of an HTML document into the PDF document, and a WordToPdfElement object adds the content of a Word document. You can also add texts, images, shapes, digital signatures, bookmarks, templates, watermarks, file attachments and notes.

Sample Code

In the code sample below, the converter records where the IMG, H1 and H2 elements of the HTML document were rendered in the PDF, and each of them is then highlighted with a green rectangle drawn on the PDF page:

PdfConverter pdfConverter = new PdfConverter();

// the HTML elements whose position in the PDF we want: the images and the h1 / h2
// headings
pdfConverter.HtmlElementsMappingOptions.HtmlElementSelectors = new string[] { "img",
    "h1", "h2" };

using (Document pdfDocument = pdfConverter.GetPdfDocumentObjectFromUrl(
    "https://www.example.com/article"))
{
    HtmlElementMappingsCollection mappings =
        pdfConverter.HtmlElementsMappingOptions.HtmlElementsMappingResult;
    foreach (HtmlElementMapping elementMapping in mappings)
    {
        // an element can span several pages: one rectangle for each page where it
        // was rendered
        foreach (HtmlElementPdfRectangle location in elementMapping.PdfRectangles)
        {
            PdfPage page = pdfDocument.Pages[location.PageIndex];
            RectangleF rectangle = location.Rectangle;

            // draw a green frame around the element
            RectangleElement frame = new RectangleElement(rectangle.X, rectangle.Y,
                rectangle.Width, rectangle.Height);
            frame.ForeColor = Color.Green;
            page.AddElement(frame);
        }
    }
    pdfDocument.Save("elements-location.pdf");
}
See Also