使用ItextSharp产PDF完整操作
记得上回有写到用C#操作Excel(.net 4.0)

- /// <summary>
- /// 产生PDf
- /// </summary>
- /// <param name="strFileName">文件名称</param>
- /// <param name="dt">数据源</param>
- public void PDF(string strFileName, DataTable dt, string PrintName)
- {
- if (!Directory.Exists(AppConfigString.FilePath))
- {
- Directory.CreateDirectory(AppConfigString.FilePath);
- }
- string strPathTemp = AppConfigString.FilePath + DateTime.Now.ToString("yyyyMMddhhmmss") + ".pdf";
- //零時的pdf地址,用于水印读取文件
- string strPath = AppConfigString.FilePath + strFileName;//真正文件的位置
- Document document = new Document(PageSize.A4, 25, 25, 25, 25);
- FileStream stream = new FileStream(strPathTemp, FileMode.Create);
- PdfWriter writer = PdfWriter.GetInstance(document, stream);
- document.Footer = Foot();//页尾的函数,在open前定义
- document.Open();
- HeaderFooter head = new HeaderFooter(HeadTable(3, PrintName), true);
- //true为在页眉的右边生成一个页数,会影响到页眉的高度,在open后定义
- //headTable为页眉的函数
- head.Border = Rectangle.NO_BORDER;
- document.Header = head;
- //页眉第一页不会出现,第2页才会出现,所以此地直接在第一列加了一个表头
- //页尾在第一页就会出现
- document.Add(HeadTable(3, PrintName));//第一頁表頭不自動添加
- //主要数据的添加
- int IRowCount = dt.Rows.Count;
- int IColumnCount = dt.Columns.Count;
- PdfPTable pdfTb = new PdfPTable(IColumnCount);
- pdfTb.HeaderRows = 1;//自動加表頭
- for (int i = 0; i < IRowCount; i++)
- {
- for (int j = 0; j < IColumnCount; j++)
- {
- string value = dt.Rows[i][j].ToString();
- Phrase phs = new Phrase(value, PdfFont.Font(3));
- //此时把数据转为Phrase只是为了使用字体,不然中文不能显示
- pdfTb.AddCell(phs);
- }
- }
- document.Add(pdfTb);
- document.Close();
- writer.Close();
- PDFWatermark(strPathTemp, strPath, 10, 10, PrintName);//为生成好的文件添加水印
- }

上面为一段完整的使用过程,注释是我刚加的,比较详细了,说下面几个问题
1:页眉和页首
》页眉与页首都使用相同的类HeaderFooter,只是页眉是给 document.Header,页尾是给document.Footer
》页尾是在document.open前,页眉是在document.open后
》页眉第一页不会出现,第2页才会出现,所以此地直接在第一列加了一个表头。页尾在第一页就会出现。
2:页眉的制作方法
由于页眉不好制作,所以我选择直接先拼成PdfPTable,再把PdfPTable放到Phrase,再把Phrase放到HeaderFooter给页眉,这样就好操作些
这个是在百度知道上找到的思路,我就这样做了,呵呵...
下面为我制作页眉的方法,一些信息安全问题已换为XXXXX

- /// <summary>
- /// 產生表頭的數據
- /// </summary>
- /// <param name="IType">1:ID 2:Name 3:detali</param>
- /// <returns></returns>
- public Phrase HeadTable(int IType, string PrintName)
- {
- string strhead = "";
- switch (IType)
- {
- case 1: strhead = "XXXXXXXXXXXXXXXXXXXX"; break;
- case 2: strhead = "XXXXXXXXXXXXXXXXXXXX"; break;
- case 3: strhead = "XXXXXXXXXXXXXXXXXXXX"; break;
- }
- Phrase phshead = new Phrase(strhead, PdfFont.Font(1));
- Phrase phsjimi = new Phrase("XXXXXXXXXXXXXXXXXXXX ", PdfFont.Font(3));
- Phrase phsPeople = new Phrase("XXXXXXXXXXXXXXXXXXXX" + PrintName + "XXXXXXXXXXXXXXXXXXXX" + PrintName, PdfFont.Font(3));
- Phrase phsPage = new Phrase(" ", PdfFont.Font(3));
- Phrase phsPrintDate = new Phrase("列印時間:" + DateTime.Now.ToString("yyyy/MM/dd hh:mm") + "\r\n Print Date:" + DateTime.Now.ToString("yyyy/MM/dd hh:mm"), PdfFont.Font(3));
- PdfPCell pchead = new PdfPCell(phshead);
- pchead.VerticalAlignment = 1;
- pchead.HorizontalAlignment = 1;
- PdfPCell pcjimi = new PdfPCell(phsjimi);
- PdfPCell pcPeople = new PdfPCell(phsPeople);
- PdfPCell pcPage = new PdfPCell(phsPage);
- PdfPCell pcPrintDate = new PdfPCell(phsPrintDate);
- PdfPCell pcnull = new PdfPCell();
- pchead.Border = 0;
- pcjimi.Border = 0;
- pcPeople.Border = 0;
- pcPage.Border = 0;
- pcPrintDate.Border = 0;
- pcnull.Border = 0;
- pcPeople.PaddingBottom = 10;
- pcPrintDate.PaddingBottom = 10;
- Phrase phead = new Phrase();
- PdfPTable thead = new PdfPTable(2);
- thead.AddCell(pchead);
- thead.AddCell(pcjimi);
- thead.AddCell(pcnull);
- thead.AddCell(pcPage);
- thead.AddCell(pcPeople);
- thead.AddCell(pcPrintDate);
- thead.SetWidths(new float[] { PageSize.A4.Width, 200 });
- phead.Add(thead);
- return phead;
- }

