【Spire.PDF】Spire.PDF导出报告之实用工具类PdfUtility

记录学习过程

创建日期:2019-04-11

【Spire.PDF】Spire.PDF导出报告之一获取与破解
【Spire.PDF】Spire.PDF导出报告之二基本绘制Draw
【Spire.PDF】Spire.PDF导出报告之三表格Grid
【Spire.PDF】Spire.PDF导出报告之四报告模板
【Spire.PDF】Spire.PDF导出报告之实用工具类PdfUtility

Spire.PDF导出报告之实用工具类PdfUtility

该PDF生成工具类仅供参考

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Diagnostics;
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Grid;
using System.Windows.Forms;

namespace Share
{

    /// <summary>
    /// PDF生成工具
    /// </summary>
    public class PdfUtility
    {
        #region 文档

        /// <summary>
        /// PDF文档
        /// </summary>
        public PdfDocument Doc { get; private set; }
        /// <summary>
        /// 页面
        /// </summary>
        public PdfPageBase Page { get; private set; }

        //595,842
        /// <summary>
        /// 页面宽度 默认595
        /// </summary>
        public float PageWidth
        {
            get { return Doc.PageSettings.Width; }
        }
        /// <summary>
        /// 页面高度 默认842
        /// </summary>
        public float PageHeight
        {
            get { return Doc.PageSettings.Height; }
        }

        /// <summary>
        /// 初始化
        /// </summary>
        public void Init()
        {
            Doc = new PdfDocument();
            Doc.PageSettings.Margins.All = 0;//设置边距为0,默认边距为40
            PagesAdd(); PageRemove(Page);//删除第一页,因为第一页有警告语
        }

        /// <summary>
        /// 保存并打开
        /// </summary>
        /// <param name="name"></param>
        public void SaveToPDF(string name)
        {
            string path = Application.StartupPath + "\\Reports\\" + name + DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf";//以日期为文件名,以防存在打开文件
            Doc.SaveToFile(path);
            Doc.Close();
            Process.Start(path);//打开文档
        }

        #endregion

        #region  Page

        /// <summary>
        /// 增加页面
        /// </summary>
        public void PagesAdd()
        {
            Page = Doc.Pages.Add();
        }

        /// <summary>
        /// 移除页面
        /// </summary>
        /// <param name="page">要移除的页面</param>
        public void PageRemove(PdfPageBase page)
        {
            Doc.Pages.Remove(page);
        }

        /// <summary>
        /// 设置页眉
        /// </summary>
        public void SetPageHeader()
        {
            foreach (var item in Doc.Pages)
            {
                float colWidth = (PageWidth - 2 * 30) / 2;
                GridInit(new float[2] { colWidth, colWidth });
                GridTextAlignments = new PdfTextAlignment[2] { PdfTextAlignment.Left,  PdfTextAlignment.Right };
                GridTextFonts = new PdfTrueTypeFont[2] { Font(10), Font(10)};
                GridTextColors = new PdfSolidBrush[2] { BrushColor(Color.Black), BrushColor(Color.Black) };
                GridRowsAdd("Logo","Text");
                GridDraw(new PointF(30, 20), true, (PdfPageBase)item);

                DrawLine(PenColor(Color.Black), new PointF(30, 40), new PointF(PageWidth - 30, 40), (PdfPageBase)item);
            }
        }

        /// <summary>
        /// 设置页脚
        /// </summary>
        public void SetPageFooter()
        {
            int count = 0;
            int allCount = Doc.Pages.Count;//总页数
            foreach (var item in Doc.Pages)
            {
                count++;//当前页码
                DrawLine(PenColor(Color.Black), new PointF(30, PageHeight - 40), new PointF(PageWidth - 30, PageHeight - 40), (PdfPageBase)item);//底线
                float colWidth = (PageWidth - 2 * 30) / 3;
                GridInit(new float[3] { colWidth, colWidth, colWidth });
                GridTextAlignments = new PdfTextAlignment[3] { PdfTextAlignment.Left, PdfTextAlignment.Center, PdfTextAlignment.Right };
                GridTextFonts = new PdfTrueTypeFont[3] { Font(8), Font(10), Font(8) };
                GridTextColors = new PdfSolidBrush[3] { BrushColor(Color.Black), BrushColor(Color.Gray), BrushColor(Color.Black) };
                GridRowsAdd(DateTime.Now.ToString("yyyy-MM-dd HH:mm"), "Logo", count + "/" + allCount);
                GridDraw(new PointF(30, PageHeight - 40 ), true, (PdfPageBase)item);
            }
        }

