Copy Header/Footer from template docx to another
Posted: Mon Jan 14, 2019 8:58 pm
I want to copy the complete Header from the template docx file and replace with it the header from the main docx file.
At the moment I have no idea how to do this because I do not have much experience with docx4j.
Current code status for removing header:
final WordprocessingMLPackage wordProcessingPackageHeader = DocxUtils
.getWordProcessingPackage(DocxUtils.class.getResourceAsStream("main-document.docx"));
Is it necessary to remove the header before setting it or can I also replace it?
How could I remove the header of the main document and then set the header from the template doc?
At the moment I have no idea how to do this because I do not have much experience with docx4j.
Current code status for removing header:
final WordprocessingMLPackage wordProcessingPackageHeader = DocxUtils
.getWordProcessingPackage(DocxUtils.class.getResourceAsStream("main-document.docx"));
- Code: Select all
final RelationshipsPart relationshipsPart = wordProcessingPackageHeader.getMainDocumentPart().getRelationshipsPart();
final List<Relationship> relationships = relationshipsPart.getRelationships().getRelationship();
for (final Relationship r : relationships) {
if (r.getType().equals(Namespaces.HEADER)) {
final Part part = relationshipsPart.getPart(r);
part.remove();
}
}
Is it necessary to remove the header before setting it or can I also replace it?
How could I remove the header of the main document and then set the header from the template doc?