Create DataTable with empty or no result set

I need to create a DataTable to establish a recurring mail merge. I am using Java and i am going to fill in the DataTable without accessing a database.

I see two constructors, one is empty and one has a result set and the Table Name. I need to set the table name to get the recurring mail merge to read the TableStart and Table end tags.

So - I either need a way to passi in an empty result set or is there a way to set the table name? I do not see a SetTableName() method in the object code. There is a getTableName() method, no setTableName() method.

Ideas?

Hi John,

Thanks for your inquiry. Yes, com.aspose.words.DataTable have not any method to set table’s name. I have logged this feature request as WORDSJAVA-781 in our issue tracking system. You will be notified via this forum thread once this feature is available. We apologize for your inconvenience.

Please use the following code snippet to achieve your requirement. Hope this helps you. Please let us know if you have any more queries.

Document doc = new Document(MyDir + "MailMergeTest.docx");
java.sql.ResultSet table = createCachedRowSet(new String[] { "field1", "field2" });
addRow(table, new String[] { "Value 1", "Value 2" });
addRow(table, new String[] { "Value 1", "Value 2" });
addRow(table, new String[] { "Value 1", "Value 2" });
com.aspose.words.DataTable dt = new com.aspose.words.DataTable(table, "Test");
doc.getMailMerge().executeWithRegions(dt);
doc.save(MyDir + "Out.docx");
private static ResultSet createCachedRowSet(String[] columnNames) throws Exception {

    RowSetMetaDataImpl metaData = new RowSetMetaDataImpl();
    metaData.setColumnCount(columnNames.length);
    for (int i = 0; i < columnNames.length; i++)
    {
        metaData.setColumnName(i + 1, columnNames[i]);
        metaData.setColumnType(i + 1, java.sql.Types.VARCHAR);
    }
    CachedRowSetImpl rowSet = new CachedRowSetImpl();
    rowSet.setMetaData(metaData);
    return rowSet;
}

private static void addRow(ResultSet resultSet, String[] values) throws Exception {
    resultSet.moveToInsertRow();
    for (int i = 0; i < values.length; i++)
    {
        resultSet.updateString(i + 1, values[i]);
    }
    resultSet.insertRow();
    resultSet.moveToCurrentRow();
    resultSet.last();
}

Ok - I see where you are going with that source code. One thing that I need to do is have an <Image:XXX> tag in the mail merge region. I can get the image into the document using doc.insertImage(byte[]) but I really need to use regions to get my dataset into the document.

I have also been successful using imageFieldMerging() method - but I saved that byte[] as a static variable and used that in imageFieldMerging().

Assuming I can gett he image into the resultset, how do i go about accessing the imabe (byte[]) in imageFieldMerging() ?

Is there a phone number I can reach you? It would be easier to talk about.

Hi John,

Thanks for your inquiry.

john.nelson:

I can get the image into the document using doc.insertImage(byte[]) but I really need to use regions to get my dataset into the document.

You can get data source in the class to which you have implemented the IFieldMergingCallback interface. Please check the highlighted code below. Hope this helps you. If this does not help you, please share some more detail about your query. We will then provide you more information on this along with code.

private class ResultSetTest implements IFieldMergingCallback {

    java.sql.ResultSet resultset;
    ResultSetTest(java.sql.ResultSet mailMergeDataSource)
    {
        resultset = mailMergeDataSource;
    }

    public void fieldMerging(FieldMergingArgs e) throws Exception {
        if (e.getFieldName().equals("Field1")) {
            resultset.first();
            System.out.println(resultset.getString("field1"));
        }
    }

    public void imageFieldMerging(ImageFieldMergingArgs args) throws Exception {
        // Do nothing.
    }
}
Document doc = new Document(MyDir + "MailMergeTest.docx");
java.sql.ResultSet table = createCachedRowSet(new String[] { "field1", "field2" });
addRow(table, new String[] { "Value 1", "Value 2" });
addRow(table, new String[] { "Value 1", "Value 2" });
addRow(table, new String[] { "Value 1", "Value 2" });
com.aspose.words.DataTable dt = new com.aspose.words.DataTable(table, "Test");
ResultSetTest callback = new ResultSetTest(table);
doc.getMailMerge().setFieldMergingCallback(callback);
doc.getMailMerge().executeWithRegions(dt);
doc.save(MyDir + "Out.docx");

john.nelson:

I have also been successful using imageFieldMerging() method - but I saved that byte[] as a static variable and used that in imageFieldMerging().

It would be great if you please share some more detail about this query.

john.nelson:

Assuming I can gett he image into the resultset, how do i go about accessing the imabe (byte[]) in imageFieldMerging() ?

You can get/set image in imageFieldMerging. Please check getImage/setImage and getImageStream/setImageStream method of ImageFieldMergingArgs class. Please check the members of ImageFieldMergingArgs class from here:
https://reference.aspose.com/words/java/com.aspose.words/FieldMergingArgs

private class HandleMergeImageFieldFromBlob implements IFieldMergingCallback {
    public void fieldMerging(FieldMergingArgs args) throws Exception {
        // Do nothing.
    }

    /**
     * This is called when mail merge engine encounters Image:XXX merge field in the document.
     * You have a chance to return an Image object, file name or a stream that contains the image.
     */
    public void imageFieldMerging(ImageFieldMergingArgs e) throws Exception {
        // The field value is a byte array, just cast it and create a stream on it.
        ByteArrayInputStream imageStream = new ByteArrayInputStream((byte[]) e.getFieldValue());
        // Now the mail merge engine will retrieve the image from the stream.
        e.setImageStream(imageStream);
    }
}

john.nelson:

Is there a phone number I can reach you? It would be easier to talk about.

We only provide technical support through forums and live chat. It means that we do not provide technical support through telephone right now.

The issues you have found earlier (filed as WORDSJAVA-781) have been fixed in this .NET update and this Java update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.