页尾的方法

- /// <summary>
- /// 页尾
- /// </summary>
- /// <returns></returns>
- public HeaderFooter Foot()
- {
- string strFoot = @"XXXXXXXXXXXXXXXXXX";
- Phrase pfoot = new Phrase(strFoot, PdfFont.Font(2));
- HeaderFooter foot = new HeaderFooter(pfoot, false);
- foot.Border = Rectangle.NO_BORDER;
- return foot;
- }

3:字体的问题
如果没有定义字体,汉字是不会出现的,下面是我定义的字体,使用的ITextSharp控件字体,当然也可以使用字体文件
详情可查看http://winsystem.ctocio.com.cn/334/12194834.shtml

- /// <summary>
- /// 文字类型定义
- /// </summary>
- /// <param name="IType">返回文字类别</param>
- /// <returns></returns>
- public static Font Font(int IType)
- {
- BaseFont.AddToResourceSearch("iTextAsian.dll");
- BaseFont.AddToResourceSearch("iTextAsianCmaps.dll");
- BaseFont bf = BaseFont.CreateFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
- //简体?繁体不能使用
- //
- Font font = new Font(bf);//普通文章
- font.Size = 10;
- Font fontFoot = new Font(bf);//頁脚文字
- fontFoot.Size = 7;
- fontFoot.Color = Color.RED;
- Font fontNormal = new Font(bf);//正常文字
- fontNormal.Size = 7;
- switch (IType)
- {
- case 1: return font;
- case 2: return fontFoot;
- case 3: return fontNormal;
- default: return fontNormal;
- }
- }

4:主要内容的制作
根据数据源,利用PdfPTable和Phase等制作,块等我还没用会,主要是样式和定位没搞会
下面的博客有在Asp.Net中操作PDF – iTextSharp -利用块,短语,段落添加文本的方法
http://www.cnblogs.com/CareySon/archive/2011/11/03/2234625.html
5:水印的添加

- /// <summary>
- /// 為生成的pdf添加水印
- /// </summary>
- /// <param name="inputfilepath"></param>
- /// <param name="outputfilepath"></param>
- /// <param name="top"></param>
- /// <param name="left"></param>
- /// <returns></returns>
- public bool PDFWatermark(string inputfilepath, string outputfilepath, float top, float left, string strName)
- {
- PdfReader pdfReader = null;
- PdfStamper pdfStamper = null;
- try
- {
- pdfReader = new PdfReader(inputfilepath);
- int numberOfPages = pdfReader.NumberOfPages;
- iTextSharp.text.Rectangle psize = pdfReader.GetPageSize(1);
- float width = psize.Width;
- float height = psize.Height;
- pdfStamper = new PdfStamper(pdfReader, new FileStream(outputfilepath, FileMode.Create));
- PdfContentByte waterMarkContent;
- WatermarkCreater wmc = new WatermarkCreater();
- Draw.Image image = wmc.GetImageByte(strName, AppConfigString.WaterMarkPath);
- var img = Image.GetInstance(image, System.Drawing.Imaging.ImageFormat.Png);
- if (left < 0)
- {
- left = width - image.Width + left;
- }
- img.SetAbsolutePosition(left, (height - image.Height) - top);
- //每一页加水印,也可以设置某一页加水印
- for (int i = 1; i <= numberOfPages; i++)
- {
- waterMarkContent = pdfStamper.GetUnderContent(i);
- waterMarkContent.AddImage(img);
- }
- return true;
- }
- catch (Exception ex)
- {
- ex.Message.Trim();
- return false;
- }
- finally
- {
- if (pdfStamper != null)
- pdfStamper.Close();
- if (pdfReader != null)
- pdfReader.Close();
- //把添加水印前的pdf文件刪除,保存最新的文件
- if (File.Exists(inputfilepath))
- {
- File.Delete(inputfilepath);
- }
- }
- }

