How to add/find/replace/delete hidden node?

Hello team,
I want to wrap a existing text with hidden texts. Could you tell me how to do it?

For example,
“<<[tag1]>>” will be replaced by “#####<<[tag1]>>#####” and “#####” is hidden.

Then, after some other operations, “#####” will be deleted.

@Doraemon You can achieve this using find/replace operation and further processing of the document. For example see the following code:

Document doc = new Document("C:\\Temp\\in.docx");

String tag = "<<[tag1]>>";
// Replace tag with itself to make it to be represented as a single run node.
FindReplaceOptions opt = new FindReplaceOptions();
opt.setUseSubstitutions(true);
doc.getRange().replace(Pattern.compile(tag), "$0", opt);

// Loop through the Run nodes in the document and wrap the tag with hidden markers.
for (Run r : (Iterable<Run>)doc.getChildNodes(NodeType.RUN, true))
{
    if (r.getText().equals(tag))
    {
        Run marker = (Run)r.deepClone(true);
        marker.setText("#####");
        marker.getFont().setHidden(true);
        r.getParentNode().insertBefore(marker, r);
        r.getParentNode().insertAfter(marker.deepClone(true), r);
    }
}

doc.save("C:\\Temp\\out.docx");

@alexey.noskov
Think you for your reply. That’s exactly what i want.

1 Like