Free Support Forum - aspose.com

Enable Disable Bookmarked Content in Word documents using Java | Merge Field inside IF Field to Show Hide Bookmark's Content

We have a document where we want to make the bookmarked content visible/hidden in certain scenarios. I see there are methods to access the bookmarks but I dont see a method to show/hide.
Is there any way to achieve this with aspose-words-19.12-jdk17.jar ?
thank you

@mp2,

I think, you can encapsulate the entire Bookmark (including the bookmarked content) within the True part of IF field such that the IF field contains a nested Merge Field in the expression (Left of Operator) and depending upon the value of Merge Field, the IF field shows or hides the content of Bookmark in Word Document. Please try using the following sample Java code example:

Document doc = new Document("E:\\temp\\show hide bookmarked content.docx");
showHideBookmarkedContent(doc,"bm",false);
doc.save("E:\\temp\\awjava-20.1.docx");

public static void showHideBookmarkedContent(Document doc, String bookmarkName, boolean showHide) throws Exception {
    DocumentBuilder builder = new DocumentBuilder(doc);
    Bookmark bm = doc.getRange().getBookmarks().get(bookmarkName);

    builder.moveToDocumentEnd();
    // {IF "{MERGEFIELD bookmark}" = "true" "" ""}
    Field field = builder.insertField("IF \"", null);
    builder.moveTo(field.getStart().getNextSibling().getNextSibling());
    builder.insertField("MERGEFIELD " + bookmarkName + "", null);
    builder.write("\" = \"true\" ");
    builder.write("\"");
    builder.write("\"");
    builder.write(" \"\"");

    Node currentNode = field.getStart();
    boolean flag = true;
    while (currentNode != null && flag) {
        if (currentNode.getNodeType() == NodeType.RUN)
            if (currentNode.toString(SaveFormat.TEXT).trim().equals("\""))
                flag = false;

        Node nextNode = currentNode.getNextSibling();

        bm.getBookmarkStart().getParentNode().insertBefore(currentNode, bm.getBookmarkStart());
        currentNode = nextNode;
    }

    Node endNode = bm.getBookmarkEnd();
    flag = true;
    while (currentNode != null && flag) {
        if (currentNode.getNodeType() == NodeType.FIELD_END)
            flag = false;

        Node nextNode = currentNode.getNextSibling();

        bm.getBookmarkEnd().getParentNode().insertAfter(currentNode, endNode);
        endNode = currentNode;
        currentNode = nextNode;
    }

    doc.getMailMerge().execute(new String[]{bookmarkName}, new Object[]{showHide});
}

Thank you…this looks like lot of processing and i have a ton of bookmarks to work with. I also tried deleting the bookmarks that I don’t need for each case but its deleting other form elements in the document. Are there any other alternatives?

@mp2,

There should not be any big performance impact because it just inserts an IF field around the Bookmarked content and fills the value of merge field inside IF field’s condition part. If you do not want to perform Mail Merge then you can replace the following line:

doc.getMailMerge().execute(new String[]{bookmarkName}, new Object[]{showHide});

With

builder.moveToMergeField(bookmarkName);
builder.write(showHide ? "true" : "false");

Hope, this helps.

thank you very much. that helps