How works ShowHideBookmarkedContent method

I am trying to use ShowHideBookmarkedContent method from https://reference.aspose.com/tutorials/words/net/programming-with-bookmarks/show-hide-bookmarks/, it replaces bookmark content with “true” but when I try to return content I get “Error! Bookmark not defined”.

Document wordDoc = new Document(Environment.CurrentDirectory + "/input.docx");

ShowHideBookmarkedContent(wordDoc, "test", false);
wordDoc.Save(Environment.CurrentDirectory + "/output.docx");

ShowHideBookmarkedContent(wordDoc, "test", true);
wordDoc.Save(Environment.CurrentDirectory + "/output2.docx");

I expect to see hidden content in output2.docx, but all I see is “Error! Bookmark not defined”. I am attaching all the files.
input.zip (57.8 KB)

@asposeuuups The provided code does not work in both directions. The idea is to wrap the bookmark’s content into IF field and depending on the condition show the content or hide. There is a mistake in the code, and the produced IF field is invalid. You can actually simplify the code like this:

public void ShowHideBookmarkedContent(Document doc, string bookmarkName, bool showHide)
{
    Bookmark bm = doc.Range.Bookmarks[bookmarkName];

    DocumentBuilder builder = new DocumentBuilder(doc);
    // Move document builder cursor befor the bookmark.
    builder.MoveToBookmark(bookmarkName, true, false);

    Field field = builder.InsertField("IF ", null);
    builder.MoveTo(field.Separator);
    builder.Write(showHide ? "0 = 0" : "0 = 1" + " \"");
    // Move field separator and end after the bookmark.
    bm.BookmarkEnd.ParentNode.InsertAfter(field.End, bm.BookmarkEnd);
    bm.BookmarkEnd.ParentNode.InsertAfter(field.Separator, bm.BookmarkEnd);
    // Move custor after the bookmakr and finish building the field code.
    builder.MoveToBookmark(bookmarkName, false, true);
    builder.Write("\" \"\"");

    field.Update();
}

In the output document you can press Alt+F9 to see the field codes. It will look like this:
{IF 0 = 0 "bookmark content" ""} or {IF 0 = 1 "bookmark content" ""}