怎么将所有的分节符替换成分页符,有没有相关代码进行操作
@321227850 您可以使用以下代码:
Document doc = new Document("input.docx");
// Iterate through the sections in reverse order
for (int i = doc.getSections().getCount() - 1; i > 0; i--) {
Section section = doc.getSections().get(i);
// Get the last paragraph of the previous section
Section previousSection = doc.getSections().get(i - 1);
Paragraph lastParagraph = previousSection.getBody().getLastParagraph();
// Insert a page break at the end of the last paragraph
Run pageBreak = new Run(doc, ControlChar.PAGE_BREAK);
lastParagraph.appendChild(pageBreak);
// Append all nodes from the current section to the previous section
while (section.getBody().hasChildNodes()) {
previousSection.getBody().appendChild(section.getBody().getFirstChild());
}
// Remove the current section
doc.getSections().remove(section);
}
// Save the modified document
doc.save("output.docx");
Right:heart: 123
1 Like