Hi,
I’m trying to swap two ranged sdts (the first with id -688534414 and the second with id 1766038418) without lose content on the attached file. I still can’t find an appropriate code solution. Can you help me?
Thanks in advance.
file_toswap.docx (189,8 KB)
@ernestocastellano It looks like you have already asked this question in another your thread:
https://forum.aspose.com/t/moving-ranged-sdt-before-another-ranged-sdt/295938
Also, you can consider using the approach described here to extract content of ranged SDT into a separate document and then use DocumentBuilder.insertDocument
method to insert it at the desired position in the document.
Hi @alexey.noskov ,
It may seem the same thing but It’s a little bit different. What I’m trying to do is to get the ranged sdt with id -688534414 move it to the position of the ranged sdt with id 1766038418 and viceversa (1766038418 in the previous position of -688534414) on the same document. I hope I was clear.
Thanks.
Ernesto.
@ernestocastellano You can use the similar approach:
Document doc = new Document("C:\\Temp\\in.docx");
// Get structured document tags.
IStructuredDocumentTag source = null;
IStructuredDocumentTag target = null;
for (IStructuredDocumentTag tag : doc.getRange().getStructuredDocumentTags())
{
if (tag.getId() == -653446606)
source = tag;
if (tag.getId() == 1766038418)
target = tag;
}
// Get child nodes of the source SDT.
Node[] sourceChildren = source.getChildNodes(NodeType.ANY, false).toArray();
Node[] targetChildren = target.getChildNodes(NodeType.ANY, false).toArray();
// Move SDT start and end
StructuredDocumentTagRangeStart srcStart = (StructuredDocumentTagRangeStart)source.getNode();
StructuredDocumentTagRangeEnd srcEnd = srcStart.getRangeEnd();
StructuredDocumentTagRangeStart targetStart = (StructuredDocumentTagRangeStart)target.getNode();
StructuredDocumentTagRangeEnd targetEnd = targetStart.getRangeEnd();
targetStart.getParentNode().insertBefore(srcStart, targetStart);
srcEnd.getParentNode().insertAfter(targetStart, srcEnd);
srcStart.getParentNode().insertAfter(srcEnd, srcStart);
targetStart.getParentNode().insertAfter(targetEnd, targetStart);
// Move child nodes.
for (Node node : sourceChildren)
srcEnd.getParentNode().insertBefore(node, srcEnd);
// Move child nodes.
for (Node node : targetChildren)
targetEnd.getParentNode().insertBefore(node, targetEnd);
doc.save("C:\\Temp\\out.docx");
Hi @alexey.noskov,
the solution given seems to work very good but I notice that the original Stds (with id -653446606 and 1766038418) are no longer StructuredDocumentTagRangeStart but StructuredDocumentTag. Could you tell me why?
Thanks.
@ernestocastellano This occurs because after moving content section break is removed, so the SDT is no longer a ranged SDT and is represented as a simple StructuredDocumentTag
node.
1 Like