itext添加标题(heading)--http://www.cnblogs.com/anrainie/archive/2012/03/21/2410013.html

itext添加标题(heading)

标题即是在文档在大纲里的目录分级。

itext 2.7之后才有了该功能。

具体实现方式如下:

            FONT_PROJECT = RtfParagraphStyle.STYLE_HEADING_1;

            FONT_APPLICATION = RtfParagraphStyle.STYLE_HEADING_2;

            FONT_TYPE = RtfParagraphStyle.STYLE_HEADING_3;
com.lowagie.text.rtf.style.RtfParagraphStyle类继承自RtfFont,RtfFont又继承自Font。
它为Paragraph提供Style,使用方式和Font一致。
RtfParagraphStyle.STYLE_HEADING_1

该常量为RtfParagraphStyle的一个默认实现,表示第一级标题。

我们来看看它的实现:

复制代码
    /**
     * The style for level 1 headings.
     */
    public static final RtfParagraphStyle STYLE_HEADING_1 = new RtfParagraphStyle("heading 1", "Normal");
    /**
     * The style for level 2 headings.
     */
    public static final RtfParagraphStyle STYLE_HEADING_2 = new RtfParagraphStyle("heading 2", "Normal");
    /**
     * The style for level 3 headings.
     */
    public static final RtfParagraphStyle STYLE_HEADING_3 = new RtfParagraphStyle("heading 3", "Normal");
复制代码

注意第一个参数"heading 1",这是rtf的json属性值之一,它生产的json文件会包括如下部分:

{
{\style\s3 \ql\fi0\li0\ri0\f2\fs32\b\i\cf0 heading 3;}
{\style\s2 \ql\fi0\li0\ri0\f2\fs32\b\cf0 heading 2;}
{\style\s1 \ql\fi0\li0\ri0\f2\fs40\b\cf0 heading 1;}
}

这就是rtf本身的标题定义,了解了这些,我们就能用

STYLE_HEADING_1
STYLE_HEADING_2
STYLE_HEADING_3 

来定制三级菜单了。

示例:

复制代码
public class HeadingTest {
    public static void main(String[] args) throws FileNotFoundException,
            DocumentException {
        String file = "C:\\Users\\my\\Desktop\\dome2.doc";
        Document document = new Document(PageSize.A4);
     RtfWriter2.getInstance(document,new FileOutputStream(file));
        document.open();
        
        document.add(new Paragraph("1", RtfParagraphStyle.STYLE_HEADING_1));
        document.add(new Paragraph("2", RtfParagraphStyle.STYLE_HEADING_2));
        document.add(new Paragraph("3", RtfParagraphStyle.STYLE_HEADING_3));
        document.close();
        System.out.println("DONE!");
    }
}
复制代码


如果要生成多级标题呢?要稍微复杂点,假如我们参照 STYLE_HEADING_1 的写法来自定义一段:

RtfParagraphStyle heading_4 = new RtfParagraphStyle("heading 4",
                "Normal");

很快我们就会发现,会跑出一个NullPointException
原因在于,所有的RtfParagraphStyle都是被RtfDocumentHeader的RtfStylesheetList里所维护的

我们需要在RtfStylesheetList里注册我们自定义的这个heading_4。

实现方式如下:

 

复制代码
RtfWriter2 writer = RtfWriter2.getInstance(document,
                new FileOutputStream(file));
        document.open();
        RtfDocumentSettings settings = writer.getDocumentSettings();
        RtfParagraphStyle heading_4 = new RtfParagraphStyle("heading 4",
                "Normal");
        settings.registerParagraphStyle(heading_4);
复制代码

完成注册后,该RtfParagraphStyle就可以正常使用了

对之前的示例代码进行一点修改:

复制代码
public class HeadingTest {
    public static void main(String[] args) throws FileNotFoundException,
            DocumentException {
        String file = "C:\\Users\\gateway\\Desktop\\dome2.doc";
        Document document = new Document(PageSize.A4);
        RtfWriter2 writer = RtfWriter2.getInstance(document,
                new FileOutputStream(file));
        document.open();
        RtfDocumentSettings settings = writer.getDocumentSettings();
        RtfParagraphStyle heading_4 = new RtfParagraphStyle("heading 4",
                "Normal");
        RtfParagraphStyle heading_5 = new RtfParagraphStyle("heading 5",
                "Normal");
        settings.registerParagraphStyle(heading_4);
        settings.registerParagraphStyle(heading_5);
        
        document.add(new Paragraph("1", RtfParagraphStyle.STYLE_HEADING_1));
        document.add(new Paragraph("2", RtfParagraphStyle.STYLE_HEADING_2));
        document.add(new Paragraph("3", RtfParagraphStyle.STYLE_HEADING_3));
        document.add(new Paragraph("4", heading_4));
        document.add(new Paragraph("5", heading_5));
        document.close();
        System.out.println("DONE!");
    }
}
复制代码

实现效果如图所示:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值