IText 生成页脚页码
做doc文檔報表的時候可能遇到這樣的需求:
每一個頁面需要頁碼,用IText可以完成這樣的需求。
IText生成doc文檔需要三個包:iTextAsian.jar,iText-rtf-2.1.4.jar,iText-2.1.4.jar
代碼親測無錯,如下所示:
import com.lowagie.text.*;
import com.lowagie.text.Font;
import com.lowagie.text.rtf.RtfWriter2;
import java.awt.*;
import java.io.FileOutputStream;
/**
?* 生成頁腳頁碼
?* User: HYY
?* Date: 13-8-1
?* Time: 下午9:54
?* To change this template use File | Settings | File Templates.
?*/
public class FooterTest {
??? /**
???? * @param args
???? */
??? public static void main(String[] args) throws Exception {
??????? // 創建word文檔,并設置紙張的大小
??????? Document document = new Document(PageSize.A4);
??????? //設置存放位置
??????? RtfWriter2.getInstance(document, new FileOutputStream("C:\\Documents and Settings\\Administrator\\桌面//1.doc"));
??????? document.open();
??????? //正文(為了生成兩頁紙)
??????? Table table = new Table(7,60);
??????? document.add(table);
??????? //————————頁腳設置————————
??????? //頁腳段落(這里為空,可以自己添加文字)
??????? Paragraph paraFooter = new Paragraph();
??????? Font font = new Font();
??????? //頁腳的字體大小
??????? font.setSize(12f);
??????? font.setColor(new Color(0,0,0));
??????? paraFooter.setFont(font);
??????? paraFooter.setAlignment("center");
??????? //(參數一)頁腳的段落?? 和? (參數二)是否有頁碼
??????? HeaderFooter footer = new HeaderFooter(paraFooter, false);
??????? //頁腳的對齊方式(應該在footer設置而不是段落中設置)
??????? footer.setAlignment(1);
??????? document.setFooter(footer);
??????? document.close();
??? }
}
?
附:
HeaderFooter類的構造器官方文檔:
HeaderFooter(Phrase?before, boolean?numbered)
??????????Constructs a Header-object with a pagenumber at the end.
由上可知,第二個參數是專門設置頁腳頁碼的參數。
true表示生成頁碼,false表示不生成頁碼。
轉載于:https://www.cnblogs.com/wuyou/p/3236671.html
總結
以上是生活随笔為你收集整理的IText 生成页脚页码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C#通过FFmpeg获得视频文件参数
- 下一篇: Delphi 自带的 Base64 编解