记录一个.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 = "";
private static readonly String cnNumber = "零壹贰叁肆伍陆柒捌玖";
private static readonly String cnUnit = "分角元拾佰仟万拾佰仟亿拾佰仟兆拾佰仟";
/// <summary>
/// 转换人民币大小金额
/// </summary>
/// <param name="num">金额</param>
/// <returns>返回大写形式</returns>
public static string GetUpper(decimal num)
{
String[] tmpString = num.ToString().Split('.');
String intString = num.ToString(); // 默认为整数
String decString = ""; // 保存小数部分字串
String rmbCapital = ""; // 保存中文大写字串
int k;
int j;
int n;
if (tmpString.Length > 1)
{
intString = tmpString[0]; // 取整数部分
decString = tmpString[1]; // 取小数部分
}
decString += "00";
decString = decString.Substring(0, 2); // 保留两位小数位
intString += decString;
try
{
k = intString.Length - 1;
if (k > 0 && k < 18)
{
for (int i = 0; i <= k; i++)
{
j = (int)intString[i] - 48;
// rmbCapital = rmbCapital + cnNumber[j] + cnUnit[k-i]; // 供调试用的直接转换
n = i + 1 >= k ? (int)intString[k] - 48 : (int)intString[i + 1] - 48; // 等效于 if( ){ }else{ }
if (j == 0)
{
if (k - i == 2 || k - i == 6 || k - i == 10 || k - i == 14)
{
rmbCapital += cnUnit[k - i];
}
else
{
if (n != 0)
{
rmbCapital += cnNumber[j];
}
}
}
else
{
rmbCapital = rmbCapital + cnNumber[j] + cnUnit[k - i];
}
}
rmbCapital = rmbCapital.Replace("兆亿万", "兆");
rmbCapital = rmbCapital.Replace("兆亿", "兆");
rmbCapital = rmbCapital.Replace("亿万", "亿");
rmbCapital = rmbCapital.TrimStart('元');
rmbCapital = rmbCapital.TrimStart('零');
string lastStr = rmbCapital.Substring(rmbCapital.Length - 1);
if (lastStr == "元" || lastStr == "角")
rmbCapital += "整";
return rmbCapital;
}
else
{
return ""; // 超出转换范围时,返回零长字串
}
}
catch
{
return ""; // 含有非数值字符时,返回零长字串
}
}
#region 对文件绘制表格
/// <summary>
/// 对文件绘制表格
/// </summary>
/// <param name="row"></param>
/// <param name="font"></param>
/// <param name="Content"></param>
/// <param name="BorderWidth"></param>
/// <param name="VerticalAlignment"></param>
/// <param name="HorizontalAlignment"></param>
/// <param name="Colspan"></param>
/// <param name="PaddingLeft"></param>
/// <param name="PaddingTop"></param>
/// <param name="PaddingRight"></param>
/// <param name="PaddingBottom"></param>
/// <param name="r"></param>
/// <param name="setLeading"></param>
/// <param name="Rowspan"></param>
/// <param name="BorderWidthTop"></param>
public static void WriteCell(PdfPTable row, Font font, string Content, float BorderWidth = 0, int VerticalAlignment = PdfPCell.ALIGN_MIDDLE, int HorizontalAlignment = PdfPCell.ALIGN_CENTER, int Colspan = 0, float PaddingLeft = 5, float PaddingTop = 5, float PaddingRight = 5, float PaddingBottom = 5, int r = 0, float setLeading = 0.6F, int Rowspan = 0, int BorderWidthTop = 1)
{
PdfPCell cell = new PdfPCell(new Paragraph(Content, font));
//设置行间距
cell.SetLeading(10, setLeading);
if (Colspan > 0)
cell.Colspan = Colspan;
if (Rowspan > 0)
cell.Rowspan = Rowspan;
cell.VerticalAlignment = VerticalAlignment;
cell.HorizontalAlignment = HorizontalAlignment;
//避免重复画边框导致过粗
if (BorderWidth != 0)
{
if (BorderWidthTop > 0)
cell.BorderWidthTop = BorderWidth;
else
cell.BorderWidthTop = Rectangle.NO_BORDER;
cell.BorderWidthRight = BorderWidth;
if (r == 0)
{
cell.BorderWidthLeft = BorderWidth;
cell.BorderWidthBottom = Rectangle.NO_BORDER;
}
if (r == 1)
{
cell.BorderWidthLeft = BorderWidth;
cell.BorderWidthBottom = BorderWidth;
}
if (r == 2)
{
cell.BorderWidthLeft = Rectangle.NO_BORDER;
cell.BorderWidthBottom = Rectangle.NO_BORDER;
}
if (r == 3)
{
cell.BorderWidthLeft = Rectangle.NO_BORDER;
cell.BorderWidthBottom = BorderWidth;
}
}
else
cell.Border = Rectangle.NO_BORDER;
cell.PaddingLeft = PaddingLeft;
cell.PaddingRight = PaddingRight;
cell.PaddingTop = PaddingTop;
cell.PaddingBottom = PaddingBottom;
row.AddCell(cell);//将单元格添加到表格中
}
#endregion
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
}
}
生成之后长这样