Insert image with mail merge engine using Java

«Image:mPhoto»
While trying to insert a image i get the image merge field as well. Can any one tell me why does Image:mPhoto appear? It should be replaces by the image right?
I am using the following code.

CPRiskCADataSource CADataSource = new CPRiskCADataSource(CAList);
doc.getMailMerge().setFieldMergingCallback(new HandleMergeImageFieldFromBlob());
doc.getMailMerge().executeWithRegions(CADataSource);

private class HandleMergeImageFieldFromBlob implements IFieldMergingCallback
{

    public void fieldMerging(FieldMergingArgs args) throws Exception
    {
        // Do nothing.
    }

    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);
    }
}

public class CPRiskCADataSource implements IMailMergeDataSource {
    
    private RiskCAList mRiskCAList;
    private int mRecordIndex;
    
    public CPRiskCADataSource(RiskCAList analysisList) {
        mRiskCAList = analysisList;
        // When the data source is initialized, it must be positioned before the first record.
        mRecordIndex = -1;
    }
    
    ///
    
    /// The name of the data source. Used by Aspose.Words only when executing mail merge with repeatable regions.
    ///
    
    public String getTableName() {
        return "riskCA";
    }
    
    ///
    /// Aspose.Words call this to get a value for every data field.
    ///
    public boolean getValue(String fieldName, Object[] fieldValue) {
        if ("fileName".equals(fieldName)) {
            fieldValue[0] = mRiskCAList.get(mRecordIndex).getFileName();
            return true;
        } else if ("mPhoto".equals(fieldName)) {
            fieldValue[0] = mRiskCAList.get(mRecordIndex).getAttach();
            return true;
        } else {
            // A field with this name was not found,
            // return false to the Aspose.Words mail merge engine.
            fieldValue[0] = null;
            return false;
        }
    }
    
    ///
    /// A standard implementation for moving to a next record in a collection.
    ///
    public boolean moveNext()
    {
        if (isEof())
            return false;
        
        mRecordIndex++;
        
        return (!isEof());
    }
    
    private boolean isEof()
    {
        return (mRecordIndex>= mRiskCAList.size());
    }
    
    @Override
    public IMailMergeDataSource getChildDataSource(String arg0)
            throws Exception
    {
        // TODO Auto-generated method stub
        return null;
    }
}

The way i have it in the document is as follows

«TableStart:riskCA»
«fileName»
«Image:mPhoto»
«MERGEFIELD TableEnd:riskCostAnalysis \* MERGEFORMAT »«TableEnd:riskCA»

Hi Kedar,

Thanks for your query. The reported issue had already been logged in our issue tracking system and will be fixed in next release of Aspose.Words for Java. For the time being, please use the following workaround. Please read insertImage methods of DocumentBuilder class.

public void imageFieldMerging(ImageFieldMergingArgs args) throws Exception
{
    if (args.getFieldName().equals("TestImage"))
    {
        DocumentBuilder builder = new DocumentBuilder(args.getDocument());
        // Move to the Image merge filed
        builder.moveToMergeField("TestImage");
        // Read image and use [insertImage ](http://www.aspose.com/documentation/java-components/aspose.words-for-java/com/aspose/words/documentbuilder.html#insertImage%28byte%5B%5D%29)method of [DocumentBuilder ](https://reference.aspose.com/words/java/com.aspose.words/DocumentBuilder)class
        // If you are getting data from database, you can write code to read image file here
        ResultSet rs = statement.executeQuery();
        // Get as a BLOB
        
        Blob aBlob = rs.getBlob(1);
        
        byte[] allBytesInBlob = aBlob.getBytes(1, (int) aBlob.length());
        // Insert image
        builder.insertImage(allBytesInBlob);
    }
}

Hi Tahir,

I am suing this workaround in some places but if i have a requirement to insert image in a table i.e. one image in each row how would i go about handling that without using data source?

Regards,
Kedar

Something like this…

<Table Start>
1) Mr. John Smith <<Image>>
123 Anytown USA
Email
phone
Background

- 
Text from the Background field goes here 
Notes
-         
Text from the notes field goes here 

2)  Mr. Karen Smith<<Image>>

1 Minikola USA
Email
phone 
Background

-         
Text from the Background field goes here
Notes
-         
Text from the notes field goes here 
<TableEnd>

Hi
Thanks for your request. The issue you reported is already resolved. The fix will be included into the next version of Aspose.Words that comes out in few days. So I would suggest you simply to wait for the fix.
Best regards,

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

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