Free Support Forum - aspose.com

Aspose.Word java - list manipulation

Hi,

I have a section of text in word document that lists optional items (no list markers e.g bullets numbering ). I'd like to use Aspose.words to remove some list items and re -format so there are no blank lines , is it possible to do this ?

Below i've given an example of how the section looks before manipulation and how i'd like the section to be after manipulation.

1. before manipulation

Header

Option 1

Option 2

Option 3

2. after manipulation

Header

Option 2

Option 3

Thanks ,

Hi

Thanks for your request. You ca n try to remove the item from the list using Range.replace() method.

public void ReplaceWithInsertHtml() throws Exception

{

// Open the document.

Document doc = new Document("ReplaceSimple.doc");

doc.getRange().replace(Pattern.compile("Option 1"), new ReplaceWithHtmlEvaluator(), false);

// Save the modified document.

doc.save("SimpleReplaceOut.doc");

}

private class ReplaceWithHtmlEvaluator implements ReplaceEvaluator

{

public int replace(Object sender, ReplaceEvaluatorArgs e) throws Exception

{

e.getMatchNode().getParentNode().remove();

return ReplaceAction.SKIP;

}

}

Also see the following link.
http://www.aspose.com/Products/Aspose.Words/Api/com/aspose/words/Range.html

I hope that this information will help you.

Best regards.

Thanks for the response, I tried this out but the formatting is not corrected as items are removed from the list, blank lines remain as the items are removed (see example below) . I really need a way or re-applying the original format to the modified list.. is there no way of doing this ?

Before Manipulation

Option 1

Option 2

Option 3

After Manipualtion (using suggested solution)

Option 2

Option 3

Regards

Hi

Please attach your word document. I will investigate it and try to help you.

Best regards.

Please find attached a sample document. All list items are optional.

Regards,

Hi

Thanks for additional information. When you remove list item also you should remove the empty paragraph after this item. Try to use the following ReplaceEvaluator code.

private class ReplaceWithHtmlEvaluator implements ReplaceEvaluator

{

public int replace(Object sender, ReplaceEvaluatorArgs e) throws Exception

{

e.getMatchNode().getParentNode().getNextSibling().remove();

e.getMatchNode().getParentNode().remove();

return ReplaceAction.SKIP;

}

}

I hope that it will help you.

Best regards.

Exactly what I was after ! Thanks.