这就是我们为什么要用一个零时路径了,先把产出的pdf放到零时路径,用来产生水印的时候读取,生成水印文件后,再把零时文件删除即可
其中的WatermarkCreater方法可以看我以前的博客,报表水印的产生http://www.cnblogs.com/xiaoshuai1992/p/waterMark.html,方法相同
至此可以达到客户的要求,但是一些样式的问题就需要大家仔细研究了,这就是我的实践过程,希望可以和大家一起学习了
使用ItextSharp产PDF完整操作的更多相关文章
- 利用ItextSharp产PDF完整操作
记得上回有写到用C#操作Excel(.net 4.0) 很多朋友说推荐用NPOI,的确,用微软自带的操作execl会有很大的问题.客户的主机不愿意安装excel, 这时我才意识到用自带组件完全是不行的 ...
- 利用NPOI组件产Excel完整操作
最终还是要使用NPOi了.刚开始做的是用com组件,发现如果本机不按照excel就不能使用,后来把其中一支改为了用Itextsharp产生pdf,但是还有几支批次要产生Excel,只能改用NPOI了. ...
- 利用Com组件产Excel完整操作
最近公司要批次产出报表,是利用控制台应用程序操作Excel,并设置各种样式. 在网上搜索此类的例子,但是感觉一些用法都已经发生了变化,我用的.net 4.0 ,Microsoft.Office.Int ...
- 基于iTextSharp的PDF文档操作
公司是跨境电商,需要和各种物流打交道,需要把东西交给物流,让他们发到世界各地.其中需要物流公司提供一个运单号,来追踪货物到达哪里?! 最近在和DHL物流公司(应该是个大公司)对接,取运单号的方式是调用 ...
- 基于iTextSharp的PDF操作(PDF打印,PDF下载)
基于iTextSharp的PDF操作(PDF打印,PDF下载) 准备 1. iTextSharp的简介 iTextSharp是一个移植于java平台的iText项目,被封装成c#的组件来用于C#生成P ...
- 根据路径获得文件名以及Aspose.Cells.dll操作excel 以及使用iTextSharp.text.pdf.PdfReader.dll对PDF的操作
string result = Regex.Match(str,@"[^\\]+$").Value;//正则表达式 this.listBox1.Items.Add(Path.Get ...
- .NET的那些事儿(9)——C# 2.0 中用iTextSharp制作PDF(基础篇) .
该文主要介绍如何借助iTextSharp在C# 2.0中制作PDF文件,本文的架构大致按照iTextSharp的操作文档进行翻译,如果需要查看原文,请点击一下链接:http://itextsharp. ...
- itextsharp去掉PDF加密
在操作PDF文件时会遇到PDF文件加密了,不能操作的问题,从网络中查找资料一上午,鼓捣出如下的代码,可实现将已加密的PDF转化成未加密的PDF文件,纯代码,无需借助PDF解密软件,使用前需要导入如下引 ...
- [PDF] PDFOperation--C#PDF文件操作帮助类 (转载)
点击下载 PDFOperation.rar 这个类是关于PDFOperation的帮助类,主要是实现C#PDF的文件操作,具体实现功能如下1.构造函数2.私有字段3.设置字体4.设置页面大小5.实例化 ...
随机推荐
- Java疯狂讲义(二)
- Hadoop_Lucene
http://codelife.me/blog/2012/11/03/jackson-polymorphic-deserialization/ http://itindex.net/blog/2012 ...
- viminfo: 无效的启动字符
当自己进入一个用户,使用vi打开一个文件时,出现以下情况: [gexd@localhost ~]$ vi test.c E575: viminfo: 无效的启动字符 位于行: int main() . ...
- 【充电器】小米手机2S电池座充——小米手机官网
ligh@local-host$ ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.0.3 [充电器]小米手机2S电池座充--小米手机官网 小米手机2S电池座 ...
- HDU 4569Special equations2012长沙邀请赛E题(数学知识)
Special equations Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- Strategic Game(匈牙利算法,最小点覆盖数)
Strategic Game Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- __autoload函数
./index.php----------------------------------------------------------------------------------<?ph ...
- ognl--数据运转的催化剂
原文链接:http://struts2.group.iteye.com/group/wiki/1353-ognl-catalyst-for-data-operation-in-struts2 首先让我 ...
- Apache ab 使用说明
第一章 简介 ab是Apache超文本传输协议(HTTP)的性能测试工具.其设计意图是描绘当前所安装的Apache的执行性能,主要是显示你安装的Apache每秒可以处理多少个请求. 第二章 说明 ab ...
- VC++在对话框中加入属性页
当一个基于对话框的程序中有相当多的控件时,你一定会想到使用属性页来将这些控件分类放置.本文针对这种方法来讨论几种可能实现的方案. 方案一本方案的例子请见源代码打包文件中的Property1部分 在对话 ...