private static void addPageHeader(Document doc, String no, boolean isNote) throws Exception {
Section section = null;
if (isNote) {
section = doc.getSections().get(1);
} else {
section = doc.getSections().get(0);
}
// 检查当前节的页眉是否已经存在
HeaderFooter existingHeader = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_PRIMARY);
if (existingHeader != null) {
// 如果存在,移除已存在的页眉
section.getHeadersFooters().remove(existingHeader);
}
// 设置当前节的起始页码
section.getPageSetup().setPageStartingNumber(2);
// 创建新的页眉
HeaderFooter header = new HeaderFooter(doc, HeaderFooterType.HEADER_PRIMARY);
section.getHeadersFooters().add(header);
// 使用DocumentBuilder添加内容
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToSection(doc.getSections().indexOf(section));
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
Table table = builder.startTable();
// 插入第一行
builder.insertCell();
builder.getCellFormat().setWidth(ConvertUtil.millimeterToPoint((21.6))); // 设置第一列宽度
InputStream is = new FileInputStream("/Users/lpg/Documents/工作/中合通用平台/zhonghe-guangan-java/zhonghe-admin/src" +
"/main/resources/test_report_header.png");
builder.insertImage(is, ConvertUtil.millimeterToPoint(17.7), ConvertUtil.millimeterToPoint(16.3));
builder.getCellFormat().setVerticalMerge(CellMerge.Continue.getValue()); // 开始合并
builder.insertCell();
builder.setBold(true);
builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
builder.getFont().setSize(14);
builder.write("产品质量监督检验研究院\n");
builder.getFont().setName("宋体");
builder.getFont().setSize(16);
builder.write("检 测 报 告");
builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
builder.getCellFormat().setVerticalMerge(CellMerge.Continue.getValue()); // 开始合并
builder.getCellFormat().setWidth(ConvertUtil.millimeterToPoint((113.3))); // 设置第一列宽度
builder.insertCell();
builder.getFont().setBold(false);
builder.getFont().setSize(10.5);
builder.getFont().setName("Times New Roman");
builder.write(no); // 输入第三列的内容
builder.getFont().setBold(true);
builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
builder.getCellFormat().setHorizontalMerge(CellMerge.None.getValue());
builder.getCellFormat().setWidth(ConvertUtil.millimeterToPoint((40))); // 设置第一列宽度
builder.getRowFormat().setHeight(ConvertUtil.millimeterToPoint((8.5))); // 设置第一列宽度
builder.endRow(); // 结束当前行
// 插入第二行
builder.insertCell();
builder.getCellFormat().setVerticalMerge(CellMerge.Continue.getValue()); // 继续合并第一列
builder.insertCell();
builder.getCellFormat().setVerticalMerge(CellMerge.Continue.getValue()); // 继续合并第二列
builder.insertCell();
builder.getFont().setBold(false);
builder.getFont().setName("Times New Roman");
builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
builder.getCellFormat().setVerticalMerge(CellMerge.None.getValue()); // 不合并第三列
builder.write("第");
builder.insertField("PAGE ", null);
builder.write("页 共");
// 插入域代码
Field formulaFieldNumPages = builder.insertField(FieldType.FIELD_FORMULA, false);
builder.moveTo(formulaFieldNumPages.getSeparator());
builder.insertField(FieldType.FIELD_NUM_PAGES, true);
builder.write("-4");
builder.moveToField(formulaFieldNumPages, true);
builder.write("页");
//设置文本内容居中
builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
builder.getRowFormat().setHeight(ConvertUtil.millimeterToPoint((8.5)));
builder.endRow(); // 结束当前行
//边框加粗
table.setBorders(LineStyle.SINGLE, 1.5, Color.BLACK);
table.setTextWrapping(TextWrapping.AROUND);
table.setPreferredWidth(PreferredWidth.fromPoints(ConvertUtil.millimeterToPoint((175))));
//设置表格居中
table.setAlignment(TableAlignment.CENTER);
//设置文本环绕
table.setTextWrapping(TextWrapping.AROUND);
builder.endTable();
doc.updatePageLayout();
doc.updateFields();
}
使用的是DocumentBuilder 来构建的, builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER) 这个方法文本居中了,但是上下不居中