コード例 #1
0
        private static void Main(string[] args)
        {
            var path = AppDomain.CurrentDomain.BaseDirectory;
            var data = File.ReadAllText(path + "\\data.json");
            ComparisonQuotaValue comparisonQuotaValue = JsonConvert.DeserializeObject <ComparisonQuotaValue>(data);

            var outPath = GeneratePDF.ObjectToPdf(comparisonQuotaValue, path);

            Console.WriteLine("pdf生成成功,请到下面路径查看");
            Console.WriteLine(outPath);
        }
コード例 #2
0
        /// <summary>
        /// 转换成pdf
        /// </summary>
        /// <param name="model"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        public static string ObjectToPdf(ComparisonQuotaValue model, string filePath)
        {
            string currentDate = DateTime.Now.ToString("yyyyMMdd");
            var    uploadPath  = filePath + $"/" + currentDate + "/";//>>>相当于HttpContext.Current.Server.MapPath("")

            if (!Directory.Exists(uploadPath))
            {
                Directory.CreateDirectory(uploadPath);
            }
            //输出的文件名称
            var    _temp    = DateTime.Now.ToString("yyyyMMddHHmmssffff");
            string fileName = string.Format("{0}.pdf", $"{model.QuotaName}_" + _temp, Encoding.UTF8);
            //string newFileName = string.Format("{0}_1.pdf", $"{obj.QuotaName}_" + _temp, Encoding.UTF8);
            string pdfPath     = uploadPath + "\\" + fileName;//自定义生成的pdf名
            string newFilePath = "";

            try
            {
                #region 定义字体样式

                BaseFont bfChinese        = BaseFont.CreateFont($"C://WINDOWS//fonts//simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
                Font     fontChinese_12   = new Font(bfChinese, 18, Font.BOLD, new BaseColor(0, 0, 0));
                Font     fontChinese_11   = new Font(bfChinese, 14, Font.BOLD, new BaseColor(0, 0, 0));
                Font     fontChinese_10   = new Font(bfChinese, 10, Font.NORMAL, new BaseColor(248, 248, 255));
                Font     fontChinese_bold = new Font(bfChinese, 8, Font.BOLD, new BaseColor(0, 0, 0));
                Font     fontChinese_8    = new Font(bfChinese, 8, Font.NORMAL, new BaseColor(0, 0, 0));
                Font     fontChinese      = new Font(bfChinese, 7, Font.NORMAL, new BaseColor(0, 0, 0));
                //黑体
                BaseFont bf_ht = BaseFont.CreateFont("C://WINDOWS//Fonts//simhei.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
                Font     ht_7  = new Font(bf_ht, 7, Font.NORMAL, new BaseColor(0, 0, 0));

                #endregion 定义字体样式

                //行高
                float cellHeight = 20;
                //初始化一个目标文档类
                Document document = new Document(PageSize.A4, 5f, 5f, 30f, 0f);
                //调用PDF的写入方法流
                //注意FileMode-Create表示如果目标文件不存在,则创建,如果已存在,则覆盖。
                PdfWriter pdfWriter = PdfWriter.GetInstance(document, new FileStream(pdfPath, FileMode.Create));

                // 页眉页脚
                string space1 = " ".PadLeft(19, ' ');
                string space2 = " ".PadLeft(120, ' ');

                Chunk  chunk1 = new Chunk(space1 + "中国XX企业对标提升数据报表\r\n\r\n", fontChinese_12);                                      //页眉主题
                Chunk  chunk2 = new Chunk(space2 + "日期: " + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + "\r\n", fontChinese_8); //导出时间
                Phrase ph     = new Phrase(9)
                {
                    chunk1,
                    chunk2
                };
                HeaderFooter header = new HeaderFooter(ph, false)
                {
                    Border = Rectangle.NO_BORDER
                };
                document.Header = header;
                //页码
                HeaderFooter footer = new HeaderFooter(new Phrase(4, "页码: ", fontChinese_8), true)
                {
                    Border    = Rectangle.NO_BORDER,
                    Alignment = Element.ALIGN_CENTER,
                    Bottom    = 20
                };
                document.Footer = footer;

                //打开目标文档对象
                document.Open();
                // 创建第一页(如果只有一页的话,这一步可以省略)
                document.NewPage();

                PdfPTable table = new PdfPTable(1)
                {
                    TotalWidth  = 550,
                    LockedWidth = true
                };

                table.SetWidths(new int[] { 550 });
                PdfPCell cell;
                //自定义title
                cell = new PdfPCell(new Phrase($"{model.QuotaName}({model.Stage})", fontChinese_11))
                {
                    Colspan             = 1,
                    HorizontalAlignment = Element.ALIGN_CENTER,
                    VerticalAlignment   = Element.ALIGN_MIDDLE,
                    Border      = Rectangle.NO_BORDER,
                    FixedHeight = 50
                };
                table.AddCell(cell);
                document.Add(table);

                //自定义创建第一行表头
                table = new PdfPTable(model.Attributes.Count + 2)
                {
                    TotalWidth  = 550,
                    LockedWidth = true
                };

                cell = new PdfPCell(new Phrase("单位名称", fontChinese_10))
                {
                    HorizontalAlignment = Element.ALIGN_CENTER,
                    VerticalAlignment   = Element.ALIGN_MIDDLE,
                    BackgroundColor     = BaseColor.DarkGray,
                    FixedHeight         = cellHeight
                };
                table.AddCell(cell);

                cell = new PdfPCell(new Phrase("排名", fontChinese_10))
                {
                    HorizontalAlignment = Element.ALIGN_CENTER,
                    VerticalAlignment   = Element.ALIGN_MIDDLE,
                    BackgroundColor     = BaseColor.DarkGray,
                    FixedHeight         = cellHeight
                };
                table.AddCell(cell);

                model.Attributes.ForEach(x =>
                {
                    cell = new PdfPCell(new Phrase(x.AttributeName, fontChinese_10))
                    {
                        HorizontalAlignment = Element.ALIGN_CENTER,
                        VerticalAlignment   = Element.ALIGN_MIDDLE,
                        BackgroundColor     = BaseColor.DarkGray,
                        FixedHeight         = cellHeight
                    };
                    table.AddCell(cell);
                });

                //数据渲染
                model.EnterpriseComparisonQuotaValue.ForEach(x =>
                {
                    cell = new PdfPCell(new Phrase(x.OrgName, fontChinese_8))
                    {
                        HorizontalAlignment = Element.ALIGN_CENTER,
                        VerticalAlignment   = Element.ALIGN_MIDDLE,
                        FixedHeight         = cellHeight
                    };
                    table.AddCell(cell);

                    cell = new PdfPCell(new Phrase(x.Rank, fontChinese_8))
                    {
                        HorizontalAlignment = Element.ALIGN_CENTER,
                        VerticalAlignment   = Element.ALIGN_MIDDLE,
                        FixedHeight         = cellHeight
                    };
                    table.AddCell(cell);
                    x.Values.ToList().ForEach(m =>
                    {
                        cell = new PdfPCell(new Phrase(m.Value.ToString(), fontChinese_8))
                        {
                            HorizontalAlignment = Element.ALIGN_CENTER,
                            VerticalAlignment   = Element.ALIGN_MIDDLE,
                            FixedHeight         = cellHeight
                        };
                        table.AddCell(cell);
                    });
                });

                document.Add(table);
                //关闭目标文件
                document.Close();
                //关闭写入流
                pdfWriter.Close();

                //添加水印
                AddWatermark(pdfPath, "机密文件,请勿泄露", out newFilePath);
            }
            catch (Exception)
            {
                throw;
            }

            return(newFilePath);
        }