用Itext操作word需要导入三个jar包:iText-2.1.7.jar、iText-rtf-2.1.7.jar、iTextAsian.jar
private void execute(){
document = new Document(PageSize.A4);
try {
RtfWriter2.getInstance(document, new FileOutputStream(file));
document.open();
//设置中文字体
BaseFont bfChinese = BaseFont.createFont("STSongStd-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
//标题字体风格
Font titleFont = new Font(bfChinese,16, Font.BOLD);
//正文字体风格
Font contextFont = new Font(bfChinese,10,Font.NORMAL);
//标题
Paragraph title = new Paragraph("文章标题",titleFont);
title.setAlignment(Element.ALIGN_CENTER);//设置标题格式对齐方式
// title.setFont(titleFont);//设置字体
document.add(title);
Paragraph p = new Paragraph("文书制作");
//设置段落居中,其中1为居中对齐,2为右对齐,3为左对齐
p.setAlignment(1);
// 设置字体,字号,加粗,颜色
// p.setFont(new Font(bfChinese, 20, Font.BOLD, new Color(255, 0, 0)));
document.add(p);
//调用系统的“楷体”字体,设置该段落时楷体
BaseFont bf = BaseFont.createFont("C:\\Windows\\Fonts\\simkai.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
p = new Paragraph( " itext可以实现插入段落,可以设置段落的缩进,居中,首行缩进,段前距,段后距。可以设置字体,字号,格式。功能比较齐全。", new Font(bf, 16, Font.NORMAL, new Color(0, 0, 0)));
// 设置段落缩进
p.setIndentationLeft(20);
// 设置首行缩进
p.setFirstLineIndent(30f);
// 设置段后距和段前距
p.setSpacingAfter(10f);
p.setSpacingBefore(100f);
// p.setFont(new Font(bf, 16, Font.NORMAL, new Color(0, 0, 0)));
document.add(p);
p = new Paragraph("itext可以插入表格,设置表格的行列数,可以设置表格边框,可以设置表格位置,可以设置表格总宽度和每一列的宽度。单元格的插入和内容可控", new Font(bf, 16, Font.NORMAL, new Color(0, 0, 0)));
// p.setFont(new Font(bf, 16, Font.NORMAL, new Color(0, 0, 0)));
document.add(p);
// 创建有三列的表格
Table table = new Table(3,3);
// 设置table的边框宽度为0
table.setBorderWidth(1f);
// 设置表格右对齐,其中1为居中对齐,2为右对齐,3为左对齐
table.setAlignment(2);
// 设置各列的宽度
int[] widths = { 25, 25, 50 };
table.setWidths(widths);
table.setPadding(0);
table.setSpacing(0);
// 读取图片(参数为gif、jpg、png格式的图片都可以),设置图片大小
Image image = Image.getInstance("E:/timg.jpg");
// 设置图片的绝对大小,宽和高
image.scaleAbsolute(50f, 50f);
// 设置图片居中显示
image.setAlignment(Image.MIDDLE);
// 创建单元格,并且将单元格内容设置为图片
Cell cell = new Cell(image);
// 设置单元格边框为0
cell.setBorder(0);
// cell.setHeader(true);
// cell.setColspan(3);// 设置表格为三列
// cell.setRowspan(3);// 设置表格为三行
table.addCell(cell);
cell = new Cell("该单元格的长度是200");
cell.setBorder(0);
table.addCell(cell);
cell = new Cell("该单元格的长度是100");
// cell.setWidth("10px");
table.addCell(cell);
// cell.setBorder(1);
// 设置垂直居中
cell.setVerticalAlignment(1);
// 设置水平居中
cell.setHorizontalAlignment(1);
// document.add(new Paragraph("用java生成word文件"));
document.add(table);
Table table2 = new Table(3);
int width[] = {25,25,50};//设置每列宽度比例
table2.setWidths(width);
table2.setWidth(90);//占页面宽度比例
table2.setAlignment(Element.ALIGN_CENTER);//居中
table2.setAlignment(Element.ALIGN_MIDDLE);//垂直居中
table2.setAutoFillEmptyCells(true);//自动填满
table2.setBorderWidth(1);//边框宽度
//设置表头
Cell haderCell = new Cell("表格表头");
haderCell.setHeader(true);
haderCell.setColspan(3); //合并3个单元格
table2.addCell(haderCell);
table2.endHeaders();
Font fontChinese = new Font(bfChinese,12,Font.NORMAL,Color.GREEN);
Cell cell2 = new Cell(new Paragraph("这是一个3*3测试表格数据",fontChinese));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table2.addCell(cell2);
table2.addCell(new Cell("#1"));
// table2.addCell(new Cell("#2"));
// table2.addCell(new Cell("#3"));
document.add(table2);
// 关闭document
document.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
需要导入的引用:
import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.rtf.RtfWriter2;
文档截图: