I agree that having to treat the first paragraph separately is annoying,
however, there are already several classes that do that (e.g., XWPFTableRow,
which creates a new cell and that cell has a paragraph), so unless we modify
them all to not create an initial child object, it seems like consistency of
behavior argues for creating the initial paragraph.
What I have working at the moment is footnote creation where the footnote is
created with an initial paragraph that contains the required footnote reference
in the first run.
As this reference is mandatory and otherwise requires dropping into the CT
classes it seemed reasonable to add the paragraph that then contains the
reference.
I also added createParagraph() and createTable() (and now that I think about it
I also need the insert* methods as well).
I suppose those methods could add the footnoteref if it's not already present.
Here's my real-world code that uses my new methods (it consumes a simple XML
structure that mirrors the OOXML structure and constraints):
private void makeFootnote(XWPFParagraph para, XmlCursor cursor) throws
DocxGenerationException {
XWPFFootnote note = para.getDocument().createFootnote();
cursor.toFirstChild();
boolean isFirst = true;
do {
String tagName = cursor.getName().getLocalPart();
if ("p".equals(tagName)) {
XWPFParagraph p =
isFirst ?
note.getParagraphs().get(0) : note.createParagraph();
isFirst = false;
makeParagraph(p, cursor);
} else if ("table".equals(tagName)) {
makeTable(note, cursor);
} else {
System.out.println("- [WARN] Unexpected element
'" + tagName + "' in <fn>. Ignored.");
}
} while (cursor.toNextSibling());
// Add footnote reference to the paragraph:
CTP paraCTP = para.getCTP();
CTR run = paraCTP.addNewR();
run.addNewRPr().addNewRStyle().setVal("FootnoteReference");
run.addNewFootnoteReference().setId(note.getId());
}
Notice the "isFirst" Boolean--in the abstract it would be nice not to have to
do that.
I will certainly defer to the direction from the project team on this aspect of
the design.
Cheers,
Eliot
--
Eliot Kimber
http://contrext.com
On 7/20/18, 11:06 AM, "Mark Murphy" <[email protected]> wrote:
According to the spec, the id's for footnotes and end notes are unique to
the note type (footnote or end note). You may want to create this so that
it can handle footnotes or end notes as there are only cosmetic differences
between the two. Similar to header/footer. Id's should be handled
internally so the user does not have to concern themselves with them. If
you are looking at header/footer for examples, please do not generate an
empty paragraph when a footnote or end note is created. This is too
limiting because it forces treating the first paragraph differently than
any potential additional paragraphs when adding content.
Maybe for simplicity it would be nice, for someone creating a document, to
simply be able to insert a footnote or end note with some text at the
current location in the paragraph without having to specify anything else.
The InsertFootnote () method would then do all the necessary background
work (create part if necessary, generate the next id, insert the footnote
reference) and insert the footnote text in the footnote part. It could also
return the footnote for additional modification like stylizing or adding
additional paragraphs, tables, images, etc..
On Fri, Jul 20, 2018 at 5:33 AM Eliot Kimber <[email protected]> wrote:
> For footnotes I need to set a unique ID on each newly-created getCTFtnEdn.
> In my personal code I was maintaining my own global ID counter that I used
> to set IDs on things.
>
> However, I'm not finding a similar mechanism in the POI code--is there one
> or is there a reliable technique for constructing new IDs, e.g., based on
> the size of lists of things? I'm pretty sure footnote IDs only need to be
> unique across the footnotes but I'm not 100% sure on the general semantics
> of IDs in OOXML.
>
> Searching for "setId" I'm not finding any hits outside the test cases, so
> either this is something the current API just doesn't need to do (because
> it doesn’t automatically create objects that require IDs) or I'm missing
> where ID setting happens.
>
> Thanks,
>
> Eliot
> --
> Eliot Kimber
> http://contrext.com
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]