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

Free Support Forum - aspose.com

Deleting a line or row from my word

Hi Team,


I am looking for a customized way to handle my report, here is my requirement.

My document has many merge fields, tables (tables will have 5 or 6 rows). I want to delete a row or entire table depends on some data conditions.

Currently we are using docx4j with customized merge fields, where my requirement will be satisfied, if I set the mergefield value to “DELETE”, the entire line/row will be deleted.

So I am looking for a similar kind of stuff in ASPOSE with Java. If I pass a value (say “Delete”) to my merge field, I want to delete entire line/row in my final report.

can anyone assist me on this?

Hi Sundarrajan,


Thanks for contacting support.

As per my understanding from above description, the issue appears to be related to Word documents. Can you please share the input Document, so that we can further look into this requirement.
Hi Sundarrajan,

Thanks for your inquiry. Please use the following code example to achieve your requirement. Hope this helps you.

Document doc = new Document(MyDir + "input.docx");

// Add a handler for the MergeField event.
doc.getMailMerge().setFieldMergingCallback(new HandleMergeDeleteRow());

// Your code to execute mail merge.

for (Bookmark bookmark : doc.getRange().getBookmarks())
{
if (bookmark.getName().startsWith("delete_"))
{
Row row = (Row)bookmark.getBookmarkStart().getAncestor(NodeType.ROW);
if (row != null)
row.remove();

//Do the same if you want to remove the Table node.
}
}
doc.save(MyDir + "MailMerge Out.doc");
------------------------------------------------------------------------------
class HandleMergeDeleteRow implements IFieldMergingCallback
{
DocumentBuilder builder;
int bm = 1;
public void fieldMerging(FieldMergingArgs args) throws Exception
{
if (builder == null)
builder = new DocumentBuilder(args.getDocument());

if (args.getFieldValue().toString().trim() == "DELETE")
{
builder.moveToMergeField(args.getFieldName());
builder.startBookmark("delete_" + bm);
builder.write("DELETE");
builder.endBookmark("delete_" + bm);
}
}

public void imageFieldMerging(ImageFieldMergingArgs imageFieldMergingArgs) throws Exception
{
// Do nothing
}
}