zoukankan      html  css  js  c++  java
  • 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!");
    }
    }

    实现效果如图所示:




  • 相关阅读:
    前端笔试题----JavaScript部分
    前端笔试题----html,css部分
    JS基础--执行环境及作用域
    关于css3 flex布局
    Ceph万兆内网与系统万兆迁移
    从0开始的InfiniBand硬件踩坑过程
    Redis实战与分析
    ceph osd 自动挂载的N种情况
    集群IPtables转发与防火墙
    ceph 常见问题百科全书---luminous安装部署篇
  • 原文地址:https://www.cnblogs.com/anrainie/p/2410013.html
Copyright © 2011-2022 走看看