• 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表示不生成页码。

  • 相关阅读:
    公式中表达单个双引号【"】和空值【""】的方法及说明
    Ext.net CRUD
    水煮肉片
    配置传入电子邮件(Office SharePoint Server 管理中心帮助)
    CodeSmith开发系列资料总结
    报表中的Excel操作之Aspose.Cells(Excel模板)
    40个UI设计工具和资源
    配置Sharepoint传入/传出电子邮件a
    联想乐Pad_A1获取root权限
    Windows Azure
  • 原文地址:https://www.cnblogs.com/wuyou/p/3236671.html
Copyright © 2020-2023  润新知