首先我们需要的是itext.jar包。
itext是一个开源的rtf、pdf读写项目,属于sourceforge。rtf即是Rich Text Format,由微软公司开发的跨平台文档格式,缺点在于保存的大小会比较大,可能是由于考虑了兼容性的关系。
它的读写方式是比较通用的,无论是写rtf、pdf还是html,都有统一的Document,不同的格式有不同的写入器。如RTFWriter\PDFWriter。
下面记录几个概念:
1、 Font,字体对象
2、 Chunk,文本块,处理文本的最小单位,可以设置字体,颜色,等。
3、 Phrase,短语,由一个或多个Chunk组成。
4、 Paragraph,段落,由一个或多个Chunk以及Phrace组成。
5、 Chapter,章节对象
6、 Section,小节对象
7、 Table,表格对象
8、 Image,图像对象
备份一段代码:
package com.anrainie.ide.flow.utilities.tool;
import com.anrainie.ide.core.translators.StyleTranslator;
import com.anrainie.ide.flow.utilities.document.MarsApplication;
import com.anrainie.ide.flow.utilities.document.MarsCptContainer;
import com.anrainie.ide.flow.utilities.document.MarsCptLevel1;
import com.anrainie.ide.flow.utilities.document.MarsCptLevel2;
import com.anrainie.ide.flow.utilities.document.MarsProject;
import com.anrainie.ide.flow.utilities.document.MarsTechCpt;
import com.anrainie.ide.flow.utilities.document.MarsTrade;
import com.anrainie.ide.flow.utilities.nls.Messages;
import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import com.lowagie.text.Cell;
import com.lowagie.text.Chapter;
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.Section;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.rtf.RtfWriter2;
public class WordTool {
private static BaseFont bfChinese;
private static Font FONT_TITLE;
private static Font FONT_PROJECT = null;
private static Font FONT_APPLICATION = null;
private static Font FONT_TYPE = null;
private static Font FONT_TABLE_HEAD = null;
private static Font FONT_TABLE_CONTENT = null;
static {
try {
bfChinese = BaseFont.createFont(BaseFont.TIMES_ROMAN,
BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
FONT_TITLE = new Font(bfChinese, 24, Font.BOLD);
FONT_PROJECT = new Font(bfChinese, 16, Font.BOLD);
FONT_PROJECT.setFamily("Symbol");
FONT_APPLICATION = new Font(bfChinese, 12, Font.BOLD);
FONT_APPLICATION.setFamily("Courier");
FONT_TYPE = new Font(bfChinese, 11, Font.BOLD | Font.ITALIC);
FONT_TABLE_HEAD = new Font(bfChinese, 10, Font.BOLD);
FONT_TABLE_CONTENT = new Font(bfChinese, 10, Font.NORMAL);
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 按Word格式导出TCD文档
*
* @param fileList
* @param path
*/
public static void exportTcdDocument(List<IFile> fileList, String path) {
List<MarsCptContainer> containerList = DocumentInfoUtil
.getTcdDocumentInfo(fileList);
Document document = new Document(PageSize.A4);
try {
RtfWriter2.getInstance(document, new FileOutputStream(path));
document.open();
Paragraph title = createTitle("技术组件详细清单");
document.add(title);
int count = 0;
for (MarsCptContainer container : containerList) {
Chapter chapter = new Chapter(
createProjectText(container.getName()), ++count);
for (MarsCptLevel1 pretreat : container.getPretreatList()) {
Section appSection = chapter.addSection(new Paragraph(
pretreat.getName(), FONT_APPLICATION));
appSection.setNumberDepth(2);
for (MarsCptLevel2 category : pretreat.getSubList()) {
Section cateSection = appSection
.addSection(new Paragraph(category.getName(),
FONT_TYPE));
cateSection.setNumberDepth(3);
Table table = new Table(6);
table.setWidth(100);
table.setWidths(new int[] { 10, 20, 10, 20, 20, 20 });
table.addCell(createHeadCell(Messages.ExcelTool_TCD_NAME_CN));
table.addCell(createHeadCell(Messages.ExcelTool_TCD_NAME_EN));
table.addCell(createHeadCell(Messages.ExcelTool_TCD_STYLE));
table.addCell(createHeadCell(Messages.ExcelTool_TCD_INPARAMS));
table.addCell(createHeadCell(Messages.ExcelTool_TCD_OUTPARAMS));
table.addCell(createHeadCell(Messages.ExcelTool_TCD_ANNOTATION));
for (MarsTechCpt cpt : category.getCptList()) {
table.addCell(createContentCell(cpt
.getChineseName()));
table.addCell(createContentCell(cpt
.getEnglishName()));
table.addCell(createContentCell(StyleTranslator
.translateValueToDesc(cpt.getStyle())));
String inparams = "";
for (int i = 0; i < cpt.getInparam().size(); i++) {
inparams += (i + 1) + "."
+ cpt.getInparam().get(i) + "\n";
}
String outparams = "";
for (int i = 0; i < cpt.getOutparam().size(); i++) {
outparams += (i + 1) + "."
+ cpt.getOutparam().get(i) + "\n";
}
table.addCell(createContentCell(inparams));
table.addCell(createContentCell(outparams));
table.addCell(createContentCell(cpt.getAnnocation()));
}
cateSection.add(table);
}
}
document.add(chapter);
}
DocumentInfoUtil.successRemind();
} catch (DocumentException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
document.close();
}
}
/**
* 导出Word格式的TWF文档
*
* @param fileList
* TWF文件列表
* @param path
* 导出文件的位置
*/
public static void exportTwfDocument(List<IFolder> fileList, String path) {
List<MarsProject> projectList = DocumentInfoUtil
.getTwfDocumentInfo(fileList);
Document document = new Document(PageSize.A4);
try {
RtfWriter2.getInstance(document, new FileOutputStream(path));
document.open();
Paragraph title = createTitle("交易详细清单");
document.add(title);
int count = 0;
for (MarsProject project : projectList) {
Chapter chapter = new Chapter(
createProjectText(project.getName() + "["
+ project.getDesc() + "]"), ++count);
for (MarsApplication app : project.getAppList()) {
Section appSection = chapter.addSection(new Paragraph(app
.getName() + "[" + app.getDesc() + "]",
FONT_APPLICATION));
appSection.setNumberDepth(2);
Map<String, List<MarsTrade>> tradeSectionList = new HashMap<String, List<MarsTrade>>();
for (MarsTrade trade : app.getTradeList()) {
if (tradeSectionList.keySet().contains(
trade.getCategory())) {
tradeSectionList.get(trade.getCategory())
.add(trade);
} else {
List<MarsTrade> list = new ArrayList<MarsTrade>();
list.add(trade);
tradeSectionList.put(trade.getCategory(), list);
}
}
for (String category : tradeSectionList.keySet()) {
Section cateSection = appSection
.addSection(new Paragraph(category, FONT_TYPE));
cateSection.setNumberDepth(3);
Table table = new Table(6);
table.setWidth(100);
table.setWidths(new int[] { 15, 20, 15, 10, 20, 20 });
table.addCell(createHeadCell("交易名称"));
table.addCell(createHeadCell("交易描述"));
table.addCell(createHeadCell("交易模型名称"));
table.addCell(createHeadCell("交易作者"));
table.addCell(createHeadCell("编译结果"));
table.addCell(createHeadCell("备注"));
for (MarsTrade trade : tradeSectionList.get(category)) {
table.addCell(createContentCell(trade.getName()));
table.addCell(createContentCell(trade.getDesc()));
table.addCell(createContentCell(trade
.getTradeModel()));
table.addCell(createContentCell(trade.getAuthor()));
table.addCell(createContentCell(trade
.getCompileResult()));
table.addCell(createContentCell(trade.getTooltip()));
}
cateSection.add(table);
}
}
document.add(chapter);
}
DocumentInfoUtil.successRemind();
} catch (DocumentException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
document.close();
}
}
private static Cell createContentCell(String string) {
Cell cell = new Cell();
cell.add(new Paragraph(string, FONT_TABLE_CONTENT));
return cell;
}
private static Cell createHeadCell(String string) {
Cell cell = new Cell();
cell.add(new Paragraph(string, FONT_TABLE_HEAD));
cell.setBackgroundColor(Color.YELLOW);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_CENTER);
return cell;
}
private static Paragraph createProjectText(String text) {
Paragraph title = new Paragraph(text, FONT_PROJECT);
return title;
}
private static Paragraph createTitle(String text) {
Paragraph title = new Paragraph(text, FONT_TITLE);
title.setAlignment(Element.ALIGN_CENTER);
return title;
}
}
来源:http://www.cnblogs.com/anrainie/archive/2012/03/07/2383884.html