.NET MVC之生成PDF

记录一个.NET 生成PDF文件的代码

   //指定项目下的一个文件夹 用于存储生成的PDF文件 
   var directory = "/PDF/Common/";
   //获取这个文件夹在本地的绝对路径
   var fullDirectory = HttpContext.Current.Server.MapPath(directory);
   //如果这个文件夹不存在,则创建这个文件夹
   if (!Directory.Exists(fullDirectory)) Directory.CreateDirectory(fullDirectory);
   //文件名称用生成的GUID代替
   string fileName = string.Format("{0}.pdf", Guid.NewGuid().ToString());
   //定义本地路径
   string LocalFilePath = "";

using (FileStream fs = new FileStream(localpath, FileMode.Create))
            {
                using (Document document = new Document(new Rectangle(0, 0, PageSize.A4.Width, PageSize.A4.Height)))
                {
                    PdfWriter pdfWriter = PdfWriter.GetInstance(document, fs);
                    document.Open();
                    BaseFont bsFont = BaseFont.CreateFont("C:/WINDOWS/Fonts/simfang.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
                    Font titleFont = new Font(bsFont, 15, Font.BOLD);
                    Font contentfont = new Font(bsFont, 13, Font.NORMAL);

                    #region   生成托管户划款指令
                    string moneyUpper = CommonFunctions.GetUpper(m.划款金额);
                    string dateStr = (m.划款日期.HasValue ? m.划款日期.Value.ToString("yyyy年MM月dd日") : "");
                    var sb = new System.Text.StringBuilder();
                    string title = m.托管户划款指令表名称 != m.募集户划款指令表名称 ? m.托管户划款指令表名称 : m.托管户划款指令表名称;
                    //设置托管户标题
                    document.Add(new Paragraph(" ", titleFont));
                    document.Add(new Paragraph(title, titleFont) { Alignment = Element.ALIGN_CENTER });
                    document.Add(new Paragraph(" ", contentfont));
                    //设置托管户表头
                    document.Add(new Paragraph($"日期:{dateStr}        ", contentfont) { Alignment = Element.ALIGN_RIGHT });
                    document.Add(new Paragraph(" ", contentfont));
                    //设置托管户表
                    PdfPTable table = new PdfPTable(2);
                    int[] TableWidths = { 45, 45 };//按百分比分配单元格宽带
                    table.SetWidths(TableWidths);
                    table.TotalWidth = 500;//设置绝对宽度
                    table.LockedWidth = true;//使绝对宽度模式生效
                    float BorderWidth = 0.5F;

                    WritePDF.WriteCell(table, contentfont, "付款户名:" + m.托管户户名, BorderWidth: BorderWidth, HorizontalAlignment: PdfPCell.ALIGN_LEFT, PaddingBottom: 10, r: 0);
                    WritePDF.WriteCell(table, contentfont, "收款户名:" + m.融资方户名, BorderWidth: BorderWidth, HorizontalAlignment: PdfPCell.ALIGN_LEFT, PaddingBottom: 10, r: 2);
                    WritePDF.WriteCell(table, contentfont, "付款账号:" + CommonFunctions.处理银行卡号(m.托管户账号), BorderWidth: BorderWidth, HorizontalAlignment: PdfPCell.ALIGN_LEFT, PaddingBottom: 10, r: 0);
                    WritePDF.WriteCell(table, contentfont, "收款账号:" + CommonFunctions.处理银行卡号(m.融资方账号), BorderWidth: BorderWidth, HorizontalAlignment: PdfPCell.ALIGN_LEFT, PaddingBottom: 10, r: 2);
                    WritePDF.WriteCell(table, contentfont, "开户行:" + m.托管户开户行, BorderWidth: BorderWidth, HorizontalAlignment: PdfPCell.ALIGN_LEFT, PaddingBottom: 10, r: 0, Rowspan: 2);
                    WritePDF.WriteCell(table, contentfont, "开户行:" + m.融资方开户行, BorderWidth: BorderWidth, HorizontalAlignment: PdfPCell.ALIGN_LEFT, PaddingBottom: 10, r: 2);
                    WritePDF.WriteCell(table, contentfont, "大额系统支付号:" + m.融资方大额支付号, BorderWidth: BorderWidth, HorizontalAlignment: PdfPCell.ALIGN_LEFT, PaddingBottom: 10, r: 2);
                    WritePDF.WriteCell(table, contentfont, "大写金额:人民币" + moneyUpper, BorderWidth: BorderWidth, HorizontalAlignment: PdfPCell.ALIGN_LEFT, PaddingBottom: 10, r: 0);
                    WritePDF.WriteCell(table, contentfont, "小写金额(单位:元):¥" + m.划款金额.ToString("c"), BorderWidth: BorderWidth, HorizontalAlignment: PdfPCell.ALIGN_LEFT, PaddingBottom: 10, r: 2);

                    sb.Clear();
                    int count = 3;
                    sb.AppendLine("资金用途:" + m.托管户资金用途);
                    for (int i = 0; i < count - m.托管户资金用途.Length / 15; i++) sb.AppendLine();
                    WritePDF.WriteCell(table, contentfont, sb.ToString(), BorderWidth: BorderWidth, HorizontalAlignment: PdfPCell.ALIGN_LEFT, r: 0);

                    sb.Clear();
                    sb.AppendLine("用款依据:");
                    for (int i = 0; i < count; i++) sb.AppendLine();
                    WritePDF.WriteCell(table, contentfont, sb.ToString(), BorderWidth: BorderWidth, HorizontalAlignment: PdfPCell.ALIGN_LEFT, r: 2);

                    sb.Clear();
                    sb.AppendLine("管理人经办人:");
                    for (int i = 0; i < count; i++) sb.AppendLine();
                    sb.AppendLine("管理人复核人:");
                    for (int i = 0; i < count; i++) sb.AppendLine();
                    sb.AppendLine("管理人签发人:");
                    for (int i = 0; i < count; i++) sb.AppendLine();
                    sb.AppendLine("管理人预留印鉴盖章处:");
                    for (int i = 0; i < count; i++) sb.AppendLine();

                    WritePDF.WriteCell(table, contentfont, sb.ToString(), BorderWidth: BorderWidth, HorizontalAlignment: PdfPCell.ALIGN_LEFT, r: 1);
                    sb.Clear();
                    sb.AppendLine("托管人经办人:");
                    for (int i = 0; i < count; i++) sb.AppendLine();
                    sb.AppendLine("托管人复核人:");
                    for (int i = 0; i < count; i++) sb.AppendLine();
                    sb.AppendLine("托管人审批人:");
                    for (int i = 0; i < count; i++) sb.AppendLine();
                    sb.AppendLine("托管人预留印鉴盖章处:");
                    for (int i = 0; i < count; i++) sb.AppendLine();
                    WritePDF.WriteCell(table, contentfont, sb.ToString(), BorderWidth: BorderWidth, HorizontalAlignment: PdfPCell.ALIGN_LEFT, r: 3);
                    document.Add(table);
                    #endregion

                    #region  生成募集户划款指令
                    if (m.募集户划款指令表名称.IsNotNullOrWhiteSpace() && m.募集户划款指令模板 == 0)
                    {
                        //恒丰募集户指令模板
                        document.NewPage();
                        string title_mj = m.募集户划款指令表名称;
                        //设置募集户标题
                        document.Add(new Paragraph(" ", titleFont));
                        document.Add(new Paragraph(title_mj, titleFont) { Alignment = Element.ALIGN_CENTER });
                        document.Add(new Paragraph(" ", contentfont));
                        //设置募集户表头
                        document.Add(new Paragraph($"日期:{dateStr}        ", contentfont) { Alignment = Element.ALIGN_RIGHT });
                        document.Add(new Paragraph(" ", contentfont));
                        //设置募集户表
                        PdfPTable table_mj = new PdfPTable(2);
                        int[] TableWidths_mj = { 45, 45 };//按百分比分配单元格宽带
                        table_mj.SetWidths(TableWidths_mj);
                        table_mj.TotalWidth = 500;//设置绝对宽度
                        table_mj.LockedWidth = true;//使绝对宽度模式生效
                        WritePDF.WriteCell(table_mj, contentfont, "付款户名:" + m.募集户户名, BorderWidth: BorderWidth, HorizontalAlignment: PdfPCell.ALIGN_LEFT, PaddingBottom: 10, r: 0);
                        WritePDF.WriteCell(table_mj, contentfont, "收款户名:" + m.托管户户名, BorderWidth: BorderWidth, HorizontalAlignment: PdfPCell.ALIGN_LEFT, PaddingBottom: 10, r: 2);
                        WritePDF.WriteCell(table_mj, contentfont, "付款账号:" + CommonFunctions.处理银行卡号(m.募集户账号), BorderWidth: BorderWidth, HorizontalAlignment: PdfPCell.ALIGN_LEFT, PaddingBottom: 10, r: 0);
                        WritePDF.WriteCell(table_mj, contentfont, "收款账号:" + CommonFunctions.处理银行卡号(m.托管户账号), BorderWidth: BorderWidth, HorizontalAlignment: PdfPCell.ALIGN_LEFT, PaddingBottom: 10, r: 2);
                        WritePDF.WriteCell(table_mj, contentfont, "开户行:" + m.募集户开户行, BorderWidth: BorderWidth, HorizontalAlignment: PdfPCell.ALIGN_LEFT, PaddingBottom: 10, r: 0);
                        WritePDF.WriteCell(table_mj, contentfont, "开户行:" + m.托管户开户行, BorderWidth: BorderWidth, HorizontalAlignment: PdfPCell.ALIGN_LEFT, PaddingBottom: 10, r: 2);
                        WritePDF.WriteCell(table_mj, contentfont, "付款金额(小写):¥" + m.划款金额.ToString("c"), BorderWidth: BorderWidth, HorizontalAlignment: PdfPCell.ALIGN_LEFT, PaddingBottom: 10, r: 0, Colspan: 2);
                        WritePDF.WriteCell(table_mj, contentfont, "付款金额(大写):人民币" + moneyUpper, BorderWidth: BorderWidth, HorizontalAlignment: PdfPCell.ALIGN_LEFT, PaddingBottom: 10, r: 0, Colspan: 2);

                        count = 5;
                        sb = sb.Clear();
                        sb.AppendLine("指令发出人信息栏:");
                        sb.AppendLine("要求到账时间:");
                        sb.AppendLine(string.Format("资金用途及用款依据:{0}", m.募集户资金用途));
                        sb.AppendLine("经办人:");
                        for (int i = 0; i < count; i++) sb.AppendLine();
                        sb.AppendLine("复核人:");
                        for (int i = 0; i < count; i++) sb.AppendLine();
                        sb.AppendLine("预留的有效印章:");
                        for (int i = 0; i < count; i++) sb.AppendLine();
                        WritePDF.WriteCell(table_mj, contentfont, sb.ToString(), BorderWidth: BorderWidth, HorizontalAlignment: PdfPCell.ALIGN_LEFT, r: 1);
                        sb.Clear();
                        sb.AppendLine("监督人反馈信息栏:");
                        sb.AppendLine("1、该指令已执行:");
                        sb.AppendLine("2、该指令未执行,原因如下:");
                        for (int i = 0; i < count; i++) sb.AppendLine();
                        sb.AppendLine("经办人:");
                        for (int i = 0; i < count; i++) sb.AppendLine();
                        sb.AppendLine("复核人:");
                        for (int i = 0; i < count; i++) sb.AppendLine();
                        WritePDF.WriteCell(table_mj, contentfont, sb.ToString(), BorderWidth: BorderWidth, HorizontalAlignment: PdfPCell.ALIGN_LEFT, r: 3);
                        WritePDF.WriteCell(table_mj, contentfont, "        重要提示:接此指令后,经审核无误应按照指令条款进行划款。", HorizontalAlignment: PdfPCell.ALIGN_LEFT, Colspan: 2);
                        document.Add(table_mj);

                    }
                    else if (m.募集户划款指令表名称.IsNotNullOrWhiteSpace() && m.募集户划款指令模板 == 1)
                    {
                        //生成中信募集户指令模板 委托付款通知书
                        document.NewPage();
                        title = "委托付款通知书";
                        //设置标题
                        document.Add(new Paragraph(20, title, titleFont) { Alignment = Element.ALIGN_CENTER });
                        document.Add(new Paragraph("划款时间:" + dateStr + "                                            单位:元", contentfont));
                        document.Add(new Paragraph(10, " ", contentfont));
                        PdfPTable row = new PdfPTable(2);
                        int[] TableWidth = { 50, 50 };//按百分比分配单元格宽带
                        row.SetWidths(TableWidth);
                        row.TotalWidth = 500;//设置绝对宽度
                        row.LockedWidth = true;//使绝对宽度模式生效

                        WritePDF.WriteCell(row, contentfont, "监督协议编号:SH-SM-SF0046", BorderWidth, PdfPCell.ALIGN_MIDDLE, PdfPCell.ALIGN_LEFT, 2, 0, 0, 0, 10, 0, 1);
                        WritePDF.WriteCell(row, contentfont, "付款户名:共青城祥腾兴投资管理合伙企业(有限合伙)", BorderWidth, PdfPCell.ALIGN_MIDDLE, PdfPCell.ALIGN_LEFT, 0, 0, 0, 0, 10, 0, 1);
                        WritePDF.WriteCell(row, contentfont, "收款户名:共青城祥腾兴投资管理合伙企业(有限合伙)", BorderWidth, PdfPCell.ALIGN_MIDDLE, PdfPCell.ALIGN_LEFT, 0, 0, 0, 0, 10, 2, 1);
                        WritePDF.WriteCell(row, contentfont, "付款账号:9050301201900037720118", BorderWidth, PdfPCell.ALIGN_MIDDLE, PdfPCell.ALIGN_LEFT, 0, 0, 0, 0, 10, 0, 1);
                        WritePDF.WriteCell(row, contentfont, "收款账号:8111401013600363037", BorderWidth, PdfPCell.ALIGN_MIDDLE, PdfPCell.ALIGN_LEFT, 0, 0, 0, 0, 10, 2, 1);
                        WritePDF.WriteCell(row, contentfont, "开户行:温州银行上海徐汇支行", BorderWidth, PdfPCell.ALIGN_MIDDLE, PdfPCell.ALIGN_LEFT, 0, 0, 0, 0, 10, 0, 1);
                        WritePDF.WriteCell(row, contentfont, "开户行:中信银行天津分行营业部", BorderWidth, PdfPCell.ALIGN_MIDDLE, PdfPCell.ALIGN_LEFT, 0, 0, 0, 0, 10, 2, 1);
                        WritePDF.WriteCell(row, contentfont, "大写金额:" + moneyUpper, BorderWidth, PdfPCell.ALIGN_MIDDLE, PdfPCell.ALIGN_LEFT, 0, 0, 0, 0, 10, 0, 1);
                        //祥腾兴募集转托管的指令是没有千位符号 别的所有的指令都有的
                        WritePDF.WriteCell(row, contentfont, "小写金额:¥" + m.划款金额, BorderWidth, PdfPCell.ALIGN_MIDDLE, PdfPCell.ALIGN_LEFT, 0, 0, 0, 0, 10, 2, 1);

                        sb = new StringBuilder();
                        sb.AppendLine(" 用途及备注:");
                        sb.AppendLine(" " + m.募集户资金用途);
                        sb.AppendLine();
                        sb.AppendLine();
                        sb.AppendLine();
                        sb.AppendLine();
                        sb.AppendLine();
                        sb.AppendLine();
                        sb.AppendLine();
                        WritePDF.WriteCell(row, contentfont, sb.ToString(), BorderWidth, PdfPCell.ALIGN_MIDDLE, PdfPCell.ALIGN_LEFT, 0, 0, 0, 0, 10, 0, 1);

                        sb = new StringBuilder();
                        sb.AppendLine(" 签发人:");
                        sb.AppendLine();
                        sb.AppendLine();
                        sb.AppendLine(" 预留印章:");
                        sb.AppendLine();
                        sb.AppendLine();
                        sb.AppendLine();
                        sb.AppendLine("             年      月      日             ");
                        sb.AppendLine();
                        WritePDF.WriteCell(row, contentfont, sb.ToString(), BorderWidth, PdfPCell.ALIGN_MIDDLE, PdfPCell.ALIGN_LEFT, 0, 0, 0, 0, 10, 2, 1);

                        WritePDF.WriteCell(row, contentfont, "监督人指令执行情况", BorderWidth, PdfPCell.ALIGN_MIDDLE, PdfPCell.ALIGN_CENTER, 2, 0, 0, 0, 10, 0, 1);

                        sb = new StringBuilder();
                        sb.AppendLine(" 指令处理岗办理审批意见:");
                        sb.AppendLine();
                        sb.AppendLine(" 经办:");
                        sb.AppendLine();
                        sb.AppendLine(" 复核:");
                        sb.AppendLine();
                        sb.AppendLine(" 负责人:");
                        sb.AppendLine();
                        sb.AppendLine();
                        sb.AppendLine("                                                        年      月      日             ");
                        sb.AppendLine();
                        WritePDF.WriteCell(row, contentfont, sb.ToString(), BorderWidth, PdfPCell.ALIGN_LEFT, PdfPCell.ALIGN_LEFT, 4, 0, 0, 0, 10, 1, 1);
                        document.Add(row);//将表格添加到pdf文档中
                        #endregion

                        #region 临开函
                        document.NewPage();
                        title = "关于共青城祥腾兴投资管理合伙企业(有限合伙)新增临时开放日的公告";
                        //设置标题
                        document.Add(new Paragraph(80, title, titleFont) { Alignment = Element.ALIGN_CENTER });
                        document.Add(new Paragraph(40, " ", contentfont));
                        document.Add(new Paragraph("尊敬的客户:", contentfont) { IndentationLeft = 50 });

                        var 划款日前一工作日 = CommonFunctions.跳过法定节假日加(db, m.划款日期.Value, -1);
                        var Para1 = new Paragraph(20);
                        Para1.IndentationLeft = 50;
                        Para1.IndentationRight = 50;
                        Para1.Font = contentfont;
                        Para1.Add(WritePDF.CreateSongtiNomalChunk("    根据《共青城祥腾兴投资管理合伙企业(有限合伙)》合伙协议的约定,本管理人现根据基金运作情况的需要,现新增"));
                        Para1.Add(WritePDF.CreateSongtiUnderLineChunk(划款日前一工作日.Year.ToString()));
                        Para1.Add(WritePDF.CreateSongtiNomalChunk("年"));
                        Para1.Add(WritePDF.CreateSongtiUnderLineChunk(划款日前一工作日.Month.ToString()));
                        Para1.Add(WritePDF.CreateSongtiNomalChunk("月"));
                        Para1.Add(WritePDF.CreateSongtiUnderLineChunk(划款日前一工作日.Day.ToString()));
                        Para1.Add(WritePDF.CreateSongtiNomalChunk("日至"));
                        Para1.Add(WritePDF.CreateSongtiUnderLineChunk(划款日前一工作日.Year.ToString()));
                        Para1.Add(WritePDF.CreateSongtiNomalChunk("年"));
                        Para1.Add(WritePDF.CreateSongtiUnderLineChunk(划款日前一工作日.Month.ToString()));
                        Para1.Add(WritePDF.CreateSongtiNomalChunk("月"));
                        Para1.Add(WritePDF.CreateSongtiUnderLineChunk(划款日前一工作日.Day.ToString()));
                        Para1.Add(WritePDF.CreateSongtiNomalChunk("日为本基金的临时开放日,临时开放日受理基金"));
                        Para1.Add(WritePDF.CreateSongtiUnderLineChunk(" 申购 "));
                        Para1.Add(WritePDF.CreateSongtiNomalChunk("。"));
                        document.Add(Para1);

                        document.Add(new Paragraph("    此次临时开放日后,基金开放日仍按基金合同原先的约定执行。", contentfont) { IndentationLeft = 50 });
                        document.Add(new Paragraph("    特此公告。", contentfont) { IndentationLeft = 50 });
                        document.Add(new Paragraph(100, " "));
                        document.Add(new Paragraph("上海祥乾投资管理有限公司  ", contentfont) { Alignment = Element.ALIGN_RIGHT });
                        document.Add(new Paragraph(划款日前一工作日.ToString("yyyy年MM月dd日") + "  ", contentfont) { Alignment = Element.ALIGN_RIGHT });
                    }
                    #endregion
                }
            }

生成之后长这样

发布了45 篇原创文章 · 获赞 4 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_36730649/article/details/98623236