使用带有多个标头的docx4j创建docx [英] Create docx using docx4j with multiple headers

查看:451
本文介绍了使用带有多个标头的docx4j创建docx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用docx4j为每个不同的部分创建新标头?

我看到MS Word提供了创建功能通过使用分节符创建不同的节来不同的标题。但是我想使用docx4j来实现这个功能。

I've seen that MS Word provides functionality to create different headers by creating different sections using section breaks. But I want to implement this functionality using docx4j.

例如:
如果我要创建一个可能包含产品列表的docx文件,一些术语和条件,以及其他一些部分。并且我希望每个部分的标题都显示在页面顶部(如产品摘要:第1页,共2页,条款和条件:第3页,共3页)。

For example: If I've to create a docx files that may contains list of products, some terms and conditions, and some other sections. And I want title for each section to be displayed at top with paging (Like Product Summary: Page 1 of 2, Terms & Conditions: Page 3 of 3).

此外,我正在使用MainDocumentPart.addAltChunk()方法在文档中编写html内容以创建Document。

Also I'm using MainDocumentPart.addAltChunk() method to write html content in the document for creating Document.

在此先感谢。

-

问候,

Sanshey Sachdeva

推荐答案

最后我得到了解决方案。

Finally I got the solution.

下面的一块代码允许我处理部分:

SectPr sectPr = null;
if(isLastSection){
    List<SectionWrapper> sections = wordprocessingMLPackage.getDocumentModel().getSections();
    sectPr = sections.get(sections.size() - 1).getSectPr();
    if (sectPr==null ) {
        sectPr = objectFactory.createSectPr();
        wordprocessingMLPackage.getMainDocumentPart().addObject(sectPr);
        sections.get(sections.size() - 1).setSectPr(sectPr);
    }
}
else{
    sectPr = objectFactory.createSectPr();
}

此代码允许我为每个部分创建单独的标题:

HeaderPart headerPart = new HeaderPart(new PartName("/word/header"+ (countHeader++) +".xml"));
headerPart.setPackage(wordprocessingMLPackage);
headerPart.setRelationshipType(Namespaces.HEADER);
headerPart.setJaxbElement(objectFactory.createHdr());
Relationship relationship = documentPart.addTargetPart(headerPart);
HeaderReference headerReference = objectFactory.createHeaderReference();
headerReference.setId(relationship.getId());
headerReference.setType(HdrFtrRef.DEFAULT);
sectPr.getEGHdrFtrReferences().add(headerReference);

这段代码允许我为每个部分创建单独的分页:

CTPageNumber ctPageNumber = objectFactory.createCTPageNumber();
ctPageNumber.setStart(BigInteger.ONE);
sectPr.setPgNumType(ctPageNumber);

这篇关于使用带有多个标头的docx4j创建docx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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