最近项目中需要导出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

利用ITextSharp导出PDF文件的更多相关文章

  1. C# 利用ITextSharp导出PDF文件

    最近项目中需要导出PDF文件,最后上网搜索了一下,发现ITextSharp比较好用,所以做了一个例子: public string ExportPDF() { //ITextSharp Usage / ...

  2. .Net导出pdf文件,C#实现pdf导出

    最近碰见个需求需要实现导出pdf文件,上网查了下代码资料总结了以下代码.可以成功的实现导出pdf文件. 在编码前需要在网上下载个itextsharp.dll,此程序集是必备的.楼主下载的是5.0版本, ...

  3. ITextSharp导出PDF表格和图片(C#)

    文章主要介绍使用ITextSharp导出PDF表格和图片的简单操作说明,以下为ITextSharp.dll下载链接 分享链接:http://pan.baidu.com/s/1nuc6glj 密码:3g ...

  4. 纯前端导出pdf文件

    纯前端js导出pdf,已经用于生产环境. 工具: 1.html2canvas,一种让html转换为图片的工具. 2.pdfmake或者jspdf ,一种生成.编辑pdf,并且导出pdf的工具. pdf ...

  5. .Net导出pdf文件,C#实现pdf导出 转载 http://www.cnblogs.com/hmYao/p/5842958.html

    导出pdf文件. 在编码前需要在网上下载个itextsharp.dll,此程序集是必备的.楼主下载的是5.0版本,之前下了个5.4的似乎不好用. 下载之后直接添加引用. <%@ Page Lan ...

  6. asp.net2.0导出pdf文件完美解决方案【转载】

    asp.net2.0导出pdf文件完美解决方案 作者:清清月儿 PDF简介:PDF(Portable Document Format)文件格式是Adobe公司开发的电子文件格式.这种文件格式与操作系统 ...

  7. iTextSharp - 建立PDF文件

    原文 iTextSharp - 建立PDF文件 01 using iTextSharp.text; 02 using iTextSharp.text.pdf; 03 ... 04 private vo ...

  8. 史上最全的springboot导出pdf文件

    最近项目有一个导出报表文件的需求,我脑中闪过第一念头就是导出pdf(产品经理没有硬性规定导出excel还是pdf文件),于是赶紧上网查看相关的资料,直到踩了无数的坑把功能做出来了才知道其实导出exce ...

  9. 利用FR导出PDF汉字乱码的处理

    利用FR导出pdf,然后在unigui中显示,发现汉字乱码,改成gb2312,不乱码,但不自动折行,最后是改成DefaultCharSet搞定.FR版本:5.4.6 后记:有的浏览器中还是乱码,把字体 ...

随机推荐

  1. getopts

    http://blog.sina.com.cn/s/blog_81c2cf020100v0wh.html http://www.cnblogs.com/xiangzi888/archive/2012/ ...

  2. PostgreSql安装

    官网:http://www.postgresql.org/download/linux/redhat/ 一.安装 由于我的机子是centos6.2,所以选择RedHat的. 按照官网所说的进行安装: ...

  3. C#实现中国天气网XML接口测试

    点击链接查看中国天气网接口说明,最近想研究一下接口测试,源于最近一次和某公司的技术总监(交大校友)谈话,发现接口测试的需求是比较大的,于是想要研究一下. 好不容易在网上找到了一个关于中国天气网的接口说 ...

  4. AfterEffects 关键帧辅助功能

    http://www.cocoachina.com/design/20160121/15073.html 标准缓动曲线查询网址:http://easings.net/zh-cn

  5. Linux下新的网络管理工具ip替代ifconfig零压力

    如果你使用 Linux 足够久,那么你自然知道一些工具的来与去.2009年 Debian 开发者邮件列表宣布放弃使用缺乏维护的 net-tools 工具包正是如此.到今天 net-tools 仍然被部 ...

  6. Centos 中如何快速定制二进制的内核RPM 包

    随着Linux服务器越来越多了,底层系统内核想要保持版本统一就需要定制专门的二进制安装包来便捷的升级和管理. RedHat系那当然就是使用rpmbuild来做定制化管理了. 今天我们分俩个部分(roo ...

  7. VS2013自动注释插件

    在程序编写的时候,你是否见过这种写法?整个项目每个cs文件头部都包含一个,版权,版本等信息的注释头? 类似这个类文件: /*************************************** ...

  8. CHECKBOX_CHECKED built-in in Oracle D2k Forms

    CHECKBOX_CHECKED built-in in Oracle D2k Forms DescriptionA call to the CHECKBOX_CHECKED function ret ...

  9. tophat cufflinks cuffcompare cuffmerge 的使用

    Cole Trapnell said: there are three strategies: 1) merge bams and assemble in a single run of Cuffli ...

  10. 【原创分享】python获取乌云最新提交的漏洞,邮件发送

    #!/usr/bin/env python # coding:utf-8 # @Date : 2016年4月21日 15:08:44 # @Author : sevck (sevck@jdsec.co ...