Bookmark Merge Field Values during Mail Merge & Remove Table Cells in Word Document Conditionally using Java

We are using Aspose.Total.Java, I have attached our word document with all the merge fields.

Inside EMERGING PLAN REQUIRED REVENUE SUBJECT TO WAIVER* section I have different merge fields in each rows and cells.
if merge field (EPRR-AB-JHRR) value is null or 0.0 I want to delete the cell in the row
similarly
if merge field (EPRRABMVE) value is null or 0.0 I want to delete the cell in the row

Next:
PDFGenerationTemplate.zip (34.8 KB)

if merge field (EPRR-DB-JHRR) value is null or 0.0 I want to delete the row and it’s header section (DOLLAR-BASED (as a monthly per participant fee))

while deleting left section rows and cells the Waiver Information section which is on the right side should be untouched.

please let me know if you need any additional information.

@ganeshtech,

Please try using the following code:

Document doc = new Document("E:\\PDFGenerationTemplate\\PDFGenerationTemplate.doc");

doc.getMailMerge().setFieldMergingCallback(new HandleMergeField());
doc.getMailMerge().execute(
        new String[] { "EPRR-AB-JHRR", "EPRRABMVE" },
        new Object[] { "0.0", "0.0" });

Bookmark bm = doc.getRange().getBookmarks().get("EPRR-AB-JHRR");

String str = bm.getText();
if (str.equals("0.0")){
    System.out.println("Here you go");
    Cell cell = (Cell) bm.getBookmarkStart().getAncestor(NodeType.CELL);
    cell.removeAllChildren();
    cell.ensureMinimum();
}

doc.save("E:\\PDFGenerationTemplate\\awjava-19.4.doc");

private static class HandleMergeField implements IFieldMergingCallback {
    public void fieldMerging(FieldMergingArgs args) throws Exception {
        if (args.getFieldName().equals("EPRR-AB-JHRR") ||
                args.getFieldName().equals("EPRRABMVE")) {
            DocumentBuilder builder = new DocumentBuilder(args.getDocument());
            builder.moveToMergeField(args.getFieldName());
            builder.startBookmark(args.getFieldName());
            builder.write(args.getFieldValue().toString());
            builder.endBookmark(args.getFieldName());
        }
    }

    public void imageFieldMerging(ImageFieldMergingArgs args) {

    }
}

Hope, this helps.

@awais.hafeez
Thank you for your solution,

I tried as you suggested after setting the bookmark for both the fields.

Bookmark bm1 = doc.getRange().getBookmarks().get(“EPRRABJHRR”);

String str = bm1.getText();
if (str.equals(“0.0%”) || str.equals(“0.00%”)){
System.out.println(“Here you go”);
Cell cell = (Cell) bm1.getBookmarkStart().getAncestor(NodeType.CELL);
cell.removeAllChildren();
cell.ensureMinimum();
}

Bookmark bm2 = doc.getRange().getBookmarks().get(“EPRRABMVE”);

String str2 = bm1.getText();
if (str2.equals(“0.00%”)){
System.out.println(“Here you go MVE”);
Cell cell = (Cell) bm2.getBookmarkStart().getAncestor(NodeType.CELL);
cell.removeAllChildren();
cell.ensureMinimum();
}

Inside FieldMerge Callback function

if (e.getFieldName().equals(“EPRRABJHRR”)
||
e.getFieldName().equals(“EPRRABMVE”))
{
DocumentBuilder builder = new DocumentBuilder(e.getDocument());
builder.moveToMergeField(e.getFieldName());
builder.startBookmark(e.getFieldName());
builder.write(e.getFieldValue().toString());
builder.endBookmark(e.getFieldName());
}


it just removed both merge fields EPRRABJHRR & EPRRABMVE , but the spaces need to removed / deleted. Also as in this case if both these fields not showing in the document need to delete the header “ASSET-BASED” column as well.

So only “DOLLAR BASED” SECTION should only appear.WordandPDFTemplates.zip (127.1 KB)

Attached the word document and the resulted PDF document.

@ganeshtech,

You can build logic on the following code to get the desired output. Also, please refer to the following article:
Aspose.Words Document Object Model

Document doc = new Document("E:\\WordandPDFTemplates\\PDFGenerationTemplate.doc");

doc.getMailMerge().setFieldMergingCallback(new HandleMergeField());
doc.getMailMerge().execute(
        new String[] { "EPRRABJHRR", "EPRRABMVE" },
        new Object[] { "0.0", "0.0" });

Bookmark bm = doc.getRange().getBookmarks().get("EPRRABJHRR");
Bookmark bm1 = doc.getRange().getBookmarks().get("EPRRABMVE");

String str = bm.getText();
String str1 = bm1.getText();

if (str.equals("0.0") && str1.equals("0.0")){
    Cell cell = (Cell) bm.getBookmarkStart().getAncestor(NodeType.CELL);
    cell.removeAllChildren();

    Cell cell1 = (Cell) bm1.getBookmarkStart().getAncestor(NodeType.CELL);
    cell1.removeAllChildren();

    ((Row)cell.getParentRow().getPreviousSibling()).getFirstCell().removeAllChildren();
    ((Row)cell.getParentRow().getPreviousSibling()).getFirstCell().ensureMinimum();
}

doc.save("E:\\WordandPDFTemplates\\awjava-19.4.doc"); 

private static class HandleMergeField implements IFieldMergingCallback {
    public void fieldMerging(FieldMergingArgs args) throws Exception {
        if (args.getFieldName().equals("EPRRABJHRR") ||
                args.getFieldName().equals("EPRRABMVE")) {
            DocumentBuilder builder = new DocumentBuilder(args.getDocument());
            builder.moveToMergeField(args.getFieldName());
            builder.startBookmark(args.getFieldName());
            builder.write(args.getFieldValue().toString());
            builder.endBookmark(args.getFieldName());
        }
    }
    public void imageFieldMerging(ImageFieldMergingArgs args) {

    }
}