Page 1 of 1

Endnotes

PostPosted: Mon Nov 29, 2010 6:20 pm
by GrandPoohBear
Hi all!

First off, I'm really happy to have found this project. It seems to be exactly what I need for my work. That being said, I'm having some trouble getting started.

I need to create a docx file from scratch that contains bulleted lists and endnotes. I can see from the files I've exported from Word that I'll need to make a Numbering part and an Endnotes part, and I've partially done some of this in code, but I keep hitting walls. Are there any simple examples that would show how to do something like this?

Many thanks!
--Andy

Re: Endnotes

PostPosted: Mon Nov 29, 2010 10:03 pm
by jason
Here's a basic example:
Code: Select all
      WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
      
      // Setup endnotes part
      EndnotesPart ep = new EndnotesPart();
      CTEndnotes endnotes = Context.getWmlObjectFactory().createCTEndnotes();
      ep.setJaxbElement(endnotes);
      wordMLPackage.getMainDocumentPart().addTargetPart(ep);
      
      // Add an endnote
      CTFtnEdn endnote = Context.getWmlObjectFactory().createCTFtnEdn();
      endnotes.getEndnote().add(endnote);
      
      endnote.setId(BigInteger.ONE);
      String endnoteBody = "<w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" ><w:pPr><w:pStyle w:val=\"EndnoteText\"/></w:pPr><w:r><w:rPr><w:rStyle w:val=\"EndnoteReference\"/></w:rPr><w:endnoteRef/></w:r><w:r><w:t xml:space=\"preserve\"> An endnote</w:t></w:r></w:p>";
      endnote.getEGBlockLevelElts().add( XmlUtils.unmarshalString(endnoteBody));
      
      // Add the body text referencing it
      String docBody = "<w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" ><w:r><w:t>the quick brown</w:t></w:r><w:r><w:rPr><w:rStyle w:val=\"EndnoteReference\"/></w:rPr><w:endnoteReference w:id=\"1\"/></w:r></w:p>";
      
      wordMLPackage.getMainDocumentPart().addParagraph(docBody);


If you look at the sample you created, you'll probably see it starts with 2 magic endnotes:
Code: Select all
  <w:endnote w:type="separator" w:id="-1">
    <w:p>
      <w:pPr>
        <w:spacing w:after="0" w:line="240" w:lineRule="auto"/>
      </w:pPr>
      <w:r>
        <w:separator/>
      </w:r>
    </w:p>
  </w:endnote>
  <w:endnote w:type="continuationSeparator" w:id="0">
    <w:p>
      <w:pPr>
        <w:spacing w:after="0" w:line="240" w:lineRule="auto"/>
      </w:pPr>
      <w:r>
        <w:continuationSeparator/>
      </w:r>
    </w:p>
  </w:endnote>


I've left these out. They relate to EndnotePr in DocumentSettingsPart. Pls refer to the OpenXML spec for more details (easily accessed via the search link above, though documentinteropinitiative.org seems down at the moment)

Re: Endnotes

PostPosted: Wed Dec 01, 2010 11:25 am
by GrandPoohBear
Thanks Jason!

Is it possible to create the endnoteReference the jaxb way? I don't see a type for endnoteReference (though I do see R.EndnoteRef, but that doesn't allow me to add an id). I tried using XmlUtils.unmarshalString on the endnoteReference but I get a parse exception when I do. I'm making the rest of the P the jaxb way, so it would be somewhat more difficult to add the endnotes in that way.

Any thoughts?

Thanks!
--Andy

Re: Endnotes

PostPosted: Wed Dec 01, 2010 5:31 pm
by jason
Try:

Code: Select all

    public CTFtnEdnRef createCTFtnEdnRef() {
        return new CTFtnEdnRef();
    }


etc, and then ...

Code: Select all
    @XmlElementDecl(namespace = "http://schemas.openxmlformats.org/wordprocessingml/2006/main", name = "endnoteReference", scope = R.class)
    public JAXBElement<CTFtnEdnRef> createREndnoteReference(CTFtnEdnRef value) {
        return new JAXBElement<CTFtnEdnRef>(_REndnoteReference_QNAME, CTFtnEdnRef.class, R.class, value);
    }


and add this JAXBElement<CTFtnEdnRef> object (necessary so JAXB knows which of an endnote or a footnote ref it is).

The OpenMainDocumentAndTraverse sample, run on a document containing end notes, ought to provide this info (though i didn't try it just know, so please report back either way - thanks).

.. Jason

Re: Endnotes

PostPosted: Fri Apr 15, 2011 5:41 am
by LokiTC
Hello,

I have also been trying to add endnotes in a document. I tried following along according to the first example posted, but no end note appeared in the final document. Is there a step missing here?

Thanks,

LokiTC

Re: Endnotes

PostPosted: Sat Apr 16, 2011 6:50 am
by LokiTC
Never mind, I found the problem.

I had been using

createREndnoteRef

where I should have used:

createREndnoteReference

L

Re: Endnotes

PostPosted: Fri Dec 05, 2014 10:44 am
by jason
See https://github.com/plutext/docx4j/blob/ ... teAdd.java
for a fully worked example of adding a footnote.

Adding an endnote is similar.