        #endregion

        #region Font,BrushColor,PenColor
        /// <summary>
        /// 设置字体大小样式
        /// </summary>
        /// <param name="emSize">大小</param>
        /// <param name="IsBold">是否加粗,默认不加粗</param>
        /// <returns>字体大小样式</returns>
        public PdfTrueTypeFont Font(float emSize, bool IsBold = false)
        {
            return new PdfTrueTypeFont(new Font("宋体", emSize, IsBold ? FontStyle.Bold : FontStyle.Regular), true);//true防止中文乱码
        }

        /// <summary>
        /// 设置笔刷颜色(用于填充图形形状(如矩形、椭圆、饼形、多边形和封闭路径)的内部)
        /// </summary>
        /// <param name="color">颜色</param>
        /// <returns>PdfSolidBrush</returns>
        public PdfSolidBrush BrushColor(Color color)
        {
            return new PdfSolidBrush(color);
        }

        /// <summary>
        /// 设置画笔颜色(用于绘制直线和曲线)
        /// </summary>
        /// <param name="color">颜色</param>
        /// <param name="width">画笔的宽度,默认为1</param>
        /// <returns>画笔颜色</returns>
        public PdfPen PenColor(Color color, float width = 1)
        {
            return new PdfPen(color, width);
        }

        #endregion

        #region 绘制(文本,图像,直线,弧线,贝塞尔曲线,矩形,椭圆,扇形,多边形,路径)

        #region 绘制文本和图像
        /// <summary>
        /// 绘制文本
        /// </summary>
        /// <param name="s">字符串文本</param>
        /// <param name="font">文本格式</param>
        /// <param name="brush">文本颜色</param>
        /// <param name="point">绘制的位置</param>
        /// <param name="page">页面,默认为创建的最后一个页面,也可以重新设置页面</param>
        public void DrawString(string s, PdfTrueTypeFont font, PdfSolidBrush brush, PointF point, PdfPageBase page = null)
        {
            (page ?? Page).Canvas.DrawString(s, font, brush, point);
        }

        /// <summary>
        /// 绘制图像
        /// </summary>
        /// <param name="image">图像</param>
        /// <param name="rectangle">图像的位置和大小</param>
        /// <param name="page">页面,默认为创建的最后一个页面,也可以重新设置页面</param>
        public void DrawImage(Image image, RectangleF rectangle, PdfPageBase page = null)
        {
            (page ?? Page).Canvas.DrawImage(PdfImage.FromImage(image), rectangle);
        }
        #endregion

        #region 绘制线
        /// <summary>
        /// 绘制直线
        /// </summary>
        /// <param name="pen">画笔的颜色,宽度</param>
        /// <param name="point1">第一个点</param>
        /// <param name="point2">第二个点</param>
        /// <param name="page">页面,默认为创建的最后一个页面,也可以重新设置页面</param>
        public void DrawLine(PdfPen pen, PointF point1, PointF point2, PdfPageBase page = null)
        {
            (page ?? Page).Canvas.DrawLine(pen, point1, point2);
        }

        /// <summary>
        /// 绘制弧线
        /// </summary>
        /// <param name="pen">弧线的颜色,宽度</param>
        /// <param name="rectangle">定义椭圆的边界</param>
        /// <param name="startAngle">从 x 轴到弧线的起始点沿顺时针方向度量的角(以度为单位)</param>
        /// <param name="sweepAngle">从 startAngle 参数到弧线的结束点沿顺时针方向度量的角(以度为单位)</param>
        /// <param name="page">页面,默认为创建的最后一个页面,也可以重新设置页面</param>
        public void DrawArc(PdfPen pen, RectangleF rectangle, float startAngle, float sweepAngle, PdfPageBase page = null)
        {
            (page ?? Page).Canvas.DrawArc(pen, rectangle, startAngle, sweepAngle);
        }

