论坛首页 Java企业应用论坛

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

浏览 5358 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (11) :: 隐藏帖 (0)
作者 正文
   发表时间:2011-04-13  
    研究了好几天用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();
}

}
  • 大小: 22.6 KB
  • 大小: 23 KB
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics