From 8bf427e1c104e4287fe97a43157c14936ecac829 Mon Sep 17 00:00:00 2001 From: Vitor Fernandes Date: Wed, 6 May 2015 16:43:22 +0100 Subject: [PATCH] feat(PdfGenerator): allow generated pages to be appended to a PdfDocument Adds a new method AddPdfPages that generates and appends pages to the specified PdfDocument. This allows the creation of a PdfDocument page by page, where each set of pages can have different layouts and allows better control of page breaks/contents. --- Source/HtmlRenderer.PdfSharp/PdfGenerator.cs | 42 ++++++++++++++++++-- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/Source/HtmlRenderer.PdfSharp/PdfGenerator.cs b/Source/HtmlRenderer.PdfSharp/PdfGenerator.cs index 08b270586..c8c167e05 100644 --- a/Source/HtmlRenderer.PdfSharp/PdfGenerator.cs +++ b/Source/HtmlRenderer.PdfSharp/PdfGenerator.cs @@ -89,7 +89,44 @@ public static PdfDocument GeneratePdf(string html, PdfGenerateConfig config, Css { // create PDF document to render the HTML into var document = new PdfDocument(); - + + // add rendered PDF pages to document + AddPdfPages(document, html, config, cssData, stylesheetLoad, imageLoad); + + return document; + } + + /// + /// Create PDF pages from given HTML and appends them to the provided PDF document.
+ ///
+ /// PDF document to append pages to + /// HTML source to create PDF from + /// the page size to use for each page in the generated pdf + /// the margin to use between the HTML and the edges of each page + /// optional: the style to use for html rendering (default - use W3 default style) + /// optional: can be used to overwrite stylesheet resolution logic + /// optional: can be used to overwrite image resolution logic + /// the generated image of the html + public static void AddPdfPages(PdfDocument document, string html, PageSize pageSize, int margin = 20, CssData cssData = null, EventHandler stylesheetLoad = null, EventHandler imageLoad = null) + { + var config = new PdfGenerateConfig(); + config.PageSize = pageSize; + config.SetMargins(margin); + AddPdfPages(document, html, config, cssData, stylesheetLoad, imageLoad); + } + + /// + /// Create PDF pages from given HTML and appends them to the provided PDF document.
+ ///
+ /// PDF document to append pages to + /// HTML source to create PDF from + /// the configuration to use for the PDF generation (page size/page orientation/margins/etc.) + /// optional: the style to use for html rendering (default - use W3 default style) + /// optional: can be used to overwrite stylesheet resolution logic + /// optional: can be used to overwrite image resolution logic + /// the generated image of the html + public static void AddPdfPages(PdfDocument document, string html, PdfGenerateConfig config, CssData cssData = null, EventHandler stylesheetLoad = null, EventHandler imageLoad = null) + { XSize orgPageSize; // get the size of each page to layout the HTML in if (config.PageSize != PageSize.Undefined) @@ -146,11 +183,10 @@ public static PdfDocument GeneratePdf(string html, PdfGenerateConfig config, Css HandleLinks(document, container, orgPageSize, pageSize); } } - - return document; } + #region Private/Protected methods ///