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!");
}
}
实现效果如图所示:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)