插入页码时,没有正常插入

文字文稿2.docx (21.5 KB)

Document document = new Document("D:\\ccc\\文字文稿2.docx");
Section section = document.getLastSection();
PageSetup pageSetup = section.getPageSetup();
pageSetup.setSectionStart(SectionStart.NEW_PAGE);
pageSetup.setRestartPageNumbering(true);
pageSetup.setPageStartingNumber(DocConstants.NUM_1);
pageSetup.setPageNumberStyle(NumberStyle.ARABIC);
DocumentBuilder documentBuilder = new DocumentBuilder(document);
HeaderFooterCollection headersFooters = section.getHeadersFooters();
try
{
    for (HeaderFooter headersFooter : headersFooters)
    {
        if (headersFooter.getHeaderFooterType() == HeaderFooterType.FOOTER_PRIMARY)
        {
            NodeCollection<Paragraph> paraList = headersFooter.getChildNodes(NodeType.PARAGRAPH, false);

            Paragraph paraNotBlank = (Paragraph)Arrays.stream(paraList.toArray())
                    .filter(paragraph->StringUtils.isNotBlank(paragraph.getText())).findFirst().orElse(new Paragraph(document));
            Shape shape = new Shape(document, ShapeType.TEXT_BOX);
            paraNotBlank.appendChild(shape);
            Paragraph newPara = new Paragraph(document);
            shape.appendChild(newPara);
            documentBuilder.moveTo(newPara);
            documentBuilder.insertField(FieldType.FIELD_PAGE, true);
        }
    }

}
catch (Exception exception)
{
    throw new RuntimeException();
}

按照上述代码给最后一个section增加页码,为什么得到的结果看不到页码呢

@ouchli 在您的代码中,创建了空段落,但未将其添加到文档中:

Paragraph paraNotBlank = (Paragraph)Arrays.stream(paraList.toArray())
    .filter(paragraph->StringUtils.isNotBlank(paragraph.getText())).findFirst().orElse(new Paragraph(document));

请像这样修改代码:

Paragraph paraNotBlank = (Paragraph)Arrays.stream(paraList.toArray())
    .filter(paragraph->StringUtils.isNotBlank(paragraph.getText())).findFirst().orElse(new Paragraph(document));

if(paraNotBlank.getParentNode()==null)
    headersFooter.appendChild(paraNotBlank);

您好,改了之后还是不行呢,能给我发一下您的结果嘛
自动修订结果.docx (20.5 KB)

这是我的结果

@ouchli 您需要说明形状的宽度和高度,因为默认情况下是 0。

Shape shape = new Shape(document, ShapeType.TEXT_BOX);
shape.setWidth(25);
shape.setHeight(25);
Paragraph newPara = new Paragraph(document);
shape.appendChild(newPara);
documentBuilder.moveTo(newPara);
documentBuilder.insertField(FieldType.FIELD_PAGE, true);
paraNotBlank.appendChild(shape);

下面是您提供的代码的输出结果。
output.docx (20.5 KB)

谢谢您的解答

image.png (959 字节)

请问怎么将页码位置和格式设置为
image.png (5.8 KB)
这样的格式:位置在右下角
边框设置为 无边框

@ouchli 在这种情况下,我建议您使用表格:

Document doc = new Document("input.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
Section section = doc.getLastSection();
HeaderFooterCollection headersFooters = section.getHeadersFooters();
for (HeaderFooter headersFooter : headersFooters)
{
    if (headersFooter.getHeaderFooterType() == HeaderFooterType.FOOTER_PRIMARY)
    {
        NodeCollection<Paragraph> paraList = headersFooter.getChildNodes(NodeType.PARAGRAPH, false);

        Paragraph paraNotBlank = (Paragraph) Arrays.stream(paraList.toArray())
                .filter(paragraph-> StringUtils.isNotBlank(paragraph.getText())).findFirst().orElse(new Paragraph(doc));
        String footerText = paraNotBlank.getText().trim();

        Table table = builder.startTable();
        builder.insertCell();
        builder.getParagraphFormat().setAlignment(ParagraphAlignment.LEFT);
        builder.write(footerText);

        builder.insertCell();
        builder.getParagraphFormat().setAlignment(ParagraphAlignment.RIGHT);
        builder.insertField(FieldType.FIELD_PAGE, true);
        builder.endRow();
        builder.endTable();
        table.clearBorders();

        paraNotBlank.getParentNode().insertAfter(table, paraNotBlank);
        paraNotBlank.remove();
    }
}

doc.save("output.docx");

output.docx (20.3 KB)

使用表格无法满足我的要求,
ShapeType.TEXT_BOX无法设置边框属性和位置吗?

@ouchli 舒尔,这是可能的。感谢您提供更多信息。您可以使用以下代码:

Shape shape = new Shape(doc, ShapeType.TEXT_BOX);
shape.setWidth(5);
shape.setHeight(5);
shape.setStroked(false);
shape.setFilled(false);
shape.setWrapType(WrapType.NONE);
shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.RIGHT_MARGIN);
shape.setRelativeVerticalPosition(RelativeVerticalPosition.PARAGRAPH);
shape.setLeft(-7);
shape.setTop(-5);
Paragraph newPara = new Paragraph(doc);
shape.appendChild(newPara);
builder.moveTo(newPara);
builder.insertField(FieldType.FIELD_PAGE, true);
shape.getTextBox().setFitShapeToText(true);
paraNotBlank.appendChild(shape);