itext 生成word目录标题格式样式

研究了好几天用itext生成word目录,在别人的指导下终于做到了这一步:生成标题格式和样式。在生成好的word文档中还要手工生成,插入->引用->索引和目录->目录。如果那位能直接生成目录省去手工操作请指教。我用的包是iText-2.1.7.jar和iText-rtf-2.1.7.jar两个。下面是例子:

package com.zfsoft.test;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.rtf.RtfWriter2;
import com.lowagie.text.rtf.style.RtfParagraphStyle;

public class Test2 {


public static void main(String[] args) throws DocumentException, FileNotFoundException {
Document document = new Document(PageSize.A4.rotate());
RtfWriter2.getInstance(document, new FileOutputStream("d:\\test.doc"));
document.open();
Font titleFont = new Font(Font.NORMAL,16, Font.BOLD);
/* 设置标题1格式 */
RtfParagraphStyle rtfGsBt1 = RtfParagraphStyle.STYLE_HEADING_1;
rtfGsBt1.setAlignment(Element.ALIGN_CENTER);
rtfGsBt1.setStyle(Font.BOLD);
rtfGsBt1.setSize(14);
/* 设置标题2格式 */
RtfParagraphStyle rtfGsBt2 = RtfParagraphStyle.STYLE_HEADING_2;
rtfGsBt2.setAlignment(Element.ALIGN_LEFT);
rtfGsBt2.setStyle(Font.NORMAL);
rtfGsBt2.setSize(12);
Paragraph title = new Paragraph("测试");
title.setAlignment(Element.ALIGN_CENTER);
title.setFont(titleFont);
document.add(title);
//正文
title = new Paragraph("1.第一章");
title.setFont(rtfGsBt1);
document.add(title);
title = new Paragraph("1.1 第一节");
title.setFont(rtfGsBt2);
document.add(title);
title = new Paragraph("1.2 第二节");
title.setFont(rtfGsBt2);
document.add(title);
title = new Paragraph("2.第二章");
title.setFont(rtfGsBt1);
document.add(title);
title = new Paragraph("2.1 第一节");
title.setFont(rtfGsBt2);
document.add(title);
title = new Paragraph("2.2 第二节");
title.setFont(rtfGsBt2);
document.add(title);
document.close();
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值