On some docx footer was visible in document, on others not visible, but still is present in xml. Sample of docx with not visible footer is attached as "sample.docx".
Comparison of xml of docx with visible and not visible footer does not show any difference.
Below is code for generating footer.
- Code: Select all
private static void addFooterToDocument(WordprocessingMLPackage wordMLPackage, String footerText)throws InvalidFormatException {
ObjectFactory factory = Context.getWmlObjectFactory();
Relationship relationship = createFooterPart(wordMLPackage, factory, footerText);
createFooterReference(relationship, wordMLPackage, factory);
}
private static Relationship createFooterPart(WordprocessingMLPackage wordMLPackage, ObjectFactory factory,
String vFooterText) throws InvalidFormatException {
FooterPart footerPart = new FooterPart();
footerPart.setPackage(wordMLPackage);
footerPart.setJaxbElement(createFooter(vFooterText, factory, wordMLPackage, footerPart));
return wordMLPackage.getMainDocumentPart().addTargetPart(footerPart);
}
private static Ftr createFooter(String contents, ObjectFactory factory,WordprocessingMLPackage wordMLPackage, Part sourcePart) {
Ftr footer = factory.createFtr();
byte[] imageBytes = null;
try {
imageBytes = GenerateQRCode.createQRImage(contents, 110) ;
}
catch(Exception e) {
log.error(e);
}
org.docx4j.wml.R rImage = null;
try {
if (imageBytes != null) {
rImage = runImagePart(wordMLPackage,sourcePart,imageBytes,"QRCode","QRCode",0,1);
}
}
catch (Exception e) {
log.error(e);
}
R run = factory.createR();
P paragraph = factory.createP();
RPr rpr = new RPr();
HpsMeasure size = new HpsMeasure();
size.setVal(BigInteger.valueOf(20));
rpr.setSz(size);
run.setRPr(rpr);
if(contents!= null) {
log.debug("contents = "+contents);
int i=0;
try(Scanner scan = new Scanner(contents);){
while (scan.hasNextLine() ){
String content = scan.nextLine();
log.debug("content = "+content);
if(i>0) {
Br br = factory.createBr();
run.getContent().add(br);
}
Text text = new Text();
text.setValue(content);
run.getContent().add(text);
i++;
}
}
}
paragraph.getContent().add(run);
if(rImage != null)
paragraph.getContent().add(rImage);
footer.getContent().add(paragraph);
return footer;
}
private static void createFooterReference(Relationship relationship,WordprocessingMLPackage wordMLPackage, ObjectFactory factory) {
List<SectionWrapper> sections = wordMLPackage.getDocumentModel().getSections();
SectPr sectionProperties = sections.get(sections.size() - 1)
.getSectPr();
if (sectionProperties == null) {
sectionProperties = factory.createSectPr();
wordMLPackage.getMainDocumentPart().addObject(sectionProperties);
sections.get(sections.size() - 1).setSectPr(sectionProperties);
}
List<CTRel> relations = sectionProperties.getEGHdrFtrReferences();
Iterator<CTRel> relationsItr = relations.iterator();
while (relationsItr.hasNext()) {
CTRel relation = relationsItr.next();
if (relation instanceof FooterReference) {
relationsItr.remove();
}
}
FooterReference firstPagefooterRef = factory.createFooterReference();
firstPagefooterRef.setId(relationship.getId());
firstPagefooterRef.setType(HdrFtrRef.FIRST);
sectionProperties.getEGHdrFtrReferences().add(firstPagefooterRef);
}