c# 使用 itextsharp 实现生成Pdf报表

由于项目需要,所以学习Itextsharp   此项目需求是   某一角色提交申请,然后从后台查出数据生成pdf报表

打印出来用于查看 以下是代码:

 

string sql = "select * from table " // sql 查询数据 

DataTable dt = Medalsoft.ServiceMe.ClassLibrary.SQLHelper.ExecuteDt(sql);

if (dt.Rows.Count == 0) //对查询结果做判断 没有数据就不生成PDF
{
return "dataNull";
}

Stream myStream;//文件流

string PdfFilePath = "/costPdf";  这是存放的文件夹名称  服务器中的
string temppath = HttpContext.Current.Server.MapPath(PdfFilePath);
if (System.IO.Directory.Exists(temppath) == false)//如果不存在就创建文件夹
{
System.IO.Directory.CreateDirectory(HttpContext.Current.Server.MapPath(PdfFilePath));
}

string finalPath = PdfFilePath + "/" + "expense.pdf";//获取保存后的路径  (服务器中)
string pdfPath = HttpContext.Current.Server.MapPath(PdfFilePath) + "/" + "expense.pdf"; //物理文件路径 
if(System.IO.Directory.Exists(pdfPath) == true)  //文件已存在就删除  以此达到覆盖文件的效果
{
File.Delete(pdfPath);
}
myStream = new FileStream(pdfPath, FileMode.OpenOrCreate); //打开文件并赋给IO流myStream

//string imgPath = HttpContext.Current.Server.MapPath(PdfFilePath) + "/" + "shade.png";//图片路径 阴影图片 填充不需要的单元格

//iTextSharp.text.Image img1 = iTextSharp.text.Image.GetInstance(imgPath);

