为 Apache POIFS 中的每个部分生成不同的标头 [英] Generate different header for each Section in Apache POIFS

查看:21
本文介绍了为 Apache POIFS 中的每个部分生成不同的标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将差异文档 (Docx) 与 Apache PIOFS 连接起来,但无法成功定义每个部分的适当标题.我的第一页显示有自己的标题,然后我只有一个标题用于所有其他页面.并且无法成功找到如何为每个部分设置不同的标题.

I am trying to concatenate difference document (Docx) with Apache POIFS, but can't success defining the appropriate header on each section. My first page is displayed with its own header, then I have only one header for all other pages. And just can't succeed in finding how to have different headers for each Section.

拜托,如果有人知道如何做到这一点......这是我的代码,它只是将我所有的附加标头连接到原始标头(最后,我只有一个标头,所有不同的原始标头的所有值都连接在其中).

Please, if someone knows how to do this... Here is my code, which simply concat all my additional headers to the original one (at the end, I have only one header, with all values of all my different original headers concatenate in it).

    private void appendHeader(final XWPFDocument destDocx, final XWPFDocument additionalDocx) throws IOException, XmlException {
        ////// ==>  No matter how I create the policy, the result is the same
//        final XWPFHeaderFooterPolicy destPolicy = destDocx.createHeaderFooterPolicy();
        final XWPFHeaderFooterPolicy destPolicy = new XWPFHeaderFooterPolicy(destDocx);
        final List<XWPFHeader> destHeaders = destDocx.getHeaderList();
        LOGGER.trace("Dest header size: {}", destHeaders.size());
        LOGGER.trace("Additional header size: {}", additionalDocx.getHeaderList().size());

        // Loop on additional headers to add them to the dest doc.
        for (final XWPFHeader additionaHeader : additionalDocx.getHeaderList()) {
            // Get the new header I want for this section
            final String additionalXmlHeader = additionaHeader._getHdrFtr().xmlText();

            // Format it properly
            final CTHdrFtr newHeader = CTHdrFtr.Factory.parse(additionalXmlHeader);
            // And add it to the document
            ////// ==> No matter how I set my header...
//            destHeader.setHeaderFooter(newHeader);
            final XWPFHeader destHeader = new XWPFHeader(destDocx, newHeader);
        }

        // This simply increases each time I'm adding a new header while I would like to have specific header for each section of the document
        LOGGER.trace("New dest header size: {}", destHeaders.size());
    }

干杯.奥利维尔

推荐答案

这会很困难,因为 POI 尚不支持创建多个部分,并且它也不支持向默认部分以外的任何部分添加页眉和页脚(恰好是文档中的最后一部分).您可能可以使用 CT 课程做您想做的事情,但是您将不得不研究部分的工作方式,因为我还没有解决所有的各种要求.

This will be difficult as POI does not yet support creation of multiple sections, and it also does not support adding headers and footers to any section other than the default section (which happens to be the last section in the document). You might be able to do what you want using the CT classes, but you are going to have to research how sections work as I haven't yet worked out all the various requirements.

如果您要使用 CT 课程,需要执行以下操作.您需要在每个分节符处添加一个段落.然后您需要在该段落中创建一个部分属性元素.顺便说一句,默认部分可以在没有段落包装器的文档正文的末尾找到,这是默认部分,它包含它之前的所有内容,直到上一节段落.因此,节属性位于节的末尾,而不是像您期望的那样位于开头.并且除最后一节之外的所有节属性都包含在一个段落中.最后一部分是默认部分.很奇怪不是吗?现在在您想要具有特殊标题的部分中,您需要添加页眉/页脚关系.看看最后一部分,看看它是什么样子.你可以复制它.然后困难的部分是您必须为这些关系手动创建一个新的页眉部分,因为如果您只是使用现有方法创建一个新的默认页眉或第一页页眉,它将返回默认部分的当前页眉或页脚(创建一个如果它不存在).这意味着您必须完成创建页眉代码并对其进行概括以创建部件并将关系插入到您想要页眉/页脚的部分中.

Here's what needs to happen if you are going to use CT classes. You need to add a paragraph at each section break. Then you need to create a section properties element in that paragraph. BTW, the default section can be found at the end of the document body without a paragraph wrapper, this is the default section, and it encompasses everything before it up to the previous section paragraph. So section properties are found at the end of the section rather than at the beginning as you would expect. And all section properties except the last section are contained in a paragraph. And the last section is the default section. Weird isn't it? Now inside the section that you want to have special headings you need to add header/footer relations. Look at the last section to see what that looks like. You can replicate it. Then the hard part is you will have to manually create a new header part for those relations because If you just create a new default or first page header using the existing methods, it will return the current header or footer for the default section (creating one if it doesn't already exist). This means you will have to go through the create header code and generalize that to create the part and insert the relations into the section you want the header/footer for.

现在,如果您只想在文档的第一页上有一个不同的标题,您可以使用 POI 来实现,因为它不需要多个部分.下面是你将如何去做.

Now if all you want is to have a different header on the first page of your document, you can do that with POI because it doesn't require multiple sections. Here is how you would go about that.

    XWPFDocument doc = new XWPFDocument();

    XWPFParagraph p = doc.createParagraph();

    XWPFRun r = p.createRun();
    r.setText("Some Text");
    r.setBold(true);
    r = p.createRun();
    r.setText("Goodbye");

    // create header/footer functions insert an empty paragraph
    XWPFHeader head = doc.createHeader(HeaderFooterType.FIRST);
    head.createParagraph().createRun().setText("First page header");

    XWPFFooter foot = doc.createFooter(HeaderFooterType.FIRST);
    foot.createParagraph().createRun().setText("First page footer");

    // create header/footer functions insert an empty paragraph
    XWPFHeader head = doc.createHeader(HeaderFooterType.DEFAULT);
    head.createParagraph().createRun().setText("header");

    XWPFFooter foot = doc.createFooter(HeaderFooterType.DEFAULT);
    foot.createParagraph().createRun().setText("footer");

    OutputStream os = new FileOutputStream(new File("header2.docx"));
    doc.write(os);
    doc.close();

即使存在枚举,也不支持创建偶数/奇数页眉和页脚(从 3.16 beta 1 开始).

Even/odd page headers and footers are also not yet supported (as of 3.16 beta 1) for creation even though the enum exists.

这篇关于为 Apache POIFS 中的每个部分生成不同的标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