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

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

问题描述

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

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类,这是需要发生的事情.您需要在每个分节符处添加一个段落.然后,您需要在该段落中创建一个section properties元素.顺便说一句,默认节可以在没有段落包装的文档正文末尾找到,这是默认节,它包含了之前节之前的所有内容.因此,节的属性位于节的末尾,而不是您期望的开始.并且除最后一节外的所有节属性都包含在一个段落中.最后一部分是默认部分.是不是很奇怪现在,在您要具有特殊标题的部分中,需要添加页眉/页脚关系.请看最后一部分,以了解其外观.您可以复制它.然后最困难的部分是您将必须手动为这些关系创建一个新的页眉部分,因为如果您仅使用现有方法创建新的默认页眉或首页页眉,它将为默认部分返回当前页眉或页脚(创建一个(如果尚不存在).这意味着您将必须遍历创建标头代码并对其进行概括,以创建零件并将关系插入到您想要标头/页脚所要使用的部分中.

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天全站免登陆