最近项目中需要导出PDF文件,最后上网搜索了一下,发现ITextSharp比较好用,所以做了一个例子:

public string ExportPDF()
{
//ITextSharp Usage
//Steps:1. Add content to cell;2. Add cell to table;3. Add table to document;4. Add document to rectangle;
string sAbsolutePath = ControllerContext.HttpContext.Server.MapPath(this.path);
string FileName = string.Format("Notice_{0}.pdf", DateTime.Now.ToString("yyyyMMddHHmmss"));
BaseFont bf = BaseFont.CreateFont("C:/WINDOWS/Fonts/Arial.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
Rectangle rec = new Rectangle(, );
Document document = new Document(rec);
document.SetMargins(10f, 10f, 10f, 10f);
PdfWriter pdfwriter = PdfWriter.GetInstance(document,
new System.IO.FileStream(sAbsolutePath + "\\" + FileName, System.IO.FileMode.Create)
); document.Open();
Font font = new Font(bf); try
{
PdfPTable pdftable = new PdfPTable();
pdftable.HorizontalAlignment = ; //Set cell width
float[] cellwidth = { 3f, 5f, 5f, 3.5f, 5f };
pdftable.SetWidths(cellwidth); //Header
Font PDFFontA = FontFactory.GetFont("Arial", , Font.BOLD);
Font PDFFontB = FontFactory.GetFont("Arial", , Font.BOLD);
Font PDFFontC = FontFactory.GetFont("Arial", ); //Image Object
Image jpeg = Image.GetInstance(HttpContext.Server.MapPath("../images/buddy.jpg"));
PdfPCell cell = new PdfPCell(jpeg);
cell.FixedHeight = 40f;
cell.Colspan = ;
cell.Rowspan = ;
cell.Border = ;
pdftable.AddCell(cell); cell = new PdfPCell(new Phrase(("Did You Know?"), PDFFontB));
cell.Colspan = ;
cell.Rowspan = ;
cell.Border = ;
cell.BackgroundColor = new BaseColor(, , );//RGB Color Value
cell.HorizontalAlignment = ;
pdftable.AddCell(cell); cell = new PdfPCell(new Phrase("\nTitle: Hello World!\nDate: " + DateTime.Now.ToString("MM/dd/yyyy") + "\n\n", PDFFontC));
cell.Colspan = ;
cell.Rowspan = ;
cell.Border = ;
cell.BackgroundColor = new BaseColor(, , );//RGB Color Value
cell.HorizontalAlignment = ;
pdftable.AddCell(cell); cell = new PdfPCell(new Phrase("Notice", PDFFontA));
cell.BackgroundColor = new BaseColor(, , );//RGB Color Value
cell.Colspan = ;
cell.Padding = 5f;
cell.HorizontalAlignment = ;
pdftable.AddCell(cell); string[] AssHeader = { "DateTime", "Name", "Description", "Icon", "Notes"};
for (int i = ; i < AssHeader.Length; i++)
{
cell = new PdfPCell(new Phrase(AssHeader[i], PDFFontB));
cell.HorizontalAlignment = ;
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
cell.BackgroundColor = new BaseColor(, , );
cell.PaddingBottom = 4f;
cell.PaddingTop = 4f;
pdftable.AddCell(cell);
}
for (int i = ; i < ; i++)
{
cell = new PdfPCell(new Phrase(new DateTime().ToShortDateString(), PDFFontC));
cell.HorizontalAlignment = ;
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
cell.PaddingBottom = 4f;
cell.PaddingTop = 4f;
pdftable.AddCell(cell); cell = new PdfPCell(new Phrase("Jack Chean", PDFFontC));
cell.HorizontalAlignment = ;
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
cell.PaddingBottom = 4f;
cell.PaddingTop = 4f;
pdftable.AddCell(cell); cell = new PdfPCell(new Phrase("Just for my testing!!", PDFFontC));
cell.HorizontalAlignment = ;
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
cell.PaddingBottom = 4f;
cell.PaddingTop = 4f;
pdftable.AddCell(cell); cell = new PdfPCell(new Phrase("ICON", PDFFontC));
cell.HorizontalAlignment = ;
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
cell.PaddingBottom = 4f;
cell.PaddingTop = 4f;
pdftable.AddCell(cell); cell = new PdfPCell(new Phrase("For Tom!", PDFFontC));
cell.HorizontalAlignment = ;
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
cell.PaddingBottom = 4f;
cell.PaddingTop = 4f;
pdftable.AddCell(cell);
}
//Very important
//You can display the header in each page through this properity:HeaderRows
pdftable.HeaderRows = ; document.Add(pdftable);
//Pager
float tableheight = pdftable.CalculateHeights();
if (tableheight < && tableheight > )
{
document.NewPage();
} document.NewPage();
document.Close();
}
catch (Exception ex)
{
document.Add(new Paragraph(ex.Message.ToString(), font));
}
return FileName;
}

最后的显示效果:

参考文章:http://blog.csdn.net/adgjlxxx/article/details/43307227

最新文章

  1. Android开发学习——动画
  2. Vim常用命令
  3. jdbc连接数据库的步骤 (转)
  4. Python自动化之django URL
  5. openCV_java Canny边缘检测
  6. ps 倒影制作
  7. abstract class和interface的区别
  8. 禁止触屏滑动touchmove方法介绍
  9. SSH思路
  10. Python sql数据的增删改查简单操作
  11. atitit.标准时间格式 相互转换 秒数 最佳实践
  12. web.py 学习(二)Worker
  13. std::copy性能分析与memmove机器级实现
  14. JAVA编码互转(application/x-www-form-urlencoded)
  15. Linux shell编程:状态变量
  16. Django:同一个app支持多个数据库
  17. select中想要加a链接 并且新窗口打开
  18. supervisor 文档
  19. Day05 (黑客成长日记) 文件操作系列
  20. mvn依赖冲突

热门文章

  1. p3163 [CQOI2014]危桥
  2. 自己实现 String 类
  3. 第四章输入/输出(I/O)4.2PCL中I/O模块及类介绍
  4. ubuntu16.04.2安装tftp服务器
  5. c# 使用protobuf格式操作 Redis
  6. Java基础-集合框架-ArrayList源码分析
  7. [学习笔记]man手册的使用
  8. [raspberry pi3] 安装ffmpeg
  9. Hexo下Next主题配置与优化
  10. 等和的分隔子集(DP)