Пример #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
1
        /// <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="config">the configuration to use for the PDF generation (page size/page orientation/margins/etc.)</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, PdfGenerateConfig config, CssData cssData = null, EventHandler<HtmlStylesheetLoadEventArgs> stylesheetLoad = null, EventHandler<HtmlImageLoadEventArgs> imageLoad = null)
        {
            XSize orgPageSize;
            // get the size of each page to layout the HTML in
            if (config.PageSize != PageSize.Undefined)
                orgPageSize = PageSizeConverter.ToSize(config.PageSize);
            else
                orgPageSize = config.ManualPageSize;

            if (config.PageOrientation == PageOrientation.Landscape)
            {
                // invert pagesize for landscape
                orgPageSize = new XSize(orgPageSize.Height, orgPageSize.Width);
            }

            var pageSize = new XSize(orgPageSize.Width - config.MarginLeft - config.MarginRight, orgPageSize.Height - config.MarginTop - config.MarginBottom);

            if (!string.IsNullOrEmpty(html))
            {
                using (var container = new HtmlContainer())
                {
                    if (stylesheetLoad != null)
                        container.StylesheetLoad += stylesheetLoad;
                    if (imageLoad != null)
                        container.ImageLoad += imageLoad;

                    container.Location = new XPoint(config.MarginLeft, config.MarginTop);
                    container.MaxSize = new XSize(pageSize.Width, 0);
                    container.SetHtml(html, cssData);
                    container.PageSize = pageSize;                    
                    container.MarginBottom = config.MarginBottom;  
                    container.MarginLeft = config.MarginLeft;
                    container.MarginRight = config.MarginRight;
                    container.MarginTop = config.MarginTop;

                    // layout the HTML with the page width restriction to know how many pages are required
                    var measure = XGraphics.CreateMeasureContext(pageSize, XGraphicsUnit.Point, XPageDirection.Downwards);              
                    container.PerformLayout(measure);                    

                    // while there is un-rendered HTML, create another PDF page and render with proper offset for the next page
                    double scrollOffset = 0;
                    //SL: if there is more than one page, increase the bottom margin to allow space for the page number
                    container.MarginBottom += scrollOffset > -container.ActualSize.Height ?  20 : 0;
                    container.PerformLayout(measure); //SL: This still does not increase the margin for the first page of a multi page.. welp
                    while (scrollOffset > -container.ActualSize.Height)
                    {
                        var page = document.AddPage();
                        page.Height = orgPageSize.Height;
                        page.Width = orgPageSize.Width;

                        using (var g = XGraphics.FromPdfPage(page))
                        {
                            //g.IntersectClip(new XRect(config.MarginLeft, config.MarginTop, pageSize.Width, pageSize.Height));
                            g.IntersectClip(new XRect(0, 0, page.Width, page.Height));

                            container.ScrollOffset = new XPoint(0, scrollOffset);
                            container.PerformPaint(g);
                        }
                        scrollOffset -= pageSize.Height;
                    }

                    if (config.AddPageCountFoooter && document.PageCount > 1)  //Only add page numbers if more than one page
                        AddPageCountFoooter(document, pageSize);

                    // SL: Set config option to handle links or not as it crashes for 
                    // some valid html links. 
                    // TODO: Investigate reason for crashing. 
                    if (config.HandleLinks)
                        HandleLinks(document, container, orgPageSize, pageSize);
                }
            }
        }
Пример #4
1
        /// <summary>
        /// Create PDF document from given HTML.<br/>
        /// </summary>
        /// <param name="html">HTML source to create PDF from</param>
        /// <param name="config">the configuration to use for the PDF generation (page size/page orientation/margins/etc.)</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, PdfGenerateConfig config, CssData cssData = null, EventHandler<HtmlStylesheetLoadEventArgs> stylesheetLoad = null, EventHandler<HtmlImageLoadEventArgs> imageLoad = null)
        {
            // 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;
        }
