利用iTextSharp输出RTF文档,同时定义文档页眉页尾表格

昨天作一个家具项目的web开发,要求依据客户报价单格式从订单页面直接输入报价单打印。最初想直接在webpage中列出打印即可,但发现要求每页有固定的页眉(头部)和页尾(底部),网上找了半天,有的思路是设置CSS样式,html中th进行CSS表达,最终也没看明白,而且也不是我想要的格式就放弃了。有的论坛里在推荐iTextSharp,到官方网站看了看,主要输出pdf格式,rtf格式也可输出但受一些限制,我的客户要求word文档格式,alright!现学现用吧。
 
1.下载itextsharp-4.0.2-dll.zip,解压后得到itextsharp.dll,加入项目中(VS2005开发);
2.代码中引用:
//..
using iTextSharp.text;
using iTextSharp.text.rtf;
using iTextSharp.text.pdf;
using iTextSharp.text.rtf.headerfooter;
//..
3.代码块例子
//..

 try
        {
               Document document = new Document(PageSize.A4, 50, 50, 50, 50);

             
RtfWriter2 writer = RtfWriter2.GetInstance(document, new FileStream(Server.MapPath("~/Quotations/") + "quotation.rtf", FileMode.Create)); //RtfWriter2 ,不要用RtfWriter,Server.MapPath服务器上存放文件路径目录
              
writer.SetDataCacheStyle(iTextSharp.text.rtf.document.output.RtfDataCache.CACHE_DISK);
              
FontFactory.Register("c:\\windows\\fonts\\mingliu.ttc,1", "mingliu1"); //这个字体是特别指定的,一定要先在FontFactory注册,第二个参数是起了一个同名,后面FontFactory.GetFont用到这个同名


iTextSharp.text.Table headerTable = new iTextSharp.text.Table(6,7);
                  
headerTable.Border = iTextSharp.text.Rectangle.NO_BORDER;
                    headerTable.Cellpadding = 2;
                    headerTable.Cellspacing = 0;
                    int[] columnWidths = { 12, 30, 10, 18, 12, 23 }; // 各个column宽度所占百分比
                    headerTable.SetWidths(columnWidths);
                    headerTable.WidthPercentage = 100; // 整个headertable 100%占满页面

Cell titleCell = new Cell(new Phrase("**傢俱股份有限公司", FontFactory.GetFont("mingliu1", 12, iTextSharp.text.Font.BOLD)));
                    titleCell.Colspan = 6;
                    titleCell.Border = iTextSharp.text.Rectangle.NO_BORDER;
                    titleCell.HorizontalAlignment = Element.ALIGN_CENTER;
                    headerTable.AddCell(titleCell);

                    Cell titleCell1 = new Cell(new Phrase("报价单", FontFactory.GetFont("mingliu1", 12, iTextSharp.text.Font.BOLD)));
                    titleCell1.Colspan = 6;
                    titleCell1.Border = iTextSharp.text.Rectangle.NO_BORDER;
                    titleCell1.HorizontalAlignment = Element.ALIGN_CENTER;
                    headerTable.AddCell(titleCell1);
                    Cell blankCell = new Cell(new Phrase("", FontFactory.GetFont("mingliu1", 12, iTextSharp.text.Font.BOLD)));
                    blankCell.Colspan = 6;
                    blankCell.Border = iTextSharp.text.Rectangle.NO_BORDER;
                    headerTable.AddCell(blankCell);

//省略部分代码,总之你定义的table要规划好,注意colspan,rowspan的用法..

 

  //以下是将定义好的table给document.Header,如果需要页尾,也是一样先定义table,然后header.SetHeaderFooter()方法+document.Footer = footertable,在此略去不写


RtfHeaderFooterGroup header = new RtfHeaderFooterGroup();
                    header.SetHeaderFooter(
                                        new iTextSharp.text.rtf.headerfooter.RtfHeaderFooter(headerTable),
                                        iTextSharp.text.rtf.headerfooter.RtfHeaderFooter.DISPLAY_ALL_PAGES);
document.Header = header;
   

//打开文档

 document.Open();

//正文的编辑

iTextSharp.text.Table dataTable = new iTextSharp.text.Table(9);
                    dataTable.DefaultCellBorder = Rectangle.NO_BORDER;
                  
                    dataTable.Cellspacing = 0;
                    dataTable.Cellpadding = 2;
                    dataTable.DefaultHorizontalAlignment = Element.ALIGN_CENTER;
                    int[] columnWidths3 = { 6, 15, 20,10,10,8,10,11,20 }; // percentage
                    dataTable.SetWidths(columnWidths3);
                    dataTable.WidthPercentage = 100; // percentage

 //正文也是一个table,数据自己定义加入,略去

 //正文定义的table加入Document
document.Add(dataTable);
 //关闭文档

document.Close();             

//文档编辑完后,我是通过resposne输出
                Response.Clear();
                Response.Buffer = true;
                Response.Charset = "UTF8";
                  Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.ContentType = "application/msword";//设置输出文件类型为WORD文件。
Response.AddHeader("Content-Disposition", "attachment;filename=" + "quotation.doc");

Response.WriteFile(Server.MapPath("~/Quotations/") + "quotation.rtf");
Response.Flush();
Response.End();

//代码完毕

 

参考:

http://itextsharp.sourceforge.net/ 官方网站

http://itextdocs.lowagie.com/tutorial/ 教程

posted @ 2010-02-07 17:24  吴永富  阅读(1999)  评论(0编辑  收藏  举报