Free Support Forum - aspose.com

Removing list items depending on code conditions

Hi,

I have the following list in a Word template:

- Personal information:<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

- 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?

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

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

doc.Save(@"Test032\out.doc");

}

ReplaceAction Replace032(object sender, ReplaceEvaluatorArgs e)

{

if ((e.MatchNode.ParentNode as Paragraph).IsListItem)

{

//Remove list item (paragraph)

e.MatchNode.ParentNode.Remove();

}

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?

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

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

addresBookmark.BookmarkStart.ParentNode.Remove();

//Save output document

doc.Save("out.doc")

Hope this helps.

Best regards.