Пример #5
1
        /// <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="config">the configuration to use for the PDF generation (page size/page orientation/margins/etc.)</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, PdfGenerateConfig config, CssData cssData = null, EventHandler<HtmlStylesheetLoadEventArgs> stylesheetLoad = null, EventHandler<HtmlImageLoadEventArgs> imageLoad = null)
        {
            XSize orgPageSize;
            // get the size of each page to layout the HTML in
            if (config.PageSize != PageSize.Undefined)
                orgPageSize = PageSizeConverter.ToSize(config.PageSize);
            else
                orgPageSize = config.ManualPageSize;

            if (config.PageOrientation == PageOrientation.Landscape)
            {
                // invert pagesize for landscape
                orgPageSize = new XSize(orgPageSize.Height, orgPageSize.Width);
            }

            var pageSize = new XSize(orgPageSize.Width - config.MarginLeft - config.MarginRight, orgPageSize.Height - config.MarginTop - config.MarginBottom);

            if (!string.IsNullOrEmpty(html))
            {
                using (var container = new HtmlContainer())
                {
                    if (stylesheetLoad != null)
                        container.StylesheetLoad += stylesheetLoad;
                    if (imageLoad != null)
                        container.ImageLoad += imageLoad;

                    container.Location = new XPoint(config.MarginLeft, config.MarginTop);
                    container.MaxSize = new XSize(pageSize.Width, 0);
                    container.SetHtml(html, cssData);
                    container.PageSize = pageSize;
                    container.MarginBottom = config.MarginBottom;
                    container.MarginLeft = config.MarginLeft;
                    container.MarginRight = config.MarginRight;
                    container.MarginTop = config.MarginTop;

                    // layout the HTML with the page width restriction to know how many pages are required
                    using (var measure = XGraphics.CreateMeasureContext(pageSize, XGraphicsUnit.Point, XPageDirection.Downwards))
                    {
                        container.PerformLayout(measure);
                    }

                    // while there is un-rendered HTML, create another PDF page and render with proper offset for the next page
                    double scrollOffset = 0;
                    while (scrollOffset > -container.ActualSize.Height)
                    {
                        var page = document.AddPage();
                        page.Height = orgPageSize.Height;
                        page.Width = orgPageSize.Width;

                        using (var g = XGraphics.FromPdfPage(page))
                        {
                            //g.IntersectClip(new XRect(config.MarginLeft, config.MarginTop, pageSize.Width, pageSize.Height));
                            g.IntersectClip(new XRect(0, 0, page.Width, page.Height));

                            container.ScrollOffset = new XPoint(0, scrollOffset);
                            container.PerformPaint(g);
                        }
                        scrollOffset -= pageSize.Height;
                    }

                    // add web links and anchors
                    HandleLinks(document, container, orgPageSize, pageSize);
                }
            }
        }
Пример #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 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);
        }
Пример #7
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));
        }
Пример #8
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));
        }
Пример #9
0
        /// <summary>
        /// Create PDF document from given HTML.<br/>
        /// </summary>
        /// <param name="html">HTML source to create PDF from</param>
        /// <param name="config">the configuration to use for the PDF generation (page size/page orientation/margins/etc.)</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, PdfGenerateConfig config, CssData cssData = null, EventHandler <HtmlStylesheetLoadEventArgs> stylesheetLoad = null, EventHandler <HtmlImageLoadEventArgs> imageLoad = null)
        {
            // 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);
        }
Пример #10
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));
        }
