@alexey.noskov , good morning alexey ! i run a document in my software, and part of the result is :
, 50, and 51 in this picture are not usable endnote, they’re just numbers
and i extract the method, and run it again, the result is :
as you can see, the endnote with the same repeated number 5050 and 5151 , which combines a usable endnote and a plain number. but the last one ‘50’ is redirected to the first ‘50’ so, that result in the first picutre above.
here’s the test file and test code are here :
test.zip (512.3 KB)
how can i fix it.
the expected result that i want is ,the second ‘50’ is not ‘Error! Bookmark not define’, i want it to display 50, even if it’s just a number
ah there’s another unexpected problem, the original file is :
however, after running the code, the result is :
why ? and how could i fix it ?
@Madecho Most likely there is a problem in your implementation of:
ReferenceService.convertEndNotesToReferences(doc, builder, footnoteNodes, endNoteReferences);
Could you please provide this method here for our reference? We will check it and provide you more information.
@alexey.noskov oh sorry , i forgot.
test.zip (512.9 KB)
here is the revised code, attached with convertEndNotesToReferences method
@Madecho Is your goal to replace endnotes with simple hyperlinks and bookmarks? If so you can modify your code like this to make references bidirectional:
Document doc = new Document("C:\\Temp\\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
doc.updateActualReferenceMarks();
NodeCollection<Footnote> footnoteNodes = doc.getChildNodes(NodeType.FOOTNOTE, true);
convertEndNotesToReferences(doc, builder, footnoteNodes);
doc.save("C:\\Temp\\out.docx");
public static void convertEndNotesToReferences( Document doc, DocumentBuilder builder,
NodeCollection<Footnote> footnoteNodes){
int bkIndex = 0;
for (Footnote footnote : footnoteNodes) {
// process only endnotes
if(footnote.getFootnoteType() == FootnoteType.FOOTNOTE)
continue;
String bkName = "_footnote" + bkIndex;
String bkRefName = "_footnote_ref" + bkIndex;
bkIndex++;
footnote.getParentNode().insertBefore(new BookmarkStart(doc, bkName), footnote);
footnote.getParentNode().insertAfter(new BookmarkEnd(doc, bkName), footnote);
try {
builder.moveToBookmark(bkName, true, true);
} catch (Exception e) {
throw new RuntimeException(e);
}
builder.getFont().setSuperscript(true);
builder.insertHyperlink(footnote.getActualReferenceMark(), bkRefName, true);
for (Node child : (Iterable<Node>) footnote.getChildNodes(NodeType.ANY, false)) {
doc.getLastSection().getBody().appendChild(child);
if (child.isComposite()) {
CompositeNode composite = (CompositeNode) child;
for (SpecialChar sc : (Iterable<SpecialChar>)composite.getChildNodes(NodeType.SPECIAL_CHAR, true)) {
// u002是一个脚注字符
if (sc.getText().equals("\u0002")) {
builder.moveTo(sc);
builder.startBookmark(bkRefName);
builder.endBookmark(bkRefName);
builder.insertHyperlink(footnote.getActualReferenceMark(), bkName, true);
sc.remove();
}
}
}
}
footnote.remove();
}
}
Regarding Error! Bookmark not defined., there is NOTEREF
field in your document, but since footnote is removed, there is no corresponding bookmark. You can unlink the field before updating fields to make the reference to be represented as a simple text. Or you can replace NOTEREF
with a simple REF
field.
1 Like
@alexey.noskov thank you so much alexey. by the way, is there a simple way to change the style of Endnote ? like from ‘1’,‘2’,‘3’ to ‘[1]’ , ‘[2]’,‘[3]’ ?
@Madecho Do you mean upon replacing them to hyperlinks? If so, you can simply modify your code like this:
builder.insertHyperlink("["+footnote.getActualReferenceMark()+"]", bkRefName, true);
1 Like