@alexey.noskov Hi alexey, goooood afternoon ! I came across another problem. How can I extract all the endnotes and attach them in the last page of File but without resetting the references ?
the original file is here test.docx (382.6 KB)
and the code that I use is :
Document doc = new Document("src/test/resources/docx/test.docx", loadOptions);
// 保存所有尾注段落的列表
ArrayList<Footnote> endnotesList = new ArrayList<>();
// 获取文档中的所有尾注
NodeCollection footnotes = doc.getChildNodes(NodeType.FOOTNOTE, true);
// 遍历所有的尾注
for (Footnote endnote : (Iterable<Footnote>)footnotes)
{
// 如果这是尾注
if (endnote.getFootnoteType() == FootnoteType.ENDNOTE)
{
// 保存尾注到列表中,但不删除
endnotesList.add(endnote);
}
}
// 获取文档最后一节
Section lastSection = doc.getLastSection();
// 确保最后一节有一个文本框来添加新的尾注段落
Body body = lastSection.getBody();
// 在文档末尾添加一个标题,标识为“尾注”
Paragraph titlePara = new Paragraph(doc);
titlePara.appendChild(new Run(doc, "EndNote:"));
body.appendChild(titlePara);
// 遍历保存的尾注并在文档末尾添加每个尾注的内容和标号
for (Footnote endnote : endnotesList)
{
// 创建新的段落用于保存尾注内容
Paragraph newPara = new Paragraph(doc);
// 遍历尾注的每个段落,将其内容复制到新段落中
for (Paragraph para : endnote.getParagraphs())
{
newPara.appendChild(new Run(doc, para.getText()));
}
// 将新的段落添加到文档末尾
body.appendChild(newPara);
}
// 保存修改后的文档
doc.save("src/test/resources/docx/result.docx");
as you can see from the test.docx , last three pages
so how should i adjust my code to keep the reference number unchanged ? also keep the cross references still work. (I mean when I click them, it would trace back to the postion in the content
@Madecho In your code you are moving endnotes content into the main document body. Is this your goal? Could you please create the expected output in MS Word and attach it here for our reference? We will check it and provide you more information.
@alexey.noskov yes, that’s my goal : moving endnotes content into the main document body and keep references and the reference number unchanged
here is the result result.docx (288.2 KB)
@Madecho I am afraid there is no direct way to achieve this. In your attached result references to footnotes does not work too. The only way to achieve this is using bookmarks and hyperlinks. For example see the following code:
Document doc = new Document("C:\\Temp\\in.docx");
// Update footnote markers.
doc.updateActualReferenceMarks();
DocumentBuilder builder = new DocumentBuilder(doc);
int bkIndex = 0;
for (Footnote note : (Iterable<Footnote>)doc.getChildNodes(NodeType.FOOTNOTE, true))
{
// Insert bookmark at the footnote.
// Note: bookmarks, which names starts from underscore are hidden in MS Word.
String bkName = "_footnote" + bkIndex;
bkIndex++;
note.getParentNode().insertBefore(new BookmarkStart(doc, bkName), note);
note.getParentNode().insertAfter(new BookmarkEnd(doc, bkName), note);
for (Node child : (Iterable<Node>)note.getChildNodes(NodeType.ANY, false))
{
Node newNode = child.deepClone(true);
doc.getLastSection().getBody().appendChild(newNode);
// Replace footnote ref special char with hyperlink to the bookmark.
if (newNode.isComposite())
{
CompositeNode newComposite = (CompositeNode)newNode;
for (SpecialChar sc : (Iterable<SpecialChar>)newComposite.getChildNodes(NodeType.SPECIAL_CHAR, true))
{
// This is a footnote character
if (sc.getText().equals("\u0002"))
{
builder.moveTo(sc);
builder.insertHyperlink(note.getActualReferenceMark(), bkName, true);
sc.remove();
}
}
}
}
}
doc.save("C:\\Temp\\out.docx");
thank you alexey, but there’s one more thing. After I attach all the endnotes into the body. Can I delete all the endnotes ? Will the hyperlinks still work in the body ? is that achievable ?
@alexey.noskov here comes another problem. after i using the code. the references still work , it can locate to the position , however, the number is missing ? I thought the number would still remained.
Can I keep the number while deleting all the endnotes?
here is the result, after adding “doc.getChildNodes(NodeType.FOOTNOTE, true).clear();” result.docx (284.8 KB)