Пример #11
0
        /// <summary>
        /// Create PDF document from given HTML.<br/>
        /// </summary>
        /// <param name="html">HTML source to create PDF from</param>
        /// <param name="config">the configuration to use for the PDF generation (page size/page orientation/margins/etc.)</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, PdfGenerateConfig config, CssData cssData = null, EventHandler <HtmlStylesheetLoadEventArgs> stylesheetLoad = null, EventHandler <HtmlImageLoadEventArgs> imageLoad = null)
        {
            // create PDF document to render the HTML into
            var document = new PdfDocument();

            // get the size of each page to layout the HTML in
            var orgPageSize = PageSizeConverter.ToSize(config.PageSize);
            var pageSize    = new XSize(orgPageSize.Width - config.MarginLeft - config.MarginRight, orgPageSize.Height - config.MarginTop - config.MarginBottom);

            if (!string.IsNullOrEmpty(html))
            {
                using (var container = new HtmlContainer())
                {
                    if (stylesheetLoad != null)
                    {
                        container.StylesheetLoad += stylesheetLoad;
                    }
                    if (imageLoad != null)
                    {
                        container.ImageLoad += imageLoad;
                    }

                    container.Location = new XPoint(config.MarginLeft, config.MarginTop);
                    container.MaxSize  = new XSize(pageSize.Width, 0);
                    container.SetHtml(html, cssData);

                    // layout the HTML with the page width restriction to know how many pages are required
                    using (var measure = XGraphics.CreateMeasureContext(pageSize, XGraphicsUnit.Point, XPageDirection.Downwards))
                    {
                        container.PerformLayout(measure);
                    }

                    // while there is un-rendered HTML, create another PDF page and render with proper offset for the next page
                    double scrollOffset = 0;
                    while (scrollOffset > -container.ActualSize.Height)
                    {
                        var page = document.AddPage();
                        page.Size = config.PageSize;
                        using (var g = XGraphics.FromPdfPage(page))
                        {
                            g.IntersectClip(new XRect(config.MarginLeft, config.MarginTop, pageSize.Width, pageSize.Height));

                            container.ScrollOffset = new XPoint(0, scrollOffset);
                            container.PerformPaint(g);
                        }
                        scrollOffset -= pageSize.Height;
                    }

                    // add web links and anchors
                    HandleLinks(document, container, orgPageSize, pageSize);
                }
            }

            return(document);
        }
Пример #12
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);
        }
Пример #13
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="config">the configuration to use for the PDF generation (page size/page orientation/margins/etc.)</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, PdfGenerateConfig config, CssData cssData = null, EventHandler <HtmlStylesheetLoadEventArgs> stylesheetLoad = null, EventHandler <HtmlImageLoadEventArgs> imageLoad = null)
 {
     AddPdfPages(null, document, html, config, cssData, stylesheetLoad, imageLoad);
 }
Пример #14
0
        /// <summary>
        /// Create PDF pages from given HTML and appends them to the provided PDF document
        /// </summary>
        /// <param name="template">template to use for the page, if null just make a blank page</param>
        /// <param name="document"></param>
        /// <param name="html"></param>
        /// <param name="config"></param>
        /// <param name="cssData"></param>
        /// <param name="stylesheetLoad"></param>
        /// <param name="imageLoad"></param>
        public static void AddPdfPages(PdfDocument template, PdfDocument document, string html, PdfGenerateConfig config, CssData cssData = null, EventHandler <HtmlStylesheetLoadEventArgs> stylesheetLoad = null, EventHandler <HtmlImageLoadEventArgs> imageLoad = null)
        {
            XSize orgPageSize;

            // get the size of each page to layout the HTML in
            if (config.PageSize != PageSize.Undefined)
            {
                orgPageSize = PageSizeConverter.ToSize(config.PageSize);
            }
            else
            {
                orgPageSize = config.ManualPageSize;
            }

            if (config.PageOrientation == PageOrientation.Landscape)
            {
                // invert pagesize for landscape
                orgPageSize = new XSize(orgPageSize.Height, orgPageSize.Width);
            }

            var pageSize = new XSize(orgPageSize.Width - config.MarginLeft - config.MarginRight, orgPageSize.Height - config.MarginTop - config.MarginBottom);

            if (!string.IsNullOrEmpty(html))
            {
                using (var container = new HtmlContainer())
                {
                    if (stylesheetLoad != null)
                    {
                        container.StylesheetLoad += stylesheetLoad;
                    }
                    if (imageLoad != null)
                    {
                        container.ImageLoad += imageLoad;
                    }

                    container.Location = new XPoint(config.MarginLeft, config.MarginTop);
                    container.MaxSize  = new XSize(pageSize.Width, 0);
                    container.SetHtml(html, cssData);
                    container.PageSize     = pageSize;
                    container.MarginBottom = config.MarginBottom;
                    container.MarginLeft   = config.MarginLeft;
                    container.MarginRight  = config.MarginRight;
                    container.MarginTop    = config.MarginTop;

                    // layout the HTML with the page width restriction to know how many pages are required
                    using (var measure = XGraphics.CreateMeasureContext(pageSize, XGraphicsUnit.Point, XPageDirection.Downwards))
                    {
                        container.PerformLayout(measure);
                    }

                    // while there is un-rendered HTML, create another PDF page and render with proper offset for the next page
                    double scrollOffset = 0;
                    while (scrollOffset > -container.ActualSize.Height)
                    {
                        PdfPage page;

                        if (template != null)
                        {
                            var templatePage = template.Pages[0];
                            page = document.AddPage(templatePage);
                        }
                        else
                        {
                            page = document.AddPage();
                        }


                        page.Height = orgPageSize.Height;
                        page.Width  = orgPageSize.Width;

                        using (var g = XGraphics.FromPdfPage(page))
                        {
                            //g.IntersectClip(new XRect(config.MarginLeft, config.MarginTop, pageSize.Width, pageSize.Height));
                            g.IntersectClip(new XRect(0, 0, page.Width, page.Height));

                            container.ScrollOffset = new XPoint(0, scrollOffset);
                            container.PerformPaint(g);
                        }
                        scrollOffset -= pageSize.Height;
                    }

                    // add web links and anchors
                    HandleLinks(document, container, orgPageSize, pageSize);
                }
            }
        }
