public void ExportPDF(DataTable dt)
{
if (dt.Rows.Count == 0)
{
return;
}
string strFileName = "../ExportPDF/" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf";
///设置页面尺寸
Document document = new Document(iTextSharp.text.PageSize.A4);
//创建writer写入
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(Server.MapPath(strFileName), FileMode.Create));
BaseFont bfSun = BaseFont.CreateFont(@"C:\\WINDOWS\\Fonts\\SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//---------------设置字体大小
iTextSharp.text.Font font01 = new iTextSharp.text.Font(bfSun, 05);
iTextSharp.text.Font font07 = new iTextSharp.text.Font(bfSun, 07);
iTextSharp.text.Font font08 = new iTextSharp.text.Font(bfSun, 08);
iTextSharp.text.Font font10 = new iTextSharp.text.Font(bfSun, 10);
iTextSharp.text.Font font14 = new iTextSharp.text.Font(bfSun, 14);
document.Open();
for (int i = 0; i < dt.Rows.Count; i++)
{
//-----------------------------table 是外层表格,tbleft,tbcent,tbright 小表格,将小表哥嵌套到大表格内就是成左右分割
#region 第一组
iTextSharp.text.Table table = new iTextSharp.text.Table(3);
table.AutoFillEmptyCells = true;
table.Width = 105;
//-----------------------------去除边框.
table.DefaultCellBorderWidth = 0;
table.DefaultCellBorder = 0;
//-----------------------------设置每列宽度,我设置外层表格有3列,总宽度105,所以下面数组只有3个,
table.SetWidths(new int[] { 52, 2, 52 });
Cell cell = new Cell("");
//-------------------------左边表格
iTextSharp.text.Table tbleft = new iTextSharp.text.Table(15);
tbleft.AutoFillEmptyCells = true;
tbleft.Border = 1;
tbleft.DefaultCellBorderWidth = 0;
tbleft.DefaultCellBorder = 0;
//-------------------------右边表格
iTextSharp.text.Table tbright = new iTextSharp.text.Table(5);
tbright.AutoFillEmptyCells = true;
tbright.DefaultCellBorderWidth = 0;
tbright.DefaultCellBorder = 0;
//-------------------------中间表格
iTextSharp.text.Table tbcent = new iTextSharp.text.Table(1);
tbcent.AutoFillEmptyCells = true;
tbright.DefaultCellBorderWidth = 0;
tbright.DefaultCellBorder = 0;
tbcent.Border = 1;
//--------------------------这是我设置中间表格的边框宽度,设置后好像没效果
tbcent.BorderWidthLeft = 3f;
tbcent.BorderWidthRight = 3f;
//--------------------------------标题
#region 个人信息
cell = new Cell(new Phrase(" 2014年四川省建设工程质量检测人", font14));
cell.Colspan = 14;
tbleft.AddCell(cell, new Point(0, 0));
cell = new Cell(new Phrase(" 员新培训考试准考证", font14));
cell.Colspan = 14;
tbleft.AddCell(cell, new Point(1, 0));
cell = new Cell(new Phrase("姓 名:", font10));
cell.Colspan = 3;
tbleft.AddCell(cell, new Point(2, 0));
cell = new Cell(new Phrase(dt.Rows[i]["personName"].ToString(), font10));
cell.Colspan = 2;
tbleft.AddCell(cell, new Point(2, 3));
#region 插入头像
//-----------------------------初始花头像
string picurl = Server.MapPath("images/nophoto.gif");//图片地址
if (dt.Rows[i]["photoURL"] != DBNull.Value)
{
//-----------------------我这是使用超链接获取的图像,也可以不使用超链接,
iTextSharp.text.Image Imgjpeg = iTextSharp.text.Image.GetInstance(new Uri(dt.Rows[i]["photoURL"].ToString()));
}
iTextSharp.text.Image jpeg = null;
try
{
jpeg = iTextSharp.text.Image.GetInstance(picurl);
}
catch (Exception)
{
picurl = Server.MapPath("images/nophoto.gif");//图片地址
jpeg = iTextSharp.text.Image.GetInstance(picurl);
}
//------------------------设置图片大小
jpeg.ScaleAbsolute(60, 80);
cell = new Cell();
cell.Add(jpeg);
//-------------------------设置单元格合并
cell.Colspan = 4;
cell.Rowspan = 4;
tbleft.AddCell(cell, new Point(2, 11));
#endregion
cell = new Cell(new Phrase("准考证号:", font10));
cell.Colspan = 3;
cell.VerticalAlignment = Element.ALIGN_TOP;
cell.VerticalAlignment = Element.ALIGN_TOP;
tbleft.AddCell(cell, new Point(3, 0));
cell = new Cell(new Phrase(dt.Rows[i]["examNO"].ToString(), font10));
cell.Colspan = 7;
tbleft.AddCell(cell, new Point(3, 3));
cell = new Cell(new Phrase("身份证号:", font10));
cell.Colspan = 3;
tbleft.AddCell(cell, new Point(4, 0));
cell = new Cell(new Phrase(dt.Rows[i]["identityNumber"].ToString(), font10));
cell.Colspan = 7;
tbleft.AddCell(cell, new Point(4, 3));
cell = new Cell(new Phrase("工作单位:", font10));
cell.Colspan = 3;
tbleft.AddCell(cell, new Point(5, 0));
cell = new Cell(new Phrase(dt.Rows[i]["enterName"].ToString(), font10));
cell.Colspan = 12;
tbleft.AddCell(cell, new Point(5, 3));
cell = new Cell(new Phrase("考试地点:", font10));
cell.Colspan = 3;
tbleft.AddCell(cell, new Point(6, 0));
cell = new Cell(new Phrase(dt.Rows[i]["testAddress"].ToString(), font10));
cell.Colspan = 12;
tbleft.AddCell(cell, new Point(6, 3));
cell = new Cell(new Phrase("考试时间:", font10));
cell.Colspan = 3;
tbleft.AddCell(cell, new Point(7, 0));
cell = new Cell(new Phrase(dt.Rows[i]["testDates"].ToString(), font10));
cell.Colspan = 12;
tbleft.AddCell(cell, new Point(7, 3));
cell = new Cell(new Phrase("考试科目:", font10));
cell.Colspan = 3;
tbleft.AddCell(cell, new Point(8, 0));
cell = new Cell(new Phrase(dt.Rows[i]["regName"].ToString(), font10));
cell.Colspan = 12;
tbleft.AddCell(cell, new Point(8, 3));
cell = new Cell("");
tbleft.AddCell(cell, new Point(9, 0));
cell = new Cell(new Phrase(" 四川省建设工程质量安全监督总站 制", font10));
cell.Colspan = 14;
tbleft.AddCell(cell, new Point(10, 0));
cell = new Cell(new Phrase(" 网址:www.cqss.gov.cn", font10));
cell.Colspan = 14;
tbleft.AddCell(cell, new Point(11, 0));
//---------------------------将tbleft 小表格插入到外层表格第0行0列,
table.InsertTable(tbleft, 0, 0);
#endregion
#region 中间表格
//------------------------------利用中间表格使中间隔离出线
cell = new Cell(new Phrase("", font10));
cell.Rowspan = 15;
tbcent.AddCell(cell, new Point(0, 0));
//---------------------------将tbcent 小表格插入到外层表格第0行1列,
table.InsertTable(tbcent, 0, 1);
#endregion
#region 考试规则
cell = new Cell(new Phrase(" 考 试 规 则", font14));
cell.Colspan = 5;
tbright.AddCell(cell, new Point(0, 0));
cell = new Cell(new Phrase(" 1、考试前20分钟,应试人员凭本人准考证和身份证(不含临时身份证、过期身份证、户口、公安局及单位证明等)进入考室,对号入座,并将准考证和身份证放于桌面右上角。两证不齐者,不能参加考试。", font07));
cell.Colspan = 5;
tbright.AddCell(cell, new Point(1, 0));
cell = new Cell(new Phrase(" 2、考试开始30分钟后,应试人员不得进入考场参加考试;考试开始60分钟内,不得交卷出场。", font07));
cell.Colspan = 5;
tbright.AddCell(cell, new Point(2, 0));
cell = new Cell(new Phrase(" 3、应试人员参加考试只准携带蓝(黑)墨水钢笔、2B铅笔、橡皮擦和简易计算器进入考室,严禁将移动电话等电子设备带入考室及座位,考试开始后不得相互借用文具及其它物品。", font07));
cell.Colspan = 5;
tbright.AddCell(cell, new Point(3, 0));
cell = new Cell(new Phrase(" 4、试卷发放后,应试人员必须在开考后30分钟内在试卷和答题卡(纸)规定的位置上准确填写(涂)本人姓名、准考证号等有关信息,但不得超过装订线,不得做任何标记。", font07));
cell.Colspan = 5;
tbright.AddCell(cell, new Point(4, 0));
cell = new Cell(new Phrase(" 5、考试开始铃响后开始答题,在答题卡上答题必须使用2B铅笔填涂;在答题试卷上答题必须使用蓝(黑)墨水钢笔。", font07));
cell.Colspan = 5;
tbright.AddCell(cell, new Point(5, 0));
cell = new Cell(new Phrase(" 6、考生不得要求监考人员解释试题,若发现试卷分发错误、试卷字迹模糊或答题卡有折皱和污损等,可举手询问。", font07));
cell.Colspan = 5;
tbright.AddCell(cell, new Point(6, 0));
cell = new Cell(new Phrase(" 7、考试期间要保持考场安静,不许交头接耳、左顾右盼,严禁偷看他人试题答案或交换试卷、草稿纸等。考室内禁止吸烟。", font07));
cell.Colspan = 5;
tbright.AddCell(cell, new Point(7, 0));
cell = new Cell(new Phrase(" 8、考试期间应试人员不得以任何理由离开考室。开考60分钟后提前交卷的人员,必须离场,不得在考场附近停留、喧哗。", font07));
cell.Colspan = 5;
tbright.AddCell(cell, new Point(8, 0));
cell = new Cell(new Phrase(" 9、考试结束信号发出后,应当立即停止答卷,并将试题、答题卡反扣在桌面上,经监考人员检查无误后方可离开考室,应试人员不得将试卷、答题卡、草稿纸带出考场,违者考试成绩无效。", font07));
cell.Colspan = 5;
tbright.AddCell(cell, new Point(9, 0));
cell = new Cell(new Phrase(" 10、应试人员因个人原因将姓名、准考证号和科目代号填(涂)错误导致考试无效的,后果自负。", font07));
cell.Colspan = 5;
tbright.AddCell(cell, new Point(10, 0));
cell = new Cell(new Phrase(" 11、准考证领取后,请妥善保管,以备考试和领取合格证书、成绩之用,并认真检查、核对,如有错误,请立即与四川省建设工程质量安全监督总站检测科联系(联系电话:028-85088615)", font07));
cell.Colspan = 5;
tbright.AddCell(cell, new Point(11, 0));
cell = new Cell(new Phrase("\n\n", font10));
tbright.AddCell(cell, new Point(12, 0));
//---------------------------将tbright 小表格插入到外层表格第0行2列,
table.InsertTable(tbright, 0, 2);
#endregion
//------------------------------将制作好的table表格加入到document
document.Add(table);
#endregion
//-------------------------------表格下留出一段距离的空白,相当于换行
Paragraph p3 = new Paragraph(" ", FontFactory.GetFont(FontFactory.HELVETICA, 12));
document.Add(p3);
PdfContentByte cb = writer.DirectContent;
BaseFont bf2 = BaseFont.CreateFont(@"C:\\WINDOWS\\Fonts\\SIMHEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
cb.BeginText();
cb.SetFontAndSize(bf2, 12);
cb.EndText();
//----------------------------这个是我个人逻辑处理判断
if (i % 2 == 1)
{
//-------------------------必须要,这是分页, 进行分页,上面的判断可以不需要,
document.NewPage();
}
}
document.Close();
//关闭目标文件
//关闭写入流
writer.Close();
String FullFileName = System.Web.HttpContext.Current.Server.MapPath(strFileName);//这里是你文件在项目中的位置,根目录下就这么写
string fileName = DateTime.Now.ToString() + ".pdf";
FileInfo DownloadFile = new FileInfo(FullFileName);
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.ClearHeaders();
System.Web.HttpContext.Current.Response.Buffer = false;
System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";
System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
System.Web.HttpContext.Current.Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
System.Web.HttpContext.Current.Response.WriteFile(DownloadFile.FullName);
System.Web.HttpContext.Current.Response.Flush();
System.Web.HttpContext.Current.Response.End();
}