Post Convert Customization of the Generated PDF Document |
The converter offers a set of render methods described in Advanced Post Convert Customization section, returning the internal Document object created by the converter. This Document object offers access to the collection of pages of PDF document. You can iterate through the document pages, add new pages to the document, append external PDF documents or add new elements like text and images to the document pages. After modification, the document can be saved into a file or to a stream using one of the Save methods of the Document class.
The section Advanced Post Convert Customization contains a detailed description of the classes and methods available in the ExpertPdf.HtmlToPdf.PdfDocument namespace.
Below there is a complete example of how to convert many HTML documents into the same PDF document using post convert customization of the Document object. The code is taken from the WinForms_MultipleHtmlConversions sample. Each additional HTML to PDF conversion is represented by a HtmlToPdfElement object that can be added in any position in a PDF page. The first conversion is performed by the call to pdfConverter.GetPdfDocumentObjectFromUrl, the next two conversions (the conversion of another URL and the conversion of a HTML string) are achieved by adding two HtmlToPdfElement objects to the document pages:
PdfConverter pdfConverter = new PdfConverter(); pdfConverter.RenderingEngine = RenderingEngine.Chromium; // convert the first page into a Document object that can be changed before it is saved Document pdfDocument = pdfConverter.GetPdfDocumentObjectFromUrl("https://www.example.com/part1"); try { // where the first conversion ended: the last page and the rectangle of its content ConversionSummary summary = pdfConverter.ConversionSummary; PdfPage lastPage = pdfDocument.Pages[summary.LastPageIndex]; RectangleF lastRectangle = summary.LastPageRectangle; // the second page continues right below the first one (use pdfDocument.Pages.AddNewPage() to start on a new page) HtmlToPdfElement part2 = new HtmlToPdfElement(lastRectangle.Left, lastRectangle.Bottom, "https://www.example.com/part2"); part2.RenderingEngine = RenderingEngine.Chromium; AddElementResult result = lastPage.AddElement(part2); // and a line of HTML after everything rendered so far PdfPage endPage = pdfDocument.Pages[result.EndPageIndex]; endPage.AddElement(new HtmlToPdfElement(result.EndPageBounds.Left, result.EndPageBounds.Bottom, "<p><i>End of the report</i></p>", null)); pdfDocument.Save("report.pdf"); } finally { pdfDocument.Close(); }