Пример #15
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="config">the configuration to use for the PDF generation (page size/page orientation/margins/etc.)</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, PdfGenerateConfig config, CssData cssData = null, EventHandler <HtmlStylesheetLoadEventArgs> stylesheetLoad = null, EventHandler <HtmlImageLoadEventArgs> imageLoad = null)
        {
            XSize orgPageSize;

            // get the size of each page to layout the HTML in
            if (config.PageSize != PageSize.Undefined)
            {
                orgPageSize = PageSizeConverter.ToSize(config.PageSize);
            }
            else
            {
                orgPageSize = config.ManualPageSize;
            }

            if (config.PageOrientation == PageOrientation.Landscape)
            {
                // invert pagesize for landscape
                orgPageSize = new XSize(orgPageSize.Height, orgPageSize.Width);
            }

            var pageSize = new XSize(orgPageSize.Width - config.MarginLeft - config.MarginRight, orgPageSize.Height - config.MarginTop - config.MarginBottom);

            if (!string.IsNullOrEmpty(html))
            {
                using (var container = new HtmlContainer())
                {
                    if (stylesheetLoad != null)
                    {
                        container.StylesheetLoad += stylesheetLoad;
                    }
                    if (imageLoad != null)
                    {
                        container.ImageLoad += imageLoad;
                    }

                    container.Location = new XPoint(config.MarginLeft, config.MarginTop);
                    container.MaxSize  = new XSize(pageSize.Width, 0);
                    container.SetHtml(html, cssData);
                    container.PageSize     = pageSize;
                    container.MarginBottom = config.MarginBottom;
                    container.MarginLeft   = config.MarginLeft;
                    container.MarginRight  = config.MarginRight;
                    container.MarginTop    = config.MarginTop;

                    // layout the HTML with the page width restriction to know how many pages are required
                    var measure = XGraphics.CreateMeasureContext(pageSize, XGraphicsUnit.Point, XPageDirection.Downwards);
                    container.PerformLayout(measure);

                    // while there is un-rendered HTML, create another PDF page and render with proper offset for the next page
                    double scrollOffset = 0;
                    //SL: if there is more than one page, increase the bottom margin to allow space for the page number
                    container.MarginBottom += scrollOffset > -container.ActualSize.Height ?  20 : 0;
                    container.PerformLayout(measure); //SL: This still does not increase the margin for the first page of a multi page.. welp
                    while (scrollOffset > -container.ActualSize.Height)
                    {
                        var page = document.AddPage();
                        page.Height = orgPageSize.Height;
                        page.Width  = orgPageSize.Width;

                        using (var g = XGraphics.FromPdfPage(page))
                        {
                            //g.IntersectClip(new XRect(config.MarginLeft, config.MarginTop, pageSize.Width, pageSize.Height));
                            g.IntersectClip(new XRect(0, 0, page.Width, page.Height));

                            container.ScrollOffset = new XPoint(0, scrollOffset);
                            container.PerformPaint(g);
                        }
                        scrollOffset -= pageSize.Height;
                    }

                    if (config.AddPageCountFoooter && document.PageCount > 1)  //Only add page numbers if more than one page
                    {
                        AddPageCountFoooter(document, pageSize);
                    }

                    // SL: Set config option to handle links or not as it crashes for
                    // some valid html links.
                    // TODO: Investigate reason for crashing.
                    if (config.HandleLinks)
                    {
                        HandleLinks(document, container, orgPageSize, pageSize);
                    }
                }
            }
        }
