Page 1 of 1

Overwrite a footer

PostPosted: Mon Dec 08, 2014 9:10 pm
by fsilvani
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;
            footerPart = new FooterPart();
            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();

        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();

            FooterReference footerReference = FACTORY.createFooterReference();

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:

The .rels file looks like
Code: Select all
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="">
   <Relationship Target="word/document.xml"
      Type="" Id="rId1" />
   <Relationship Target="docProps/core.xml"
      Type="" Id="rId2" />
   <Relationship Target="docProps/app.xml"
      Type="" Id="rId3" />
   <Relationship Target="word/footer.xml"
      Id="rId4" />

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="">
   <Relationship Target="settings.xml" Type=""
      Id="rId1" />
   <Relationship Target="footnotes.xml" Type=""
      Id="rId2" />
   <Relationship Target="endnotes.xml" Type=""
      Id="rId3" />
   <Relationship Target="webSettings.xml" Type=""
      Id="rId4" />
   <Relationship Target="theme/theme1.xml" Type=""
      Id="rId5" />
   <Relationship Target="fontTable.xml" Type=""
      Id="rId6" />
   <Relationship Target="styles.xml" Type=""
      Id="rId7" />
   <Relationship Target="header1.xml" Type=""
      Id="rId8" />
   <Relationship Target="header.xml" Type=""
      Id="rId9" />
   <Relationship Target="header2.xml" Type=""
      Id="rId10" />
   <Relationship Target="header3.xml" Type=""
      Id="rId11" />
   <Relationship Target="footer1.xml" Type=""
      Id="rId12" />
   <Relationship Target="footer.xml" Type=""
      Id="rId13" />
   <Relationship Target="footer2.xml" Type=""
      Id="rId14" />
   <Relationship Target="footer3.xml" Type=""
      Id="rId15" />
   <Relationship Target="footer4.xml" Type=""
      Id="rId16" />
   <Relationship Target="../customXml/item2.xml" Type=""
      Id="rId17" />
   <Relationship Target="../customXml/item1.xml" Type=""
      Id="rId18" />

Also there is a /word/_rels/header1.xml.rels with
Code: Select all
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="">
   <Relationship Target="media/image1.png" Type=""

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.


Re: Overwrite a footer

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

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=""
      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.