请问怎么给文档中指定的Section插入 FieldType.FIELD_PAGE 页码字段,并指定页码的位置是右下角
@ouchli 请看下面的简单代码:
Document doc = new Document("C:\\Temp\\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
// Move DocumentBuilder to the required section.
// For demonstration purposes move to the lst one.
builder.moveToSection(doc.getSections().indexOf(doc.getLastSection()));
// Move DocumentBuilder into the section footer.
builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
// Set paragraph alignment to the right.
builder.getParagraphFormat().setAlignment(ParagraphAlignment.RIGHT);
// Insert PAGE field.
builder.insertField("PAGE");
doc.save("C:\\Temp\\out.docx");
请问添加页码时,怎么指定页码应用在 本节及之后的所有节呢
那请问怎么删除一个分节符,但不删除 节 的body内容呢
@ouchli 您可以使用以下代码删除两个部分之间的分节符:
Document doc = new Document("C:\\Temp\\in.docx");
// get the first and second section from the document
Section first = doc.getSections().get(0);
Section second = doc.getSections().get(1);
// Merge content of the section and remove the second section.
first.appendContent(second);
second.remove();
doc.save("C:\\Temp\\out.docx");
原始文档:文档附件.docx (45.2 KB)
DocumentBuilder documentBuilder = new DocumentBuilder(doc);
for (HeaderFooter headersFooter : headersFooters)
{
if (headersFooter.getHeaderFooterType() == HeaderFooterType.FOOTER_PRIMARY)
{
Paragraph newPara = new Paragraph(doc);
headersFooter.insertAfter(newPara, headersFooter.getLastChild());
documentBuilder.moveTo(newPara);
documentBuilder.getParagraphFormat().setAlignment(ParagraphAlignment.RIGHT);
documentBuilder.insertField(FieldType.FIELD_PAGE, true);
doc.updateFields();
}
}
添加页码22.docx (39.4 KB)
通过上述代码,添加页码,生成的文档: 为什么页码是从2开始的
我就是要给第二节加页码,首页不需要页码,也就是从第二页开始加页码,但是页码是从2开始的,应该从1开始才对,完整代码是:
DocumentBuilder documentBuilder = new DocumentBuilder(doc);
Section section = doc.getSections().get(1);
HeaderFooterCollection headersFooters = section.getHeadersFooters();
for (HeaderFooter headersFooter : headersFooters)
{
if (headersFooter.getHeaderFooterType() == HeaderFooterType.FOOTER_PRIMARY)
{
Paragraph newPara = new Paragraph(doc);
headersFooter.insertAfter(newPara, headersFooter.getLastChild());
documentBuilder.moveTo(newPara);
documentBuilder.getParagraphFormat().setAlignment(ParagraphAlignment.RIGHT);
documentBuilder.insertField(FieldType.FIELD_PAGE, true);
doc.updateFields();
}
}
@ouchli 您应该指定节页起始编号和标志以重新开始编号:
................
DocumentBuilder documentBuilder = new DocumentBuilder(doc);
Section section = doc.getSections().get(1);
// Set page starting number for section
section.getPageSetup().setRestartPageNumbering(true);
section.getPageSetup().setPageStartingNumber(1);
..............
按照您的代码指定了起始编号之后还是不行,麻烦您帮忙调试一下
那请问怎么把分节符从第一页移到第二页呢
@ouchli 您可以将章节开头从连续页更改为新页。
Document doc = new Document("C:\\Temp\\in.docx");
DocumentBuilder documentBuilder = new DocumentBuilder(doc);
Section section = doc.getSections().get(1);
// Change section start
section.getPageSetup().setSectionStart(SectionStart.NEW_PAGE);
// Set page starting number for section
section.getPageSetup().setRestartPageNumbering(true);
section.getPageSetup().setPageStartingNumber(1);
HeaderFooterCollection headersFooters = section.getHeadersFooters();
for (HeaderFooter headersFooter : headersFooters)
{
if (headersFooter.getHeaderFooterType() == HeaderFooterType.FOOTER_PRIMARY)
{
Paragraph newPara = new Paragraph(doc);
headersFooter.insertAfter(newPara, headersFooter.getLastChild());
documentBuilder.moveTo(newPara);
documentBuilder.getParagraphFormat().setAlignment(ParagraphAlignment.RIGHT);
documentBuilder.insertField(FieldType.FIELD_PAGE, true);
doc.updateFields();
}
}
doc.save("C:\\Temp\\out.docx");
java.lang.IllegalStateException: Document structure was changed。
在执行 Field.remove() 时,报这个错。在这之前,我做了section的合并。为什么会报这个错呢