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!");
}
}

實現效果如圖所示:





免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM