Page 1 of 1

Overwrite a footer

PostPosted: Mon Dec 08, 2014 9:10 pm
by fsilvani
Hey,
i'm trying to overwrite a footer of a document with potentally many sections with my own footer.

I use the following code, inspired by this and the HeaderFooterCreate sample. This is all footer-related code i wrote.

Code: Select all

        Ftr ftr = FACTORY.createFtr();

        FooterPart footerPart;
        Relationship relationship;
        try
        {
            footerPart = new FooterPart();
            footerPart.setPackage(wordprocessingMLPackage);
            footerPart.setJaxbElement(ftr);
            relationship = wordprocessingMLPackage.addTargetPart(footerPart);
        }
        catch (InvalidFormatException exception)
        {
            throw new RuntimeException(exception);
        }

        // ftr.getContent().clear();

        // for (TextParagraphConfiguration textParagraphConfiguration : paragraphs)
        // {
        //     P p = WordParagraphUtil.createParagraph(textParagraphConfiguration);
        //     p.getPPr().setSpacing(NO_SPACING);
        //     ftr.getContent().add(p);
        // }
        P p =  FACTORY.createP();
        ftr.getContent().add(p);

        List<SectionWrapper> sections = wordprocessingMLPackage.getDocumentModel().getSections();

        for (SectionWrapper sectionWrapper : sections)
        {
            SectPr sectPr = sectionWrapper.getSectPr();
            // There is always a section wrapper, but it might not contain a
            // sectPr
            if (sectPr == null)
            {
                sectPr = FACTORY.createSectPr();
                wordprocessingMLPackage.getMainDocumentPart().addObject(sectPr);
                sectionWrapper.setSectPr(sectPr);
            }

            FooterReference footerReference = FACTORY.createFooterReference();
            footerReference.setId(relationship.getId());
            footerReference.setType(HdrFtrRef.DEFAULT);
            sectPr.getEGHdrFtrReferences().add(footerReference);
        }


Where FACTORY is Context.getWmlObjectFactory();.

Is there anything I am doing wrong here, because the output document won't open in word failing at this line in document.xml:
Image

The .rels file looks like
Code: Select all
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
   <Relationship Target="word/document.xml"
      Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Id="rId1" />
   <Relationship Target="docProps/core.xml"
      Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Id="rId2" />
   <Relationship Target="docProps/app.xml"
      Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Id="rId3" />
   <Relationship Target="word/footer.xml"
        Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer"
      Id="rId4" />
</Relationships>


Thanks,
Francesco Silvani

Re: Overwrite a footer

PostPosted: Mon Dec 08, 2014 10:11 pm
by jason
Looks like rId8 (the header) is missing?

Re: Overwrite a footer

PostPosted: Mon Dec 08, 2014 10:46 pm
by fsilvani
Yes. It is not in the /_rels/.rels but there is a /word/_rels/document.xml.rels that is:
Code: Select all
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
   <Relationship Target="settings.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings"
      Id="rId1" />
   <Relationship Target="footnotes.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/footnotes"
      Id="rId2" />
   <Relationship Target="endnotes.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/endnotes"
      Id="rId3" />
   <Relationship Target="webSettings.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings"
      Id="rId4" />
   <Relationship Target="theme/theme1.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme"
      Id="rId5" />
   <Relationship Target="fontTable.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable"
      Id="rId6" />
   <Relationship Target="styles.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles"
      Id="rId7" />
   <Relationship Target="header1.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/header"
      Id="rId8" />
   <Relationship Target="header.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/header"
      Id="rId9" />
   <Relationship Target="header2.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/header"
      Id="rId10" />
   <Relationship Target="header3.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/header"
      Id="rId11" />
   <Relationship Target="footer1.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer"
      Id="rId12" />
   <Relationship Target="footer.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer"
      Id="rId13" />
   <Relationship Target="footer2.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer"
      Id="rId14" />
   <Relationship Target="footer3.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer"
      Id="rId15" />
   <Relationship Target="footer4.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer"
      Id="rId16" />
   <Relationship Target="../customXml/item2.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXml"
      Id="rId17" />
   <Relationship Target="../customXml/item1.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXml"
      Id="rId18" />
</Relationships>


Also there is a /word/_rels/header1.xml.rels with
Code: Select all
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
   <Relationship Target="media/image1.png" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"
      Id="rId1"></Relationship>
</Relationships>


I compared the two docxs (with footer code / without footer code) and these filles didn't differ, so i didn't look into them.

It would be okay to remove all footers but i want to keep the header intact.

Thanks

Re: Overwrite a footer

PostPosted: Tue Dec 09, 2014 1:11 am
by fsilvani
Hey,

i didn't find the edit-button *blush*, so i'm posting again:

Adding the following line to /word/_rels/document.xml.rels and removing the footer from /_rels/.rels
Code: Select all
   <Relationship Target="word/footer.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer"
      Id="rId19" />

and changing rId4 to rId19 in document.xml resulted in a valid docx file.

Now I have to figure out how to tell docx4j to use the /word/_rels/document.xml.rels to store the relationship information.

I would appreciate any hint on that.

-- EDIT --

Ha, now it worked with the login and i can edit my post :)

The line
Code: Select all
            relationship = wordprocessingMLPackage.addTargetPart(footerPart);


needs to be

Code: Select all
            relationship = wordprocessingMLPackage.getMainDocumentPart().addTargetPart(footerPart);


That resolves the corrupt document.

Thanks for the hint with the missing relId. That brought me to the right track.