下面的代码:
Document document = new Document();
DocumentBuilder builder = new DocumentBuilder(document);
Section currentSection = builder.getCurrentSection();
currentSection.getPageSetup().setPageStartingNumber(10);
currentSection.getPageSetup().setOddAndEvenPagesHeaderFooter(true);
// 偶数页页码在左侧
builder.moveToHeaderFooter(HeaderFooterType.FOOTER_EVEN);
builder.getParagraphFormat().setAlignment(ParagraphAlignment.LEFT);
builder.insertField("PAGE", "");
// 奇数页页码在右侧
builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
builder.getParagraphFormat().setAlignment(ParagraphAlignment.RIGHT);
builder.insertField("PAGE", "");
builder.moveToDocumentEnd();
document.save("/Users/pizhai/Downloads/docx_word.docx", SaveFormat.DOCX);
其中的代码:currentSection.getPageSetup().setPageStartingNumber(10);已经设置了页码从10开始,
但是实际效果为:
docx_word.docx (8.1 KB)
页码依旧从1开始,怎么让自定义开始的页码生效。
@pizhai2
Cause
The issue you’re experiencing with the page numbering starting from 1 instead of 10 is likely due to the way the page numbering fields are being inserted. The setPageStartingNumber(10)
method is correctly setting the starting number, but the fields for the page numbers may not be updating accordingly.
Solution
To ensure that the page numbers reflect the starting number you’ve set, you need to update the fields after inserting them. You can do this by calling the updateFields()
method on the document after you’ve inserted the page number fields.
Code
Here’s how you can modify your code:
Document document = new Document();
DocumentBuilder builder = new DocumentBuilder(document);
Section currentSection = builder.getCurrentSection();
currentSection.getPageSetup().setPageStartingNumber(10);
currentSection.getPageSetup().setOddAndEvenPagesHeaderFooter(true);
// 偶数页页码在左侧
builder.moveToHeaderFooter(HeaderFooterType.FOOTER_EVEN);
builder.getParagraphFormat().setAlignment(ParagraphAlignment.LEFT);
builder.insertField("PAGE", "");
// 奇数页页码在右侧
builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
builder.getParagraphFormat().setAlignment(ParagraphAlignment.RIGHT);
builder.insertField("PAGE", "");
// 更新所有字段
document.updateFields();
document.save("/Users/pizhai/Downloads/docx_word.docx", SaveFormat.DOCX);
Next Steps
- Run the modified code to generate the document.
- Open the saved document and verify that the page numbers now start from 10 as expected.
- If you still encounter issues, check for any caching in the document or other conflicting settings that may affect page numbering.
Please let me know if you need further assistance!
1 Like
无效的解决方案。无效的解决方案。无效的解决方案。无效的解决方案。
@pizhai2 您还应该设置“RestartPageNumbering”属性:
Document doc = new Document("C:\\Temp\\in.html");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.getPageSetup().setOddAndEvenPagesHeaderFooter(true);
builder.getPageSetup().setRestartPageNumbering(true);
builder.getPageSetup().setPageStartingNumber(10);
builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
builder.getParagraphFormat().setAlignment(ParagraphAlignment.LEFT);
builder.insertField("PAGE");
builder.moveToHeaderFooter(HeaderFooterType.FOOTER_EVEN);
builder.getParagraphFormat().setAlignment(ParagraphAlignment.RIGHT);
builder.insertField("PAGE");
doc.save("C:\\Temp\\out.docx");
out.docx (10.4 KB)
1 Like