We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Removing list items depending on code conditions

I have the following list in a Word template:
- Personal information:
- Name, DOB etc.
- Contact details
- Addresses
- Contacts (Emergency / Next of Kin etc.)
- Club Details (if applicable)
- Passport / Travel
- Alumni subscription
I want to remove one of the items (e.g. “Addresses”) under certain conditions (in code). How can I do that?

Thanks for your inquiry. You can just remove paragraph that represents this item. For example you can try using ReplaceEvaluator. See the following code snippet and the attached documents.

public void Test032()
    // Open document
    Document doc = new Document(@"Test032\in.doc");
    Regex regex = new Regex("Address");
    doc.Range.Replace(regex, new ReplaceEvaluator(Replace032), false);
    // Save output document
ReplaceAction Replace032(object sender, ReplaceEvaluatorArgs e)
    if ((e.MatchNode.ParentNode as Paragraph).IsListItem)
        // Remove list item (paragraph)
    return ReplaceAction.Skip;

I hope this could help you.
Best regards.

Thanks for your help.
What if (at a later date) I want to change the word in the template without changing the code? e.g. I decide to write “Location” instead of “Address”.
How can I (somehow) “flag” the paragraph in the template that it’s the one I want to remove without depending on what’s written in the paragraph… but the “flag” will have to be invisible (for the case where I don’t remove the paragraph)? Is that possible?

Thanks for your inquiry. You can use bookmark as flag. Just insert bookmark into paragraph (list item). See the following code.

Document doc = new Document("in.doc");
// Get bookmark
Bookmark addresBookmark = doc.Range.Bookmarks["address"];
// Remove parent paregraph
// Save output document

Hope this helps.
Best regards.