Пример #16
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="config">the configuration to use for the PDF generation (page size/page orientation/margins/etc.)</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, PdfGenerateConfig config, CssData cssData = null, EventHandler <HtmlStylesheetLoadEventArgs> stylesheetLoad = null, EventHandler <HtmlImageLoadEventArgs> imageLoad = null)
        {
            XSize orgPageSize;

            // get the size of each page to layout the HTML in
            if (config.PageSize != PageSize.Undefined)
            {
                orgPageSize = PageSizeConverter.ToSize(config.PageSize);
            }
            else
            {
                orgPageSize = config.ManualPageSize;
            }

            if (config.PageOrientation == PageOrientation.Landscape)
            {
                // invert pagesize for landscape
                orgPageSize = new XSize(orgPageSize.Height, orgPageSize.Width);
            }

            var pageSize = new XSize(orgPageSize.Width - config.MarginLeft - config.MarginRight, orgPageSize.Height - config.MarginTop - config.MarginBottom);

            if (!string.IsNullOrEmpty(html))
            {
                using (var container = new HtmlContainer())
                {
                    if (stylesheetLoad != null)
                    {
                        container.StylesheetLoad += stylesheetLoad;
                    }
                    if (imageLoad != null)
                    {
                        container.ImageLoad += imageLoad;
                    }

                    container.Location = new XPoint(config.MarginLeft, config.MarginTop);
                    container.MaxSize  = new XSize(pageSize.Width, 0);
                    container.SetHtml(html, cssData);
                    container.PageSize     = pageSize;
                    container.MarginBottom = config.MarginBottom;
                    container.MarginLeft   = config.MarginLeft;
                    container.MarginRight  = config.MarginRight;
                    container.MarginTop    = config.MarginTop;

                    // layout the HTML with the page width restriction to know how many pages are required
                    using (var measure = XGraphics.CreateMeasureContext(pageSize, XGraphicsUnit.Point, XPageDirection.Downwards))
                    {
                        container.PerformLayout(measure);
                    }

                    // while there is un-rendered HTML, create another PDF page and render with proper offset for the next page
                    double scrollOffset = 0;

                    int totalPages = (int)(container.ActualSize.Height / pageSize.Height) + 1;

                    while (scrollOffset > -container.ActualSize.Height)
                    {
                        var page = document.AddPage();
                        page.Height = orgPageSize.Height;
                        page.Width  = orgPageSize.Width;

                        using (var g = XGraphics.FromPdfPage(page))
                        {
                            //g.IntersectClip(new XRect(config.MarginLeft, config.MarginTop, pageSize.Width, pageSize.Height));
                            g.IntersectClip(new XRect(0, 0, page.Width, page.Height));

                            container.ScrollOffset = new XPoint(0, scrollOffset);
                            container.PerformPaint(g);

                            if (config.EnablePageNumbering)
                            {
                                string pageNumbersStr     = string.Format(config.PageNumbersPattern, (-scrollOffset / pageSize.Height) + 1, totalPages);
                                var    pageNumbersStrSize = g.MeasureString(pageNumbersStr, config.PageNumbersFont);

                                double xCord = 0;
                                switch (config.PageNumberLocation)
                                {
                                case PageNumberLocation.Left:
                                    xCord = config.MarginLeft + pageNumbersStrSize.Width / 2;
                                    break;

                                case PageNumberLocation.Middle:
                                    xCord = config.MarginLeft + pageSize.Width / 2 - pageNumbersStrSize.Width / 2;
                                    break;

                                case PageNumberLocation.Right:
                                    xCord = pageSize.Width - pageNumbersStrSize.Width / 2;
                                    break;
                                }

                                //draw page numbers
                                g.DrawString(
                                    pageNumbersStr,
                                    config.PageNumbersFont,
                                    new XSolidBrush(),
                                    new XPoint(xCord, page.Height - config.PageNumbersMarginBottom));

                                //g.DrawString(
                                //	"middle",
                                //	config.PageNumbersFont,
                                //	new XSolidBrush(),
                                //	new XPoint(0, page.Height - config.PageNumbersMarginBottom));

                                //g.DrawString(
                                //	"right",
                                //	config.PageNumbersFont,
                                //	new XSolidBrush(),
                                //	new XPoint(pageSize.Width/2 + config.MarginLeft - g.MeasureString("x", config.PageNumbersFont).Width, page.Height - config.PageNumbersMarginBottom));

                                //g.DrawString(
                                //	"left",
                                //	config.PageNumbersFont,
                                //	new XSolidBrush(),
                                //	new XPoint(config.MarginLeft + g.MeasureString("left", config.PageNumbersFont).Width / 2, page.Height - config.PageNumbersMarginBottom));
                            }
                        }

                        scrollOffset -= pageSize.Height;
                    }

                    // add web links and anchors
                    HandleLinks(document, container, orgPageSize, pageSize);
                }
            }
        }
