Hello,
I want to replace a bookmark by the entire content of a document.
I use this code finded on this forum, it’s coded in C# at the base but i have simply converted it to Java without logic modification :
----
Node node = documentBuilder.getCurrentNode();
Section insertedSection = null;
int index = 0;
if (node.getAncestor(Cell.class) != null || node.getAncestor(Shape.class) != null) {
// Insertion point is tracked to Cell or Shape level:
// - insert appended document on node by node basis.
index = node.getParentNode().getChildNodes().indexOf(node);
for (int j = 0; j < nestedDocument.getSections().getCount(); j++) {
Section section = nestedDocument.getSections().get(j);
for (Node sourceNode : section.getBody().getChildNodes()) {
// Only Paragraph or Table nodes can be inserted
// into Cell or Shape
if ((sourceNode instanceof Paragraph) || (sourceNode instanceof Table)) {
// Do not insert node if it is a last empty
// paragarph in the section.
if ((sourceNode instanceof Paragraph)
&& sourceNode == section.getBody().getLastChild())
break;
Node insertedNode = document.importNode(sourceNode, true,
ImportFormatMode.KEEP_SOURCE_FORMATTING);
node.getParentNode().getChildNodes().insert(index++, insertedNode);
}
}
}
} else {
// Insertion point is tracked to Section.Body level:
// - insert appended document on section by section
// basis.
Section dstSection = null;
Body body = (Body) node.getAncestor(Body.class);
if (body.getLastChild() != node) {
DocumentBuilder builder = new DocumentBuilder(document);
builder.moveTo(node);
builder.insertBreak(BreakType.SECTION_BREAK_CONTINUOUS);
dstSection = builder.getCurrentParagraph().getParentSection();
} else {
dstSection = (Section) node.getAncestor(Section.class);
}
index = document.getSections().indexOf(dstSection);
int index0 = index;
for (int k = 0; k < nestedDocument.getSections().getCount(); k++) {
Section section = nestedDocument.getSections().get(k);
insertedSection = (Section) document.importNode(section, true,
ImportFormatMode.KEEP_SOURCE_FORMATTING);
// Uncomment this line if you want to get rid of
// headers/footers in the inserted section
insertedSection.clearHeadersFooters();
document.getSections().insert(index++, insertedSection);
}
document.getSections().get(index0).getPageSetup().setSectionStart(SectionStart.CONTINUOUS);
}
// Delete the bookmark
document.getRange().getBookmarks().get(bookmarkReplacementSetting.getName()).remove();
----
In the above code :
- nestedDocument is the Document object that i want to import
- document is the current document that will receive the content of the nestedDocument variable
- bookmarkReplacementSetting.getName() return the name of the bookmark to replace
When is check in debug mode the sections of the nestedDocument variable are imported in the document variable BUT when i save the target document to a file and i open it i didn’t see the imported content and the bookmark it still present…
Note : When i display the content of document.getText() the content of the document is present.
Thanks in advance.
Dominique Righetto