需求:对数据库的数据读取后进行页面显示,当然是表格形式,那么如果我想对其进行转化成PDF打印输出,又该如何?
一点不错,生成PDF,网上有很多方法,大部分都是itexsharp,在这里,我又整理了一下。
首先引入ICSharpCode.SharpZipLib.dll和Itextsharp.dll,网上可以下载到,很多,我感觉只引入Itextsharp.dll就足够了,均开源。
添加button按钮。代码呈上:
代码
protected void Button1_Click(object sender, EventArgs e)
{
string str1= "小潘";
string str2 = "男";
Document document = new Document();
try{
PdfWriter.GetInstance(document, new FileStream(Server.MapPath("test.pdf"), FileMode.Create));
document.Open();
BaseFont bfChinese = BaseFont.CreateFont("C:\\WINDOWS\\Fonts\\simsun.ttc,1", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
Font fontChinese = new Font(bfChinese, 12, Font.NORMAL, new Color(0, 0, 0));
PdfPTable pdftable = new PdfPTable(2); //设置成2列
pdftable.SetWidths(new float[] { 0.45f, 0.65f });
pdftable.WidthPercentage = 95; //表格占页面的百分比
// //完成第一行的两列,需合并单元格
PdfPCell tablename = new PdfPCell(new Paragraph("人员基本情况", fontChinese));
tablename.UseAscender = true;
tablename.HorizontalAlignment = Element.ALIGN_MIDDLE;
tablename.Colspan = 2;//合并单元格
pdftable.AddCell(tablename); //在pdftable中加入这个单元格
// 完成第二行的两列
PdfPCell namecell = new PdfPCell(new Paragraph("姓名:", fontChinese)); //名称
namecell.UseAscender = true;
namecell.HorizontalAlignment = Element.ALIGN_MIDDLE;
pdftable.AddCell(namecell); //在pdftable中加入这个单元格
PdfPCell namevalcell = new PdfPCell(new Paragraph(str1, fontChinese)); //"姓名的值"
namevalcell.UseAscender = true; //可写可不写
namevalcell.HorizontalAlignment = Element.ALIGN_MIDDLE;
pdftable.AddCell(namevalcell);
//完成第三行的两列
PdfPCell namesexcell = new PdfPCell(new Paragraph("姓别:", fontChinese)); //名称
namesexcell.UseAscender = true;
namesexcell.HorizontalAlignment = Element.ALIGN_MIDDLE;
pdftable.AddCell(namesexcell); //在pdftable中加入这个单元格
PdfPCell namsexevalcell = new PdfPCell(new Paragraph(str2, fontChinese)); //"姓名的值"
namsexevalcell.UseAscender = true; //可写可不写
namsexevalcell.HorizontalAlignment = Element.ALIGN_MIDDLE;
pdftable.AddCell(namsexevalcell);
document.Add(pdftable);
}
catch(Exception ex){
}
finally{
document.Close();
}
Response.Redirect("test.pdf");
}
{
string str1= "小潘";
string str2 = "男";
Document document = new Document();
try{
PdfWriter.GetInstance(document, new FileStream(Server.MapPath("test.pdf"), FileMode.Create));
document.Open();
BaseFont bfChinese = BaseFont.CreateFont("C:\\WINDOWS\\Fonts\\simsun.ttc,1", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
Font fontChinese = new Font(bfChinese, 12, Font.NORMAL, new Color(0, 0, 0));
PdfPTable pdftable = new PdfPTable(2); //设置成2列
pdftable.SetWidths(new float[] { 0.45f, 0.65f });
pdftable.WidthPercentage = 95; //表格占页面的百分比
// //完成第一行的两列,需合并单元格
PdfPCell tablename = new PdfPCell(new Paragraph("人员基本情况", fontChinese));
tablename.UseAscender = true;
tablename.HorizontalAlignment = Element.ALIGN_MIDDLE;
tablename.Colspan = 2;//合并单元格
pdftable.AddCell(tablename); //在pdftable中加入这个单元格
// 完成第二行的两列
PdfPCell namecell = new PdfPCell(new Paragraph("姓名:", fontChinese)); //名称
namecell.UseAscender = true;
namecell.HorizontalAlignment = Element.ALIGN_MIDDLE;
pdftable.AddCell(namecell); //在pdftable中加入这个单元格
PdfPCell namevalcell = new PdfPCell(new Paragraph(str1, fontChinese)); //"姓名的值"
namevalcell.UseAscender = true; //可写可不写
namevalcell.HorizontalAlignment = Element.ALIGN_MIDDLE;
pdftable.AddCell(namevalcell);
//完成第三行的两列
PdfPCell namesexcell = new PdfPCell(new Paragraph("姓别:", fontChinese)); //名称
namesexcell.UseAscender = true;
namesexcell.HorizontalAlignment = Element.ALIGN_MIDDLE;
pdftable.AddCell(namesexcell); //在pdftable中加入这个单元格
PdfPCell namsexevalcell = new PdfPCell(new Paragraph(str2, fontChinese)); //"姓名的值"
namsexevalcell.UseAscender = true; //可写可不写
namsexevalcell.HorizontalAlignment = Element.ALIGN_MIDDLE;
pdftable.AddCell(namsexevalcell);
document.Add(pdftable);
}
catch(Exception ex){
}
finally{
document.Close();
}
Response.Redirect("test.pdf");
}
经测试,代码可用,PDF效果如下(至于其中的字体和表头等,那是后话):