C# 利用ITextSharp导出PDF文件
最近项目中需要导出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文件的更多相关文章
- 利用ITextSharp导出PDF文件
最近项目中需要到处PDF文件,最后上网搜索了一下,发现ITextSharp比较好用,所以做了一个例子: public string ExportPDF() { //ITextSharp Usage / ...
- .Net导出pdf文件,C#实现pdf导出
最近碰见个需求需要实现导出pdf文件,上网查了下代码资料总结了以下代码.可以成功的实现导出pdf文件. 在编码前需要在网上下载个itextsharp.dll,此程序集是必备的.楼主下载的是5.0版本, ...
- ITextSharp导出PDF表格和图片(C#)
文章主要介绍使用ITextSharp导出PDF表格和图片的简单操作说明,以下为ITextSharp.dll下载链接 分享链接:http://pan.baidu.com/s/1nuc6glj 密码:3g ...
- 纯前端导出pdf文件
纯前端js导出pdf,已经用于生产环境. 工具: 1.html2canvas,一种让html转换为图片的工具. 2.pdfmake或者jspdf ,一种生成.编辑pdf,并且导出pdf的工具. pdf ...
- .Net导出pdf文件,C#实现pdf导出 转载 http://www.cnblogs.com/hmYao/p/5842958.html
导出pdf文件. 在编码前需要在网上下载个itextsharp.dll,此程序集是必备的.楼主下载的是5.0版本,之前下了个5.4的似乎不好用. 下载之后直接添加引用. <%@ Page Lan ...
- asp.net2.0导出pdf文件完美解决方案【转载】
asp.net2.0导出pdf文件完美解决方案 作者:清清月儿 PDF简介:PDF(Portable Document Format)文件格式是Adobe公司开发的电子文件格式.这种文件格式与操作系统 ...
- iTextSharp - 建立PDF文件
原文 iTextSharp - 建立PDF文件 01 using iTextSharp.text; 02 using iTextSharp.text.pdf; 03 ... 04 private vo ...
- 史上最全的springboot导出pdf文件
最近项目有一个导出报表文件的需求,我脑中闪过第一念头就是导出pdf(产品经理没有硬性规定导出excel还是pdf文件),于是赶紧上网查看相关的资料,直到踩了无数的坑把功能做出来了才知道其实导出exce ...
- 利用FR导出PDF汉字乱码的处理
利用FR导出pdf,然后在unigui中显示,发现汉字乱码,改成gb2312,不乱码,但不自动折行,最后是改成DefaultCharSet搞定.FR版本:5.4.6 后记:有的浏览器中还是乱码,把字体 ...
随机推荐
- c++ 子类切勿重新定义父类 non-virtual函数
子类如果重新定义了父类的non-virtual函数则不会有多态效果. 为方便描述,这里的方法和继承方式都用public.至于原因嘛,你懂的! 1:子类直接继承父类方法 class BaseOption ...
- jQuery选择器和选取方法.RP
我们已经使用了带有简单Css选择器的jQuery选取函数:$().现在是时候深入了解jQuery选择器语法,以及一些提取和扩充选中元素集的方法了. 一.jQuery选择器 在CSS3选择器标淮草案定义 ...
- AbstractBootstrap的研读
AbstractBootstrap是一个工具类,用来配置和启动Channel的,下面看下AbstractBootstrap的类继承,ServerBootstrap用于启动ServerChannel的, ...
- 11.PowerSploit攻击指南
本人小白,写这篇文章主要记录一下自己的PowerShell学习之路,大牛请绕道:https://www.anquanke.com/subject/id/90541 首先PowerShell的攻击工具有 ...
- C#和.NET Framework简介
注:本文大部分借鉴了<果壳中的C#5.0权威指南>,小编也想根据这本书好好梳理一下C#. 序言:C#是一种通用的类型安全且面向对象的编程语言.这种语言的目标是提高程序员的生产力,为此,需要 ...
- adb 无法调试的问题,ADB server didn't ACK,* failed to start daemon *
The connection to adb is down, and a severe error has occured. You must restart adb and Eclipse. Ple ...
- msbuild 中文说明文档
Microsoft (R) 生成引擎版本 14.0.25420.1 版权所有(C) Microsoft Corporation.保留所有权利. 语法: MSBuild.exe [选项] [项目文件] ...
- 基于vue框架项目开发过程中遇到的问题总结(一)
(一)关于computed修改data里变量的值 问题:computed里是不能直接修改data里变量的值,否则在git commit 时会报错 解决:在computed里使用get和set来进行获取 ...
- uoj#450. 【集训队作业2018】复读机(单位根反演)
题面 传送门 题解 我的生成函数和单位根反演的芝士都一塌糊涂啊-- \(d=1\),答案就是\(k^n\)(因为这里\(k\)个复读机互不相同,就是说有标号) \(d=2\),我们考虑复读机的生成函数 ...
- Jmeter-响应结果unicode转成中文显示
本文为转载微信公众号文章,如作者发现后不愿意,请联系我进行删除 原文链接:http://mp.weixin.qq.com/s?__biz=MjM5OTI2MTQ3OA==&mid=265217 ...