1.导入maven
<!--Spire.Doc-->
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc.free</artifactId>
<version>3.9.0</version>
<scope>system</scope>
<systemPath>${basedir}/src/main/webapp/WEB_INF/lib/Spire.Doc.jar</systemPath>
</dependency>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.pdf</artifactId>
<version>3.11.6</version>
<scope>system</scope>
<systemPath>${basedir}/src/main/webapp/WEB_INF/lib/Spire.Doc.jar</systemPath>
</dependency>
<!--用于生成图片 -->
<dependency>
<groupId>org.jfree</groupId>
<artifactId>jfreechart</artifactId>
<version>1.0.19</version>
</dependency>
2.基本方法
static{
Document document = new Document();
Section sec = document.addSection();
}
/**
* 设置标题样式(大小:20;居中)
*/
private static void titleStyle(Section sec, String text, BuiltinStyle builtinStyle) {
Paragraph p = sec.addParagraph();
TextRange tr = p.appendText(text);
p.applyStyle(builtinStyle);
//设置字体大小和颜色
tr.getCharacterFormat().setTextColor(Color.black);
tr.getCharacterFormat().setFontName("宋体");
tr.getCharacterFormat().setFontSize(20);
tr.getCharacterFormat().setItalic(false);
p.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
//设置段后间距
p.getFormat().setAfterSpacing(10);
}
/**
* 设置表格标题行
*/
private static void tableTitle(Table table, String[] header) {
TableRow row = table.getRows().get(0);
row.isHeader(true);
row.setHeight(20);
row.setHeightType(TableRowHeightType.Exactly);
for (int i = 0; i < header.length; i++) {
row.getCells().get(i).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
Paragraph p = row.getCells().get(i).addParagraph();
p.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
TextRange range1 = p.appendText(header[i]);
range1.getCharacterFormat().setFontName("Arial");
range1.getCharacterFormat().setFontSize(12f);
range1.getCharacterFormat().setBold(true);
}
}
/**
* 设置页边距
*/
private static void setPageMargins(Section sec, Float top, Float bottom, Float left, Float right) {
sec.getPageSetup().getMargins().setTop(top);
sec.getPageSetup().getMargins().setBottom(bottom);
sec.getPageSetup().getMargins().setLeft(left);
sec.getPageSetup().getMargins().setRight(right);
}