Free Support Forum - aspose.com

Conditional mail merge with executeWithRegions

Hi,

I have an Table with 3 columns. In the first column there is a merge field with only text. In the second column there is a merge field that can have only “0” or “1” in my template in the third column I have got a static text.

After the table I have got an other static text!

So on mail merge (executeWithRegions) I need to do something like that.
If in column 2 there is the value “0” I will delete the content from column 3 if the value is “1” I will let the static text there.

And then at least, if the Table is completly empty the ResultSet is empty) I will delete the table (I use setCleanupOptions) and let the static text at the bottom of the document. It the table is not empty so there are records I will delete the static text at the bottom!

DataTable schedaNumero3DataTable = new DataTable((ResultSet) stmt.getObject(6), “Table”);

doc.getMailMerge().executeWithRegions (schedaNumero3DataTable);


So how can I do this?? Are there any “callback” or something like that?

Se attachet template!

Thx
Michael

Hi Michael,


Thanks for your inquiry. We’re checking with this scenario and will get back to you soon.

Best regards,

Hi Michael,


Thanks for your patience.

The best way to achieve the requirement you mentioned in first part of your query is to implement IFieldMergingCallback Interface. You can first get a reference to Cell node in fieldMerging event and then remove the static content from it (in your case the static content consists of two Run nodes in First Paragraph). Please try running the following code snippet:
private static class HandleMergeFields implements IFieldMergingCallback {
public void fieldMerging(FieldMergingArgs e) throws Exception {
if (e.getFieldName().equals(“value_column_2”)) {
if (e.getFieldValue().equals(“0”)) {
Cell cell = (Cell) e.getField().getStart().getAncestor(NodeType.CELL);
Cell nextCell = (Cell) cell.getNextSibling();
            nextCell<font color="BLUE"><b>.</b></font>getFirstParagraph<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>.</b></font>getChildNodes<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>.</b></font>removeAt<font color="BLUE"><b>(</b></font><font color="BROWN">1</font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
            nextCell<font color="BLUE"><b>.</b></font>getFirstParagraph<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>.</b></font>getChildNodes<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>.</b></font>removeAt<font color="BLUE"><b>(</b></font><font color="BROWN">0</font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
        <font color="BLUE"><b>}</b></font>
    <font color="BLUE"><b>}</b></font>
<font color="BLUE"><b>}</b></font>

<font color="RED"><b>public</b></font> <font color="RED"><b>void</b></font> imageFieldMerging<font color="BLUE"><b>(</b></font>ImageFieldMergingArgs args<font color="BLUE"><b>)</b></font> <font color="RED"><b>throws</b></font> Exception <font color="BLUE"><b>{</b></font>
    <font color="GREEN"><i>// Do nothing.

}
}

public static void main(String[] args) throws Exception {
URI exeDir = Program.class.getResource("").toURI();
String dataDir = new File(exeDir.resolve("…/…/Data")) + File.separator;

createConnection<font color="BLUE"><b>(</b></font>dataDir<font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>

DataTable table <font color="BLUE">=</font> <font color="RED"><b>new</b></font> DataTable<font color="BLUE"><b>(</b></font>executeQuery<font color="BLUE"><b>(</b></font><font color="PURPLE">"SELECT * from [Table]"</font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>,</b></font> <font color="PURPLE">"Table"</font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>

Document doc <font color="BLUE">=</font> <font color="RED"><b>new</b></font> Document<font color="BLUE"><b>(</b></font>dataDir <font color="BLUE">+</font> <font color="PURPLE">"Template.doc"</font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
doc<font color="BLUE"><b>.</b></font>getMailMerge<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>.</b></font>setFieldMergingCallback<font color="BLUE"><b>(</b></font><font color="RED"><b>new</b></font> HandleMergeFields<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
doc<font color="BLUE"><b>.</b></font>getMailMerge<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>.</b></font>executeWithRegions<font color="BLUE"><b>(</b></font>table<font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>

doc<font color="BLUE"><b>.</b></font>save<font color="BLUE"><b>(</b></font>dataDir <font color="BLUE">+</font> <font color="PURPLE">"Out.doc"</font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>

}

To achieve second part of your requirement, please use the following code snippet:
public static void main(String[] args) throws Exception {
URI exeDir = Program.class.getResource("").toURI();
String dataDir = new File(exeDir.resolve("…/…/Data")) + File.separator;
createConnection<font color="BLUE"><b>(</b></font>dataDir<font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>

DataTable table <font color="BLUE">=</font> <font color="RED"><b>new</b></font> DataTable<font color="BLUE"><b>(</b></font>executeQuery<font color="BLUE"><b>(</b></font><font color="PURPLE">"SELECT * from [Table]"</font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>,</b></font> <font color="PURPLE">"Table"</font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>

Document doc <font color="BLUE">=</font> <font color="RED"><b>new</b></font> Document<font color="BLUE"><b>(</b></font>dataDir <font color="BLUE">+</font> <font color="PURPLE">"Template.doc"</font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>

DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToMergeField(“value_column_2”, false, false);
Table tab = (Table) builder.getCurrentNode().getAncestor(NodeType.TABLE);

doc.getMailMerge().setCleanupOptions(MailMergeCleanupOptions.REMOVE_UNUSED_REGIONS);
doc.getMailMerge().setFieldMergingCallback(new HandleMergeFields());
doc.getMailMerge().executeWithRegions(table);

if (tab.getRows().getCount() == 1)
tab.getNextSibling().getNextSibling().remove();

doc.save(dataDir + “Out.doc”);
}


I hope, this helps. Please let me know if I can be of any further assistance.

Best regards,

Hi, I was able to do all with your help!!!

Thx
Michael