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

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

  1. 利用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. 数字图像处理实验(7):PROJECT 04-03 , Lowpass Filtering 标签: 图像处理MATLAB 2017-05-25 09:30 109人

    实验要求: Objective: To observe how the lowpass filtering smoothes an image. Main requirements: Ability ...

  2. 51NOD 1616 最小集合

    传送门 分析 不难发现集合中的数一定是集合内其它一堆数的$gcd$ 于是我们枚举$i$,统计原来集合中有几个数是$i$的倍数,设这个值为$f(i)$ 之后对于每个$i$如果不存在$f(x*i) = f ...

  3. CF 1025C Plasticine zebra

    昨晚忘记判只有一个字符的情况fst了呜呜呜 挺有趣的题,昨晚连刚带猜弄出结论 考虑答案的取值,最优答案可能是一个后缀,或者是一个前缀,或者是一个后缀加上前缀 那么翻转之后最优答案的可选值就有了1的前缀 ...

  4. wordcount小程序

    wordcount小程序 (1)github网址 https://github.com/yuyuyu960818/count_txt_file (2)PSP表 PSP2.1 PSP阶段 预估耗时 (分 ...

  5. JDBC 配置环境

    一.配置JDBC环境:先下载驱动jar包 1.配置classpath环境变量  如(E:\jdbc\mysql-connector-java-5.1.7-bin.jar) 2.数据库URL: 2.1 ...

  6. win8使用every'thing无法显示搜索结果的解决方法

    关键词: win8,everything,无搜索结果 进入everything ,tools->option右下角有个 restore defaults 如果安全软件阻拦,点击  允许 就行了, ...

  7. c#处理未捕获的异常(UnhandledException)

    处理未捕获的异常,放在program类的Main函数下 1.UnhandledException 作用:接收未捕获到的异常 例: static void Main(string[] args) { A ...

  8. .NET 实体转换辅助类

    /// <summary> /// 实体转换辅助类 /// </summary> public class ModelConvertHelper<T> where ...

  9. Linux之sersync数据实时同步

    sersync其实是利用inotify和rsync两种软件技术来实现数据实时同步功能的,inotify是用于监听sersync所在服务器上的文件变化,结合rsync软件来进行数据同步,将数据实时同步给 ...

  10. [翻译]CURAND Libaray--Host API--(2)

    Translated by xingoo 如果有错误请联系:xinghl90@gmail.com 2.3 返回值 所有的CURAND host端的函数返回值都是curandStatus_t.如果调用没 ...