【问题标题】:Add footer text and page number in same line on docx file with docx4j java使用 docx4j java 在 docx 文件的同一行中添加页脚文本和页码
【发布时间】:2018-03-16 15:24:28
【问题描述】:

我正在尝试在 .docx 文档的页脚上添加小文本(左侧)和页码(右侧)在同一行中

到目前为止,我可以添加文本和页码,但在 2 行中

TextVersionv02312     
                                                                                   1

但我需要它

TextVersionv02312                                                                        1

我用来添加文本和页码的代码是:

private static Ftr createFooter(WordprocessingMLPackage wordMLPackage, String content, ObjectFactory factory, Part sourcePart, InputStream is) throws IOException, Throwable {
        Ftr footer = factory.createFtr();
        P paragraph = factory.createP();
        R run = factory.createR();
        /*
         * Change the font size to 8 points(the font size is defined to be in half-point
         * size so set the value as 16).
         */
        RPr rpr = new RPr();
        HpsMeasure size = new HpsMeasure();
        size.setVal(BigInteger.valueOf(16));
        rpr.setSz(size);
        run.setRPr(rpr);
        Text text = new Text();
        text.setValue(content);
        run.getContent().add(text);
        paragraph.getContent().add(run);
        footer.getContent().add(paragraph);

        // add page number
        P pageNumParagraph = factory.createP();
        addFieldBegin(factory, pageNumParagraph);
        addPageNumberField(factory, pageNumParagraph);
        addFieldEnd(factory, pageNumParagraph);
        footer.getContent().add(pageNumParagraph);
        return footer;
    }

private static void addPageNumberField(ObjectFactory factory, P paragraph) {
        R run = factory.createR();
        PPr ppr = new PPr();
        Jc jc = new Jc();
        jc.setVal(JcEnumeration.RIGHT);
        ppr.setJc(jc);
        paragraph.setPPr(ppr);
        Text txt = new Text();
        txt.setSpace("preserve");
        txt.setValue(" PAGE   \\* MERGEFORMAT ");
        run.getContent().add(factory.createRInstrText(txt));
        paragraph.getContent().add(run);

    }

我一直在考虑在页脚添加一个表格或类似的东西来将元素放在同一行,但似乎我过于复杂了。

或者我可以将页码附加到文本段落

你怎么看?

提前致谢!

【问题讨论】:

    标签: java docx4j


    【解决方案1】:

    您可以在 Word 中以任何方式执行此操作,例如,使用制表位。 (或者如您所说,表格,但如果您想要居中则右对齐,我会说居中对齐然后右对齐标签)

    最简单的方法是在 Word 中正确处理,然后使用 docx4j webapp 或 Docx4j Helper Word AddIn,从该示例文档生成相应的 Java 代码。

    【讨论】:

    • 最后,不能使用制表符。但是,使用空格,它可以工作。问题是文档的边距何时发生变化。对我来说幸运的是,要求发生了变化。谢谢!
    猜你喜欢
    • 2016-07-25
    • 1970-01-01
    • 1970-01-01
    • 2012-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-03
    • 2018-03-18
    相关资源
    最近更新 更多