        /// <summary>
        /// 绘制贝塞尔曲线
        /// </summary>
        /// <param name="pen">贝塞尔曲线的颜色,宽度</param>
        /// <param name="startPoint">曲线的起始点</param>
        /// <param name="firstControlPoint">曲线的第一个控制点</param>
        /// <param name="secondControlPoint">曲线的第二个控制点</param>
        /// <param name="endPoint">曲线的结束点</param>
        /// <param name="page">页面,默认为创建的最后一个页面,也可以重新设置页面</param>
        public void DrawBezier(PdfPen pen, PointF startPoint, PointF firstControlPoint, PointF secondControlPoint, PointF endPoint, PdfPageBase page = null)
        {
            (page ?? Page).Canvas.DrawBezier(pen, startPoint, firstControlPoint, secondControlPoint, endPoint);
        }
        #endregion

        #region 绘制图形

        #region 矩形(三种形式)
        /// <summary>
        /// 绘制矩形(只有填充)
        /// </summary>
        /// <param name="brush">填充的颜色</param>
        /// <param name="rectangle">矩形的位置和大小</param>
        /// <param name="page">页面,默认为创建的最后一个页面,也可以重新设置页面</param>
        public void DrawRectangle(PdfBrush brush, RectangleF rectangle, PdfPageBase page = null)
        {
            (page ?? Page).Canvas.DrawRectangle(brush, rectangle);
        }

        /// <summary>
        /// 绘制矩形(有边框,无填充)
        /// </summary>
        /// <param name="pen">矩形的颜色、宽度和样式</param>
        /// <param name="rectangle">矩形的位置和大小</param>
        /// <param name="page">页面,默认为创建的最后一个页面,也可以重新设置页面</param>
        public void DrawRectangle(PdfPen pen, RectangleF rectangle, PdfPageBase page = null)
        {
            (page ?? Page).Canvas.DrawRectangle(pen, rectangle);
        }

        /// <summary>
        /// 绘制矩形(有边框,有填充)
        /// </summary>
        /// <param name="pen">矩形的颜色、宽度和样式</param>
        /// <param name="brush">填充的颜色</param>
        /// <param name="rectangle">矩形的位置和大小</param>
        /// <param name="page">页面,默认为创建的最后一个页面,也可以重新设置页面</param>
        public void DrawRectangle(PdfPen pen, PdfBrush brush, RectangleF rectangle, PdfPageBase page = null)
        {
            (page ?? Page).Canvas.DrawRectangle(pen, brush, rectangle);
        }
        #endregion

        #region 绘制椭圆,扇形,多边形,路径(与绘制矩形类似都有三种样式)

        #region 椭圆(三种形式)
        /// <summary>
        /// 绘制椭圆(只有填充)
        /// </summary>
        /// <param name="brush">填充的颜色</param>
        /// <param name="rectangle">椭圆的边界</param>
        /// <param name="page">页面,默认为创建的最后一个页面,也可以重新设置页面</param>
        public void DrawEllipse(PdfBrush brush, RectangleF rectangle, PdfPageBase page = null)
        {
            (page ?? Page).Canvas.DrawEllipse(brush, rectangle);
        }

        /// <summary>
        /// 绘制椭圆(有边框,无填充)
        /// </summary>
        /// <param name="pen">曲线的颜色、宽度和样式</param>
        /// <param name="rectangle">椭圆的边界</param>
        /// <param name="page">页面,默认为创建的最后一个页面,也可以重新设置页面</param>
        public void DrawEllipse(PdfPen pen, RectangleF rectangle, PdfPageBase page = null)
        {
            (page ?? Page).Canvas.DrawEllipse(pen, rectangle);
        }

        /// <summary>
        /// 绘制椭圆(有边框,有填充)
        /// </summary>
        /// <param name="pen">曲线的颜色、宽度和样式</param>
        /// <param name="brush">填充的颜色</param>
        /// <param name="rectangle">椭圆的边界</param>
        /// <param name="page">页面,默认为创建的最后一个页面,也可以重新设置页面</param>
        public void DrawEllipse(PdfPen pen, PdfBrush brush, RectangleF rectangle, PdfPageBase page = null)
        {
            (page ?? Page).Canvas.DrawEllipse(pen, brush, rectangle);
        }

        #endregion

        /// <summary>
        /// 绘制扇形
        /// </summary>
        public void DrawPie(PdfPen pen, PdfBrush brush, RectangleF rectangle, float startAngle, float sweepAngle)
        { }

        /// <summary>
        /// 绘制多边形
        /// </summary>
        public void DrawPolygon(PdfPen pen, PdfBrush brush, PointF[] points)
        { }

