/// <summary>
/// 点击打印按钮
/// </summary>
/// <remarks>
/// <para>
/// </para>
/// </remarks>
protected void Pdf_BT_Click(object sender, EventArgs e)
{
if (Convert.ToInt32(ScrechNumber_LB.Text) == 0 || Convert.ToInt32(ScrechNumber_LB.Text)>100)
{
//弹出提示框
this.Page.RegisterStartupScript("", "<script>alert(\"当查询到记录不为0条或不超过100条时才可以打印!\")</script>");
//跳出按钮事件
return;
}
//在跟踪LOG中写入信息
Log4netWriter.Debug(aspxName, "Pdf_BT_Click():Start!!!");
//定义一个数据流对象并实例化它
MemoryStream m = new MemoryStream();
//定义文档的类型为纵向A4
Document document = new Document(PageSize.A4);
//读取资源文件信息
String AddTitle = "";
String AddKeywords = "";
String AddCreator= "";
String AddSubject= "";
String AddAuthor= "";
String title_LB = "";
String Node = "";
String Message_info = "";
String datatime = "";
//当是中文的时候,赋PDF文档中的字段值中文
if ("zh-cn".Equals(Session["Language"].ToString()))
{
AddTitle = (String)HttpContext.GetGlobalResourceObject("Print", "AddTitle");
AddKeywords = (String)HttpContext.GetGlobalResourceObject("Print", "AddKeywords");
AddCreator = (String)HttpContext.GetGlobalResourceObject("Print", "AddCreator");
AddSubject = (String)HttpContext.GetGlobalResourceObject("Print", "AddSubject");
AddAuthor = (String)HttpContext.GetGlobalResourceObject("Print", "AddAuthor");
title_LB = (String)HttpContext.GetGlobalResourceObject("Print", "Title");
Node=(String)HttpContext.GetGlobalResourceObject("Print", "Node");
Message_info = (String)HttpContext.GetGlobalResourceObject("Print", "Message_info");
datatime = (String)HttpContext.GetGlobalResourceObject("Print", "datatime");
}
//当是韩文的时候,,赋PDF文档中的字段值为韩文
else if ("ko".Equals(Session["Language"].ToString()))
{
AddTitle = GetResource.ChangeGrobalLanguage("Print", "AddTitle","zh-cn");
AddKeywords = GetResource.ChangeGrobalLanguage("Print", " AddKeywords", "zh-cn");
AddCreator = GetResource.ChangeGrobalLanguage("Print", "AddCreator", "zh-cn");
AddSubject = GetResource.ChangeGrobalLanguage("Print", "AddSubject", "zh-cn");
AddAuthor = GetResource.ChangeGrobalLanguage("Print", "AddAuthor", "zh-cn");
title_LB = GetResource.ChangeGrobalLanguage("Print", "Title", "zh-cn");
Node = GetResource.ChangeGrobalLanguage("Print", "Node", "zh-cn");
Message_info = GetResource.ChangeGrobalLanguage("Print", " Message_info", "zh-cn");
datatime = GetResource.ChangeGrobalLanguage("Print", " datatime ", "zh-cn");
}
try
{
//定义Documnet创建一个Writer实例
PdfWriter writer = PdfWriter.GetInstance(document, m);
//定义生成的PDF文档标题
document.AddTitle(AddTitle);
//定义文档的关键字
document.AddKeywords(AddKeywords);
//定义文档的应用程序
document.AddCreator(AddCreator);
//定义文档的主题
document.AddSubject(AddSubject);
//定义文档的标头
document.AddHeader("Expires","0");
//定义文档的作者
document.AddAuthor(AddAuthor);
//打开文档
document.Open();
//定义插入的输入数据为字体对象chinese为非嵌入式横字黑体
BaseFont chinese = BaseFont.CreateFont(Server.MapPath("~/Bin/Sun-ExtA.ttf"), BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//定义的内容的对象
PdfContentByte cb=writer.DirectContent ;
//定义文档摸版的高度和宽度
PdfTemplate template=cb.CreateTemplate(50,50);
//定义字体为为大小12的汉字
iTextSharp.text.Font font12 = new iTextSharp.text.Font(chinese, 12);
//定义字体为为大小28的汉字
iTextSharp.text.Font font28 = new iTextSharp.text.Font(chinese, 28);
//表格制作参数,第一个段
//定义个空的段
Paragraph paraGraph = null;
//定义个空的表
iTextSharp.text.Table table = null;
//定义一个为空的单元
Cell cell = null;
//对段的初始化,把title_LB也就是标题中的值添加到文档中
paraGraph = new Paragraph(title_LB, font28);
//定义段的对齐方式
paraGraph.Alignment = Element.ALIGN_CENTER;
//把段添加到文档中
document.Add(paraGraph);
//表格制作参数,第二个段
//定义个空的段
Paragraph paraGraph_1 = null;
//定义个空的表
iTextSharp.text.Table table_1 = null;
//定义一个为空的单元
Cell cell_1 = null;
String time = "";
time = (String)DateTime.Now.ToString();
//对段的初始化,把5个空格添加到文档中
paraGraph_1 = new Paragraph(" "+time+" ", font12);
//定义段的对齐方式
paraGraph_1.Alignment = Element.ALIGN_CENTER;
//把段添加到文档中
document.Add(paraGraph_1);
//表格制作参数,第三个段
//定义个空的段
Paragraph paraGraph_2 = null;
//定义个空的表
iTextSharp.text.Table table_2 = null;
//定义一个为空的单元
Cell cell_2 = null;
//对段的初始化,把5个空格添加到文档中
paraGraph_2 = new Paragraph(" ", font12);
//定义段的对齐方式
paraGraph_2.Alignment = Element.ALIGN_CENTER;
//把段添加到文档中
document.Add(paraGraph_2);
//定义一个18栏的表
table = new iTextSharp.text.Table(18);
//定义表为自动换页
table.CellsFitPage = true;
//定义表的长度
table.AbsWidth = "540";
//定义表的外框线宽度
table.BorderWidth = 1;
//定义表的内部线宽度
table.DefaultCellBorderWidth = 1;
//定义表的位置为左右居中,上下居中
table.DefaultHorizontalAlignment = Element.ALIGN_CENTER;
table.DefaultVerticalAlignment = Element.ALIGN_MIDDLE;
//定义表的字符与上个段底线的距离
table.Padding = 3;
//定义表的字符与字符距离
table.Spacing = 0;
//定义表的字符与距上边垂直距离
table.Offset = 30;
//定义表中标题内容以及位置
table = this.creatListTable(18, chinese, 12, Node, Message_info, datatime);
//赋值给PDF用到的数据源
GridViewBound();
//赋值数据赋值给ds
DataSet ds = tableds;
//定义INT型变量
int rowCount = 0;
//当数据不为空的时候,把数据的记录数赋值变量
if (ds != null)
{
rowCount = ds.Tables[0].Rows.Count;
}
//判断到记录的结尾时,数据停止输入PDF中的表
for (int i = 1; i < rowCount+1; i++)
{
//标签项(非用户输入项)
//要使用粗体
font12 = new iTextSharp.text.Font(chinese, 12, iTextSharp.text.Font.BOLD);
//定义段的内容,把数据一行第一个数据添加到段中
paraGraph = new Paragraph(ds.Tables[0].Rows[i-1][0].ToString(), font12);
//把段输入到单元格中
cell = new Cell(paraGraph);
//是否为递增
cell.UseAscender = true;
//定义单元的位置为左右居中,上下居中
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
cell.HorizontalAlignment = Element.ALIGN_CENTER;
//定义单元的周线的宽度
cell.BorderWidth = 1;
//单元格占的1行数和3列数
cell.Rowspan = 1;
cell.Colspan = 3;
//定义单元的坐标
table.AddCell(cell, new Point(i, 0));
//定义段的内容,把数据一行第二个数据添加到段中
paraGraph = new Paragraph(ds.Tables[0].Rows[i-1][1].ToString(), font12);
//定义把段中的内容,添加到单元格中
cell = new Cell(paraGraph);
cell.UseAscender = true;
//定义单元的位置为左右居中,上下居中
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
cell.HorizontalAlignment = Element.ALIGN_CENTER;
//定义单元的周线的宽度
cell.BorderWidth = 1;
//单元格占的1行数和12列数
cell.Rowspan = 1;
cell.Colspan = 12;
//定义单元的坐标
table.AddCell(cell, new Point(i, 3));
//定义段的内容,把数据一行第三个数据添加到段中
paraGraph = new Paragraph(((DateTime)ds.Tables[0].Rows[i - 1][2]).ToShortDateString(), font12);
//定义把段中的内容,添加到单元格中
cell = new Cell(paraGraph);
//是否为递增
cell.UseAscender = true;
//定义单元的位置为左右居中,上下居中
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
cell.HorizontalAlignment = Element.ALIGN_CENTER;
//定义单元的周线的宽度
cell.BorderWidth = 1;
//单元格占的1行数和3列数
cell.Rowspan = 1;
cell.Colspan = 3;
//定义单元的坐标
table.AddCell(cell, new Point(i, 15));
//判断当文档中的表不是第一个页时,加如标题和页数,内容等等
if (!writer.FitsPage(table))
{
//把表中最后一行空白行删除
table.DeleteLastRow();
//变量i递减
i--;
//把表添加到文档中
document.Add(table);
//定义个字符型变量把本页的页数赋值给这个变量
String text = "" + writer.PageNumber + "/";
//定义页数的字体大小
float len = chinese.GetWidthPoint(text, 14);
//利用ContentByte对象设置页数字体和尺寸,显示的位置,并把标题添加到每页中
cb.BeginText();
cb.SetFontAndSize(chinese, 14);
cb.SetTextMatrix(540, 820);
cb.ShowText(text);
cb.EndText();
cb.AddTemplate(template, 540 + len, 820);
document.NewPage();
table = this.creatListTable(128, chinese, 12, Node, Message_info, datatime);
}
}
//利用ContentByte对象设置第一页的页数字体和尺寸,显示的位置
String text1 = "" + writer.PageNumber + "/";
float len2 = chinese.GetWidthPoint(text1, 12);
cb.BeginText();
cb.SetFontAndSize(chinese, 14);
cb.SetTextMatrix(540, 820);
cb.ShowText(text1);
cb.EndText();
cb.AddTemplate(template, 540 + len2, 820);
//设置摸版的页数字体并显示
template.BeginText();
template.SetFontAndSize(chinese, 14);
template.ShowText((writer.PageNumber).ToString());
template.EndText();
//把表添加到文本中
document.Add(table);
}
catch (DocumentException de)
{
//把de异常显示到错误LOG中
Log4netWriter.Error(aspxName, de.Message);
}
catch (IOException ioe)
{
//把ioe异常显示到错误LOG中
Log4netWriter.Error(aspxName, ioe.Message);
}
//关闭文档
document.Close();
//清除HTTP返回数缓存中的内容
Response.Buffer = true;
Response.ExpiresAbsolute = DateTime.Now.AddSeconds(-1);
Response.Expires = 0;
Response.CacheControl = "no-cache";
Response.Cache.SetNoStore();
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
//设置输出文档的类型以及名称
Response.ContentType = "application/pdf";
Response.AppendHeader("Connection", "keep-alive");
Response.AppendHeader("Content-Disposition", "attachment;filename=info.pdf");
//清除缓冲区输出数据流
Response.OutputStream.Write(m.GetBuffer(), 0, m.GetBuffer().Length);
Response.OutputStream.Flush();
Response.OutputStream.Close();
}
private iTextSharp.text.Table creatListTable(int col, BaseFont fontType, int fontSize,String Node,String Message_info,String datatime)
{
iTextSharp.text.Table listTable = new iTextSharp.text.Table(col);
////定义一个18栏的表
listTable = new iTextSharp.text.Table(18, 1);
listTable.CellsFitPage = true;
//定义table的长度
listTable.AbsWidth = "540";
//定义table的外框线宽度
listTable.BorderWidth = 1;
//定义table的内部线宽度
listTable.DefaultCellBorderWidth = 1;
//定义table的位置为左右居中,上下居中
listTable.DefaultHorizontalAlignment = Element.ALIGN_CENTER;
listTable.DefaultVerticalAlignment = Element.ALIGN_MIDDLE;
//定义table的字符与Cell底线的距离
listTable.Padding = 3;
//定义table的字符与字符距离
listTable.Spacing = 0;
//定义table的字符与距上边垂直距离
listTable.Offset = 0;
Paragraph paraGraph = null;
Cell cell = null;
//标签项(非用户输入项)
//要使用粗体
iTextSharp.text.Font font12 = new iTextSharp.text.Font(fontType, fontSize, iTextSharp.text.Font.BOLD);
//定义段的内容,以及内容的字体类型
paraGraph = new Paragraph(Node, font12);
//把段中的内容添加到单元格中
cell = new Cell(paraGraph);
//设置单元中的背景色
cell.BackgroundColor = new iTextSharp.text.Color(0xC0, 0xC0, 0xC0);
//是否递增
cell.UseAscender = true;
//定义单元的位置为左右居中,上下居中
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
cell.HorizontalAlignment = Element.ALIGN_CENTER;
//定义单元的周线的宽度
cell.BorderWidth = 1;
//单元格占的1行数和3列数
cell.Rowspan = 1;
cell.Colspan = 3;
//定义单元的坐标
listTable.AddCell(cell, new Point(0, 0));
//定义段的内容,以及内容的字体类型
paraGraph = new Paragraph(Message_info, font12);
//把段中的内容添加到单元格中
cell = new Cell(paraGraph);
//设置单元中的背景色
cell.BackgroundColor = new iTextSharp.text.Color(0xC0, 0xC0, 0xC0);
//是否递增
cell.UseAscender = true;
//定义单元的位置为左右居中,上下居中
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
cell.HorizontalAlignment = Element.ALIGN_CENTER;
//定义单元的周线的宽度
cell.BorderWidth = 1;
//单元格占的1行数和12列数
cell.Rowspan = 1;
cell.Colspan = 12;
//定义单元的坐标
listTable.AddCell(cell, new Point(0, 3));
//定义段的内容,以及内容的字体类型
paraGraph = new Paragraph(datatime, font12);
//把段中的内容添加到单元格中
cell = new Cell(paraGraph);
//设置单元中的背景色
cell.BackgroundColor = new iTextSharp.text.Color(0xC0, 0xC0, 0xC0);
//是否递增
cell.UseAscender = true;
//定义单元的位置为左右居中,上下居中
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
cell.HorizontalAlignment = Element.ALIGN_CENTER;
//定义单元的周线的宽度
cell.BorderWidth = 1;
//单元格占的1行数和3列数
cell.Rowspan = 1;
cell.Colspan = 3;
//定义单元的坐标
listTable.AddCell(cell, new Point(0, 15));
//返回这个表
return listTable;
}
只是实现一个简单的的绑定表,但是发现PDF文档的显示页数部分如果显示到最底下容易被表中的内容覆盖
不知道加入页角的话会不会好或加一个新的表占住位置.以后在弄
posted on
浙公网安备 33010602011771号