Пример #17
0
        /// <summary>
        /// Create PDF document from given HTML.<br/>
        /// </summary>
        /// <param name="html">HTML source to create PDF from</param>
        /// <param name="config">the configuration to use for the PDF generation (page size/page orientation/margins/etc.)</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 async Task <PdfDocument> GeneratePdf(IResourceServer resourceServer, PdfGenerateConfig config
                                                           )
        {
            // create PDF document to render the HTML into
            var document = new PdfDocument();

            // add rendered PDF pages to document
            AddPdfPages(document, resourceServer, config);

            return(document);
        }
Пример #18
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="config">the configuration to use for the PDF generation (page size/page orientation/margins/etc.)</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 async Task AddPdfPages(PdfDocument document,
                                             IResourceServer resourceServer,
                                             PdfGenerateConfig config
                                             )
        {
            XSize orgPageSize;

            // get the size of each page to layout the HTML in
            if (config.PageSize != PageSize.Undefined)
            {
                orgPageSize = PageSizeConverter.ToSize(config.PageSize);
            }
            else
            {
                orgPageSize = config.ManualPageSize;
            }

            if (config.PageOrientation == PageOrientation.Landscape)
            {
                // invert pagesize for landscape
                orgPageSize = new XSize(orgPageSize.Height, orgPageSize.Width);
            }

            var pageSize = new XSize(orgPageSize.Width - config.MarginLeft - config.MarginRight, orgPageSize.Height - config.MarginTop - config.MarginBottom);

            var html = await resourceServer.GetHtmlAsync();

            if (!string.IsNullOrEmpty(html))
            {
                using (var container = new HtmlContainer())
                {
                    container.Location = new XPoint(config.MarginLeft, config.MarginTop);
                    container.MaxSize  = new XSize(pageSize.Width, 0);
                    await container.SetResourceServerAsync(resourceServer);

                    container.PageSize     = pageSize;
                    container.MarginBottom = config.MarginBottom;
                    container.MarginLeft   = config.MarginLeft;
                    container.MarginRight  = config.MarginRight;
                    container.MarginTop    = config.MarginTop;

                    // layout the HTML with the page width restriction to know how many pages are required
                    using (var measure = XGraphics.CreateMeasureContext(pageSize, XGraphicsUnit.Point, XPageDirection.Downwards))
                    {
                        container.PerformLayout(measure);
                    }

                    // while there is un-rendered HTML, create another PDF page and render with proper offset for the next page
                    double scrollOffset = 0;
                    while (scrollOffset > -container.ActualSize.Height)
                    {
                        var page = document.AddPage();
                        page.Height = orgPageSize.Height;
                        page.Width  = orgPageSize.Width;

                        using (var g = XGraphics.FromPdfPage(page))
                        {
                            //g.IntersectClip(new XRect(config.MarginLeft, config.MarginTop, pageSize.Width, pageSize.Height));
                            g.IntersectClip(new XRect(0, 0, page.Width, page.Height));

                            container.ScrollOffset = new XPoint(0, scrollOffset);
                            container.PerformPaint(g);
                        }
                        scrollOffset -= pageSize.Height;
                    }

                    // add web links and anchors
                    HandleLinks(document, container, orgPageSize, pageSize);
                }
            }
        }