Пример #1
2
 /// <summary>
 /// Create PDF document from given HTML.<br/>
 /// </summary>
 /// <param name="html">HTML source to create PDF from</param>
 /// <param name="pageSize">the page size to use for each page in the generated pdf </param>
 /// <param name="margin">the margin to use between the HTML and the edges of each page</param>
 /// <param name="cssData">optional: the style to use for html rendering (default - use W3 default style)</param>
 /// <param name="stylesheetLoad">optional: can be used to overwrite stylesheet resolution logic</param>
 /// <param name="imageLoad">optional: can be used to overwrite image resolution logic</param>
 /// <returns>the generated image of the html</returns>
 public static PdfDocument GeneratePdf(string html, PageSize pageSize, int margin = 20, CssData cssData = null, EventHandler<HtmlStylesheetLoadEventArgs> stylesheetLoad = null, EventHandler<HtmlImageLoadEventArgs> imageLoad = null)
 {
     var config = new PdfGenerateConfig();
     config.PageSize = pageSize;
     config.SetMargins(margin);
     return GeneratePdf(html, config, cssData, stylesheetLoad, imageLoad);
 }
Пример #2
1
        /// <summary>
        /// Create PDF using PdfSharp project, save to file and open that file.
        /// </summary>
        private void OnGeneratePdf_Click(object sender, EventArgs e)
        {
            PdfGenerateConfig config = new PdfGenerateConfig();
            config.PageSize = PageSize.A4;
            config.SetMargins(20);

            var doc = PdfGenerator.GeneratePdf(_mainControl.GetHtml(), config, null, DemoUtils.OnStylesheetLoad, HtmlRenderingHelper.OnImageLoadPdfSharp);
            var tmpFile = Path.GetTempFileName();
            tmpFile = Path.GetFileNameWithoutExtension(tmpFile) + ".pdf";
            doc.Save(tmpFile);
            Process.Start(tmpFile);
        }
Пример #3
0
        /// <summary>
        /// Create PDF document from given HTML.<br/>
        /// </summary>
        /// <param name="html">HTML source to create PDF from</param>
        /// <param name="pageSize">the page size to use for each page in the generated pdf </param>
        /// <param name="margin">the margin to use between the HTML and the edges of each page</param>
        /// <param name="cssData">optional: the style to use for html rendering (default - use W3 default style)</param>
        /// <param name="stylesheetLoad">optional: can be used to overwrite stylesheet resolution logic</param>
        /// <param name="imageLoad">optional: can be used to overwrite image resolution logic</param>
        /// <returns>the generated image of the html</returns>
        public static PdfDocument GeneratePdf(string html, PageSize pageSize, int margin = 20, CssData cssData = null, EventHandler <HtmlStylesheetLoadEventArgs> stylesheetLoad = null, EventHandler <HtmlImageLoadEventArgs> imageLoad = null)
        {
            var config = new PdfGenerateConfig();

            config.PageSize = pageSize;
            config.SetMargins(margin);
            return(GeneratePdf(html, config, cssData, stylesheetLoad, imageLoad));
        }
Пример #4
0
        /// <summary>
        /// Create PDF pages from given HTML and appends them to the provided PDF document.<br/>
        /// </summary>
        /// <param name="document">PDF document to append pages to</param>
        /// <param name="html">HTML source to create PDF from</param>
        /// <param name="pageSize">the page size to use for each page in the generated pdf </param>
        /// <param name="margin">the margin to use between the HTML and the edges of each page</param>
        /// <param name="cssData">optional: the style to use for html rendering (default - use W3 default style)</param>
        /// <param name="stylesheetLoad">optional: can be used to overwrite stylesheet resolution logic</param>
        /// <param name="imageLoad">optional: can be used to overwrite image resolution logic</param>
        /// <returns>the generated image of the html</returns>
        public static void AddPdfPages(PdfDocument document, string html, PageSize pageSize, int margin = 20, CssData cssData = null, EventHandler <HtmlStylesheetLoadEventArgs> stylesheetLoad = null, EventHandler <HtmlImageLoadEventArgs> imageLoad = null)
        {
            var config = new PdfGenerateConfig();

            config.PageSize = pageSize;
            config.SetMargins(margin);
            AddPdfPages(document, html, config, cssData, stylesheetLoad, imageLoad);
        }
Пример #5
0
        /// <summary>
        /// Create PDF document from given HTML.<br/>
        /// </summary>
        /// <param name="html">HTML source to create PDF from</param>
        /// <param name="pageSize">the page size to use for each page in the generated pdf </param>
        /// <param name="margin">the margin to use between the HTML and the edges of each page</param>
        /// <param name="cssData">optional: the style to use for html rendering (default - use W3 default style)</param>
        /// <param name="stylesheetLoad">optional: can be used to overwrite stylesheet resolution logic</param>
        /// <param name="imageLoad">optional: can be used to overwrite image resolution logic</param>
        /// <returns>the generated image of the html</returns>
        public static Task <PdfDocument> GeneratePdf(IResourceServer resourceServer,
                                                     PageSize pageSize, int margin = 20
                                                     )
        {
            var config = new PdfGenerateConfig();

            config.PageSize = pageSize;
            config.SetMargins(margin);
            return(GeneratePdf(resourceServer, config));
        }
Пример #6
0
        /// <summary>
        /// Create PDF pages from given HTML and appends them to the provided PDF document.<br/>
        /// </summary>
        /// <param name="document">PDF document to append pages to</param>
        /// <param name="html">HTML source to create PDF from</param>
        /// <param name="pageSize">the page size to use for each page in the generated pdf </param>
        /// <param name="margin">the margin to use between the HTML and the edges of each page</param>
        /// <param name="cssData">optional: the style to use for html rendering (default - use W3 default style)</param>
        /// <param name="stylesheetLoad">optional: can be used to overwrite stylesheet resolution logic</param>
        /// <param name="imageLoad">optional: can be used to overwrite image resolution logic</param>
        /// <returns>the generated image of the html</returns>
        public static Task AddPdfPages(PdfDocument document, IResourceServer resourceServer,
                                       PageSize pageSize, int margin = 20
                                       )
        {
            var config = new PdfGenerateConfig();

            config.PageSize = pageSize;
            config.SetMargins(margin);
            return(AddPdfPages(document, resourceServer, config));
        }
Пример #7
0
        /// <summary>
        /// Create PDF document from given HTML.
        /// <para>This also take a template document that will be used to overlay the HTML onto
        /// this allows for a corporate look and feel to the PDFs</para>
        /// </summary>
        /// <param name="template">PDFDocument containing the template, if not required set to null</param>
        /// <param name="html"></param>
        /// <param name="pageSize"></param>
        /// <param name="margin"></param>
        /// <param name="cssData"></param>
        /// <param name="stylesheetLoad"></param>
        /// <param name="imageLoad"></param>
        /// <returns></returns>
        public static PdfDocument GeneratePdf(PdfDocument template, string html, PageSize pageSize,
                                              int margin = 20, CssData cssData = null,
                                              EventHandler <HtmlStylesheetLoadEventArgs> stylesheetLoad = null,
                                              EventHandler <HtmlImageLoadEventArgs> imageLoad           = null)
        {
            var config = new PdfGenerateConfig();

            config.PageSize = pageSize;
            config.SetMargins(margin);

            var document = new PdfDocument();

            // add rendered PDF pages to document
            AddPdfPages(template, document, html, config, cssData, stylesheetLoad, imageLoad);

            return(document);
        }