Seems like I messed up a bit since this post was supposed to be an answer to this topic.
Replacing text by a document (Aspose.Words JAVA)
Hi,
Thank you for your answer. We are now to do the replacement. The problem we have is that we have a continuous section break that is actually like a next page section break and we really don’t want that. I attached a generated document for you to see. The text we replaced is the one that is between two section breaks.
And here is the code we use. It’s almost the one you gave us:
private void insertClause(Node node, Document doc)
{
Document dstDoc = node.getDocument();
int index;
Node cell = node.getAncestor(Cell.class);
Node shape = node.getAncestor(Shape.class);
if (cell != null || shape != null) {
// Insertion point is tracked to Cell or Shape level:
// insert appended document on node by node basis.
index = node.getParentNode().getChildNodes().indexOf(node);
Sections sections = doc.getSections();
for (int s=0; s<sections.getCount(); s++) {
Section section = (Section)sections.get(s);
Section insertedSection = null;
try {
insertedSection = (Section) dstDoc.importNode(section, true, ImportFormatMode.KEEP\_SOURCE\_FORMATTING);
for (int n=0; n<insertedSection.getBody().getChildNodes().getCount(); n++) {
Node npt = insertedSection.getBody().getChildNodes().get(n);
// Only Paragraph or Table nodes can be inserted into Cell or Shape
if (node instanceof Paragraph ||
node instanceof Table) {
node.getParentNode().getChildNodes().insert(index++, npt);
}
}
} catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} 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)
{
try
{
DocumentBuilder builder = new DocumentBuilder(dstDoc);
builder.moveTo(node);
builder.insertBreak(BreakType.SECTION\_BREAK\_CONTINUOUS);
dstSection = builder.getCurrentParagraph().getParentSection();
index = dstDoc.getSections().indexOf(dstSection);
int index0 = index;
Sections sections = doc.getSections();
for (int s = 0; s < sections.getCount(); s++)
{
Section section = (Section)sections.get(s);
Section insertedSection = null;
insertedSection = (Section)dstDoc.importNode(section, true, ImportFormatMode.KEEP\_SOURCE\_FORMATTING);
dstDoc.getSections().insert(index++, insertedSection);
}
dstDoc.getSections().get(index0).getPageSetup().setSectionStart(SectionStart.CONTINUOUS);
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
{
try
{
dstSection = (Section)node.getAncestor(Section.class);
index = dstDoc.getSections().indexOf(dstSection);
int index0 = index;
Sections sections = doc.getSections();
for (int s = 0; s < sections.getCount(); s++)
{
Section section = (Section)sections.get(s);
Section insertedSection = null;
insertedSection = (Section)dstDoc.importNode(section, true, ImportFormatMode.KEEP\_SOURCE\_FORMATTING);
dstDoc.getSections().insert(index++, insertedSection);
}
dstDoc.getSections().get(index0).getPageSetup().setSectionStart(SectionStart.CONTINUOUS);
} catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Thank you for your help.