        /// <summary>
        /// 绘制路径
        /// </summary>
        public void DrawPath(PdfPen pen, PdfBrush brush, PdfPath path, PdfPageBase page = null)
        { }
        #endregion

        #endregion 绘制图形

        #endregion

        #region 表格Grid

        /// <summary>
        /// 表格
        /// </summary>
        public PdfGrid Grid { get; private set; }
        /// <summary>
        /// 表格的行
        /// </summary>
        public PdfGridRow GridRow { get; set; }

        /// <summary>
        /// 表格初始化
        /// </summary>
        /// <param name="colWidths">列宽度集合</param>
        public void GridInit(float[] colWidths)
        {
            Grid = new PdfGrid();
            Grid.Columns.Add(colWidths.Length);//增加列
            Grid.Style.CellPadding = new PdfPaddings(1, 1, 1, 1);
            Grid.Style.Font = Font(10);//设置字体

            //设置各列宽度
            for (int i = 0; i < colWidths.Length; i++)
            {
                Grid.Columns[i].Width = colWidths[i];
            }
        }

        /// <summary>
        /// 单元格文本对齐方式 为null时,左对齐
        /// </summary>
        public PdfTextAlignment[] GridTextAlignments;
        /// <summary>
        /// 单元格文本字体 为null时,字体10
        /// </summary>
        public PdfTrueTypeFont[] GridTextFonts;
        /// <summary>
        /// 单元格文本颜色 为null时,颜色黑色
        /// </summary>
        public PdfSolidBrush[] GridTextColors;

        /// <summary>
        /// 表格添加行
        /// </summary>
        /// <param name="rowHeight">行高</param>
        public void GridRowsAdd(float rowHeight = 18f)
        {
            GridRow = Grid.Rows.Add();
            GridRow.Height = rowHeight;//设置行高
            for (int i = 0; i < GridRow.Cells.Count; i++)
            {
                GridRow.Cells[i].StringFormat = new PdfStringFormat(
                    GridTextAlignments == null ? PdfTextAlignment.Left : GridTextAlignments[i], PdfVerticalAlignment.Middle);
            }
        }

        /// <summary>
        /// 设置单元格值
        /// </summary>
        /// <param name="values">单元格的值集合</param>
        public void SetGridRowCellsValue(string[] values)
        {
            for (int i = 0; i < GridRow.Cells.Count; i++)
            {
                GridRow.Cells[i].Value = values[i];
                GridRow.Cells[i].Style.Font = GridTextFonts == null ? Grid.Style.Font : GridTextFonts[i];
                GridRow.Cells[i].Style.TextBrush = GridTextColors == null ? new PdfSolidBrush(Color.Black) : GridTextColors[i];
            }
        }

        /// <summary>
        /// 添加行并设置内容
        /// </summary>
        /// <param name="rowWidth">行高</param>
        public void GridRowsAdd(params string[] values)
        {
            GridRowsAdd();
            SetGridRowCellsValue(values);
        }

        /// <summary>
        /// 设置表格边框样式
        /// </summary>
        public void SetBorders()
        {
            PdfBorders borders = new PdfBorders
            {
                All = new PdfPen(Color.Transparent, 0.1f)
            };
            foreach (PdfGridRow row in Grid.Rows)
            {
                foreach (PdfGridCell cell in row.Cells)
                {
                    cell.Style.Borders = borders;
                }
            }
        }

        /// <summary>
        /// 将表格绘制到页面
        /// </summary>
        /// <param name="pointf">表格的位置</param>
        /// <param name="isNoBorder">是否无边框(默认无边框)</param>
        /// <param name="page">默认当前页面(创建的最后一个页面)</param>
        public void GridDraw(PointF pointf, bool isNoBorder = true, PdfPageBase page = null)//测试改为false
        {
            if (isNoBorder)
                SetBorders();
            Grid.Draw(page ?? Page, pointf);
        }

        #endregion 表格Grid
    }
}


【Spire.PDF】Spire.PDF导出报告之一获取与破解
【Spire.PDF】Spire.PDF导出报告之二基本绘制Draw
【Spire.PDF】Spire.PDF导出报告之三表格Grid
【Spire.PDF】Spire.PDF导出报告之四报告模板
【Spire.PDF】Spire.PDF导出报告之实用工具类PdfUtility

相关推荐
©️2020 CSDN 皮肤主题: 精致技术 设计师:CSDN官方博客 返回首页