.net下生成创建(较复杂表格,插图的)pdf文档
2013-10-08 16:21
465 查看
效果图预览如下:
本程序主要应用基于iTextSharp的PDF文件创建功能,其实我觉得这真正的的WEB应用需求不多,更多提生成EXCEL数据表格.
但也有少数用到pdf的,今天也研究了一下,实现了做一个.net课程表的pdf,其在iTextSharp中应用到的技术 创建表格,合并单元格,插图,调整边框,字体,间距这些.
核心代码如下:
//创建PDF文档对象
string tempfile = filePath;
Document documents = new Document(PageSize.A4);
//实例化生成的文档
PdfWriter.GetInstance(documents, new FileStream(Server.MapPath(tempfile), FileMode.Create));
documents.Open();//打开文档
//要支持中文,必须添加引用对应的中文字体
BaseFont bfSun = BaseFont.CreateFont(@"c:\windows\fonts\SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
Font font = new Font(bfSun, 16);
//添加表格
//程序逻辑,程序的单元格是自动按顺序添加,从第一行和第一列开始,然后是第二列,第一行满后转第二行第一列……
iTextSharp.text.Table table = new iTextSharp.text.Table(6);//创建6行三例表格
//table.BorderWidth = 0;//边框宽度
//table.BorderColor = new Color(51,178, 214);//边框颜色
table.Cellpadding = 5;//内间距
table.Cellspacing = 5;//间距
Cell cell = new Cell();//创建一个单元格
cell.Header = true;//显示表头
cell.Colspan = 6;//合并单元格
cell.AddElement(new Paragraph("星期一课程表", font));
cell.HorizontalAlignment = Element.ALIGN_CENTER;//设置居中
table.AddCell(cell);//添加表头单元格到表格
cell = new Cell();//创建一个单元格
cell.Rowspan = 2;//合并当前格和第三行的第一列单元格
//cell.BorderColor = new Color(255, 0, 0);//边框颜色
//cell.AddElement(new Paragraph("星期/科目/节次", font));
string imagePath = Server.MapPath("/2.jpg");//添加一张图片,主要是无法绘制星期/科目/节次之间的斜线
iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(new Uri(imagePath));
cell.Add(jpg);
//cell.AddElement(new ImgRaw(iTextSharp.text.Image.GetInstance(Server.MapPath("/2.jpg"))));
table.AddCell(cell);//添加单元格到表格
//添加显示上午的单元格,合并三列
cell = new Cell();
cell.Colspan = 3;//合并三列,为上午的三节课
cell.AddElement(new Paragraph("上午", font));
table.AddCell(cell);//添加到表格
//同样方法添加显示下午的单元格,合并两列
cell = new Cell();
cell.Colspan = 2;
cell.AddElement(new Paragraph("下午",font));
table.AddCell(cell);
//第三行添加节次
cell = new Cell();
table.AddCell(new Paragraph("第一节", font));
table.AddCell(new Paragraph("第二节", font));
table.AddCell(new Paragraph("第三节", font));
table.AddCell(new Paragraph("第四节", font));
table.AddCell(new Paragraph("第五节", font));
//从第四行开始写课程,但第一列必须写星期
table.AddCell(new Paragraph("星期一", font));
table.AddCell(new Paragraph(TextBox1.Text, font));
table.AddCell(new Paragraph(TextBox2.Text, font));
table.AddCell(new Paragraph(TextBox3.Text, font));
table.AddCell(new Paragraph(TextBox4.Text, font));
table.AddCell(new Paragraph(TextBox5.Text, font));
documents.Add(table); //添加内容
documents.Close(); //关闭文档对象
Response.Redirect(tempfile);
本程序主要应用基于iTextSharp的PDF文件创建功能,其实我觉得这真正的的WEB应用需求不多,更多提生成EXCEL数据表格.
但也有少数用到pdf的,今天也研究了一下,实现了做一个.net课程表的pdf,其在iTextSharp中应用到的技术 创建表格,合并单元格,插图,调整边框,字体,间距这些.
核心代码如下:
//创建PDF文档对象
string tempfile = filePath;
Document documents = new Document(PageSize.A4);
//实例化生成的文档
PdfWriter.GetInstance(documents, new FileStream(Server.MapPath(tempfile), FileMode.Create));
documents.Open();//打开文档
//要支持中文,必须添加引用对应的中文字体
BaseFont bfSun = BaseFont.CreateFont(@"c:\windows\fonts\SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
Font font = new Font(bfSun, 16);
//添加表格
//程序逻辑,程序的单元格是自动按顺序添加,从第一行和第一列开始,然后是第二列,第一行满后转第二行第一列……
iTextSharp.text.Table table = new iTextSharp.text.Table(6);//创建6行三例表格
//table.BorderWidth = 0;//边框宽度
//table.BorderColor = new Color(51,178, 214);//边框颜色
table.Cellpadding = 5;//内间距
table.Cellspacing = 5;//间距
Cell cell = new Cell();//创建一个单元格
cell.Header = true;//显示表头
cell.Colspan = 6;//合并单元格
cell.AddElement(new Paragraph("星期一课程表", font));
cell.HorizontalAlignment = Element.ALIGN_CENTER;//设置居中
table.AddCell(cell);//添加表头单元格到表格
cell = new Cell();//创建一个单元格
cell.Rowspan = 2;//合并当前格和第三行的第一列单元格
//cell.BorderColor = new Color(255, 0, 0);//边框颜色
//cell.AddElement(new Paragraph("星期/科目/节次", font));
string imagePath = Server.MapPath("/2.jpg");//添加一张图片,主要是无法绘制星期/科目/节次之间的斜线
iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(new Uri(imagePath));
cell.Add(jpg);
//cell.AddElement(new ImgRaw(iTextSharp.text.Image.GetInstance(Server.MapPath("/2.jpg"))));
table.AddCell(cell);//添加单元格到表格
//添加显示上午的单元格,合并三列
cell = new Cell();
cell.Colspan = 3;//合并三列,为上午的三节课
cell.AddElement(new Paragraph("上午", font));
table.AddCell(cell);//添加到表格
//同样方法添加显示下午的单元格,合并两列
cell = new Cell();
cell.Colspan = 2;
cell.AddElement(new Paragraph("下午",font));
table.AddCell(cell);
//第三行添加节次
cell = new Cell();
table.AddCell(new Paragraph("第一节", font));
table.AddCell(new Paragraph("第二节", font));
table.AddCell(new Paragraph("第三节", font));
table.AddCell(new Paragraph("第四节", font));
table.AddCell(new Paragraph("第五节", font));
//从第四行开始写课程,但第一列必须写星期
table.AddCell(new Paragraph("星期一", font));
table.AddCell(new Paragraph(TextBox1.Text, font));
table.AddCell(new Paragraph(TextBox2.Text, font));
table.AddCell(new Paragraph(TextBox3.Text, font));
table.AddCell(new Paragraph(TextBox4.Text, font));
table.AddCell(new Paragraph(TextBox5.Text, font));
documents.Add(table); //添加内容
documents.Close(); //关闭文档对象
Response.Redirect(tempfile);
相关文章推荐
- asp.net利用itextsharp生成pdf文档
- asp.net下用Aspose.Words for .NET动态生成word文档中的数据表格的方法(转)
- 生成PDF文件-使用iText制作复杂表格
- C# 在PDF文档中创建表格的实现方法
- 专业性的PDF文档创建组件 Spire.PDF for .NET
- .Net文档图像处理工具包GdPicture.NET发布v14.0.30,改进PDF/OCR生成速度
- 用Aspose.Words for .NET动态生成word文档中的数据表格
- Java iText 动态 生成 PDF 文档表格中文问题
- 用C#.NET实现生成PDF文档和将WORD转换为PDF
- VS+C#由创建的Excel生成PDF文档
- Java生成PDF文档(表格、列表、添加图片等)
- Asp.Net复杂表格:应用 Repeater 创建复杂表头的表格
- ASP.NET API Helper Page 创建并生成相关帮助文档
- Asp.net或C#使用word模板生成替换后的Word和pdf文档-总结
- 推荐.NET使用Aspose.Words动态生成word文档数据表格(附Aspose.Words for NET 6.5 破解版下载)
- 用Aspose.Words for .NET动态生成word文档中的数据表格
- 【转载自某园友】用Aspose.Words for .NET动态生成word文档中的数据表格
- C# 如何在PDF文档中创建表格
- C# 如何在PDF文档中创建表格