Document document = new Document(PageSize.A4.Rotate()); //创建A4纸、横向PDF文档 默认纵向
PdfWriter writer = PdfWriter.GetInstance(document, myStream); //将PDF文档写入创建的文件中
document.Open();
//要在PDF文档中写入中文必须指定中文字体,否则无法写入中文
BaseFont bftitle = BaseFont.CreateFont("C:\\Windows\\Fonts\\SIMHEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); //用系统中的字体文件SimHei.ttf创建文件字体
iTextSharp.text.Font fonttitle = new iTextSharp.text.Font(bftitle, 20); //标题字体,大小30
BaseFont bf1 = BaseFont.CreateFont("C:\\Windows\\Fonts\\SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); //用系统中的字体文件SimSun.ttc创建文件字体
iTextSharp.text.Font CellFont = new iTextSharp.text.Font(bf1, 12); //单元格中的字体,大小12
iTextSharp.text.Font fonttitle2 = new iTextSharp.text.Font(bf1, 15); //副标题字体,大小15

//添加标题
Paragraph Title = new Paragraph("上海长濑贸易有限公司费用报销凭证", fonttitle); //添加段落,第二个参数指定使用fonttitle格式的字体,写入中文必须指定字体否则无法显示中文
Title.Alignment = iTextSharp.text.Rectangle.ALIGN_CENTER; //设置居中
document.Add(Title); //将标题段加入PDF文档中

//空一行
Paragraph null1 = new Paragraph(" ", fonttitle2);
null1.Leading = 20;  //此数值用于调整空白大小
document.Add(null1);

List<string> list = new List<string>();
double num1, num2, num3, num4, num5;
double sum = 0;
for (int i = 0; i < dt.Rows.Count; i++)
{
string date = dt.Rows[i]["字段名"].ToString().Substring(0, 10);
list.Add(date);
string dept = dt.Rows[i]["字段名"].ToString();
list.Add(dept);
 

 
}


Chunk underline = new Chunk(" ");
underline.SetUnderline(0.1f, -1f);
Chunk underline1 = new Chunk(" ");
underline1.SetUnderline(0.1f, -1f);
 Paragraph apply = new Paragraph("申请日:", fonttitle2);
apply.Leading = 10;
apply.FirstLineIndent = 75;
apply.Add(underline);
apply.Add(new Chunk("年"));
apply.Add(underline);
apply.Add(new Chunk("月"));
apply.Add(underline);
apply.Add(new Chunk("日 申请人:"));
apply.Add(underline1);
document.Add(apply);

//上面这一段主要是添加下划线 达到“申请日_______年______月________日”的效果  由于使用输入法的话打印出来会有间隔,所以使用这种方法


//空一行
Paragraph nulll = new Paragraph(" ", fonttitle2);
nulll.Leading = 20;
document.Add(nulll);


PdfPTable table = new PdfPTable(xxx); // 括号中 为用户设置的列数,创建xxx列的表格,行会根据写入数据自动扩展

table.SetWidths(new int []) ;//表格每列宽度  参数为数组
table.WidthPercentage = 100;//表格总宽度100%


string[] tableHeader = { "字段名", "字段名", };//此处以两个为例  
PdfPCell cell1;
foreach (string th in tableHeader)
{
cell1 = new PdfPCell(new Phrase(th, CellFont));
cell1.HorizontalAlignment = Element.ALIGN_CENTER;//单元格水平居中
cell1.VerticalAlignment = Element.ALIGN_MIDDLE;//单元格垂直居中

 cel.MinimumHeight = 44; //设置单元格高度

table.AddCell(cell1);
}
foreach (string td in list)
{
cell1 = new PdfPCell(new Phrase(td, CellFont));
cell1.HorizontalAlignment = Element.ALIGN_CENTER;//单元格水平居中
cell1.VerticalAlignment = Element.ALIGN_MIDDLE;//单元格垂直居中

 cel.MinimumHeight = 44;

table.AddCell(cell1);

}

//表格最后一行
PdfPCell cell;
cell = new PdfPCell(new Phrase("合计", CellFont));
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.Colspan = 2;//单元格合并列
table.AddCell(cell);
PdfPCell cel2 = new PdfPCell();
cel2.BackgroundColor = new BaseColor(128, 128, 128); //这里是设置单元格背景色  表示一些cell 不需要填数据
for (int i = 0; i < 5; i++)
{
table.AddCell(cel2);
}
PdfPCell cel;
cel = new PdfPCell(new Phrase(sum.ToString("f2"), CellFont));
cel.VerticalAlignment = Element.ALIGN_MIDDLE;
cel.HorizontalAlignment = Element.ALIGN_CENTER;
table.AddCell(cel);
PdfPCell cel3 = new PdfPCell(new Phrase(" "));
cel3.BackgroundColor = new BaseColor(128,128,128);
table.AddCell(cel3);

document.Add(table);
PdfPTable table1 = new PdfPTable(4);
//空一行
Paragraph null2 = new Paragraph(" ", fonttitle2);
null2.Leading = 10;
document.Add(null2);

PdfPCell cell2;
string[] tableHeader1 = { "1 (COO)", "2 (TRE)", "3 (财务)", "4" };


string[] result = { "审批:", "审批:", "审核:", "领款人:" };//此数组需要拼接数据
foreach (string th1 in tableHeader1)
{
cell2 = new PdfPCell(new Phrase(th1, CellFont));
cell2.HorizontalAlignment = Element.ALIGN_CENTER;
cell2.VerticalAlignment = Element.ALIGN_MIDDLE;
table1.AddCell(cell2);
}
PdfPCell cell3;
foreach (string th1 in result)
{
cell3 = new PdfPCell(new Phrase(th1, CellFont));
cell3.VerticalAlignment = Element.ALIGN_MIDDLE;
cell3.MinimumHeight = 40;
table1.AddCell(cell3);
}
document.Add(table1);
//空一行
Paragraph null3 = new Paragraph(" ", fonttitle2);
null3.Leading = 10;
document.Add(null3);

Paragraph remark = new Paragraph("备注:3000元以上必须COO审批。", CellFont);
remark.Leading = 10;
remark.FirstLineIndent = 75;
document.Add(remark);

 

document.Close(); myStream.Close();

posted @ 2017-09-12 16:04  最爱吃丨豆腐  阅读(1781)  评论(0编辑  收藏  举报