com.aspose.words
Class MergeImageFieldEventArgs

java.lang.Object
  extended by MergeFieldEventArgsBase
      extended by com.aspose.words.MergeImageFieldEventArgs

public class MergeImageFieldEventArgs 
extends MergeFieldEventArgsBase

Provides data for the MergeImageFieldEventHandler event.

This event occurs during mail merge when an image mail merge field is encountered in the document. You can respond to this event to return a file name, stream, or an java.awt.image.BufferedImage object to the mail merge engine so it is inserted into the document.

There are three properties avaialable ImageFileName, ImageStream and Image to specify where the image must be taken from. Set only one of these properties.

To insert an image mail merge field into a document in Word, select Insert/Field command, then select MergeField and type Image:MyFieldName.

Example:

Shows how to insert images stored in a database BLOB field into a report.
public void MailMergeImageFromBlob() throws Exception
{
    Document doc = new Document(getMyDir() + "MailMerge.MergeImage.doc");

    // Set up the event handler for image fields and perform mail merge.
    doc.getMailMerge().addMergeImageFieldEventHandler(new HandleMergeImageFieldFromBlob());

    // Open a DSN-less connection.
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    String connString = "jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb)};" +
        "DBQ=" + getDatabaseDir() + "Northwind.mdb;UID=Admin";
    Connection conn = DriverManager.getConnection(connString);

    // Load a data table.
    Statement statement = conn.createStatement();
    ResultSet employeesTable = statement.executeQuery("SELECT * FROM Employees");

    // Execute mail merge.
    doc.getMailMerge().executeWithRegions("Employees", employeesTable);

    // Close the database.
    conn.close();

    doc.save(getMyDir() + "MailMerge.MergeImage Out.doc");
}

/// <summary>
/// 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.
/// </summary>
private class HandleMergeImageFieldFromBlob implements MergeImageFieldEventHandler
{
    public void mergeImageField(Object sender, MergeImageFieldEventArgs e)
    {
        // The field value is a byte array, just cast it and create a stream on it.
        InputStream imageStream = new ByteArrayInputStream((byte[])e.getFieldValue());
        // Now the mail merge engine will retrieve the image from the stream.
        e.setImageStream(imageStream);
    }
}
See Also:
MergeImageFieldEventHandler

Property Getters/Setters Summary
DocumentgetDocument()→ inherited from MergeFieldEventArgsBase
           Returns the Document object for which the mail merge is performed.
java.lang.StringgetDocumentFieldName()→ inherited from MergeFieldEventArgsBase
           Gets the name of the merge field as specified in the document.
FieldgetField()→ inherited from MergeFieldEventArgsBase
           Gets the object that represents the current merge field.
java.lang.StringgetFieldName()→ inherited from MergeFieldEventArgsBase
           Gets the name of the merge field in the data source.
java.lang.ObjectgetFieldValue()→ inherited from MergeFieldEventArgsBase
           Gets the value of the field from the data source.
java.awt.image.BufferedImagegetImage()
voidsetImage(java.awt.image.BufferedImage value)
           Specifies the image that the mail merge engine must insert into the document.
java.lang.StringgetImageFileName()
voidsetImageFileName(java.lang.String value)
           Sets the file name of the image that the mail merge engine must insert into the document.
java.io.InputStreamgetImageStream()
voidsetImageStream(java.io.InputStream value)
           Specifies the stream for the mail merge engine to read an image from.
intgetRecordIndex()→ inherited from MergeFieldEventArgsBase
           Gets the zero based index of the record that is being merged.
java.lang.StringgetTableName()→ inherited from MergeFieldEventArgsBase
           Gets the name of the data table for the current merge operation or empty string if the name is not available.
 

Property Getters/Setters Detail

getImageFileName/setImageFileName

public java.lang.String getImageFileName() / public void setImageFileName(java.lang.String value)
Sets the file name of the image that the mail merge engine must insert into the document.

getImageStream/setImageStream

public java.io.InputStream getImageStream() / public void setImageStream(java.io.InputStream value)
Specifies the stream for the mail merge engine to read an image from.

Aspose.Words closes this stream after it merges the image into the document.

Example:

Shows how to insert images stored in a database BLOB field into a report.
public void MailMergeImageFromBlob() throws Exception
{
    Document doc = new Document(getMyDir() + "MailMerge.MergeImage.doc");

    // Set up the event handler for image fields and perform mail merge.
    doc.getMailMerge().addMergeImageFieldEventHandler(new HandleMergeImageFieldFromBlob());

    // Open a DSN-less connection.
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    String connString = "jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb)};" +
        "DBQ=" + getDatabaseDir() + "Northwind.mdb;UID=Admin";
    Connection conn = DriverManager.getConnection(connString);

    // Load a data table.
    Statement statement = conn.createStatement();
    ResultSet employeesTable = statement.executeQuery("SELECT * FROM Employees");

    // Execute mail merge.
    doc.getMailMerge().executeWithRegions("Employees", employeesTable);

    // Close the database.
    conn.close();

    doc.save(getMyDir() + "MailMerge.MergeImage Out.doc");
}

/// <summary>
/// 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.
/// </summary>
private class HandleMergeImageFieldFromBlob implements MergeImageFieldEventHandler
{
    public void mergeImageField(Object sender, MergeImageFieldEventArgs e)
    {
        // The field value is a byte array, just cast it and create a stream on it.
        InputStream imageStream = new ByteArrayInputStream((byte[])e.getFieldValue());
        // Now the mail merge engine will retrieve the image from the stream.
        e.setImageStream(imageStream);
    }
}

getImage/setImage

public java.awt.image.BufferedImage getImage() / public void setImage(java.awt.image.BufferedImage value)
Specifies the image that the mail merge engine must insert into the document.

getDocument

→ inherited from MergeFieldEventArgsBase
public Document getDocument()
Returns the Document object for which the mail merge is performed.

Example:

Shows how to mail merge HTML data into a document.
// File 'MailMerge.InsertHtml.doc' has merge field named 'htmlField1' in it.
// File 'MailMerge.HtmlData.html' contains some valid Html data.
// The same approach can be used when merging HTML data from database.
public void MergeHtml() throws Exception
{
    Document doc = new Document(getMyDir() + "MailMerge.InsertHtml.doc");

    // Add a hadler for the MergeField event.
    doc.getMailMerge().addMergeFieldEventHandler(new HandleMergeFieldInsertHtml());

    // Load some Html from file.
    FileReader fileReader = new FileReader(getMyDir() + "MailMerge.HtmlData.html");
    BufferedReader buffReader = new BufferedReader(fileReader);
    String temp = "";
    String htmltext = "";
    while ((temp = buffReader.readLine()) != null)
        htmltext += temp;
    buffReader.close();

    // Execute mail merge.
    doc.getMailMerge().execute(new String[] {"htmlField1"}, new String[] {htmltext});

    // Save resulting document with a new name.
    doc.save(getMyDir() + "MailMerge.InsertHtml Out.doc");
}

/// <summary>
/// This is called when merge field is actually merged with data in the document.
/// </summary>
private class HandleMergeFieldInsertHtml implements MergeFieldEventHandler
{
    public void mergeField(Object sender, MergeFieldEventArgs e) throws Exception
    {
        // All merge fields that expect HTML data should be marked with some prefix, e.g. 'html'.
        if (e.getDocumentFieldName().startsWith("html"))
        {
            // Insert the text for this merge field as HTML data, using DocumentBuilder.
            DocumentBuilder builder = new DocumentBuilder(e.getDocument());
            builder.moveToMergeField(e.getDocumentFieldName());
            builder.insertHtml((String)e.getFieldValue());

            // The HTML text itself should not be inserted.
            // We have already inserted it as an HTML.
            e.setText("");
        }
    }
}

getTableName

→ inherited from MergeFieldEventArgsBase
public java.lang.String getTableName()
Gets the name of the data table for the current merge operation or empty string if the name is not available.

getRecordIndex

→ inherited from MergeFieldEventArgsBase
public int getRecordIndex()
Gets the zero based index of the record that is being merged.

getFieldName

→ inherited from MergeFieldEventArgsBase
public java.lang.String getFieldName()
Gets the name of the merge field in the data source.

If you have a mapping from a document field name to a different data source field name, then this is the mapped field name.

If you specified a field name prefix, for example "Image:MyFieldName" in the document, then FieldName returns field name without the prefix, that is "MyFieldName".


getDocumentFieldName

→ inherited from MergeFieldEventArgsBase
public java.lang.String getDocumentFieldName()
Gets the name of the merge field as specified in the document.

If you have a mapping from a document field name to a different data source field name, then this is the original field name as specified in the document.

If you specified a field name prefix, for example "Image:MyFieldName" in the document, then DocumentFieldName returns field name without the prefix, that is "MyFieldName".

Example:

Shows how to mail merge HTML data into a document.
// File 'MailMerge.InsertHtml.doc' has merge field named 'htmlField1' in it.
// File 'MailMerge.HtmlData.html' contains some valid Html data.
// The same approach can be used when merging HTML data from database.
public void MergeHtml() throws Exception
{
    Document doc = new Document(getMyDir() + "MailMerge.InsertHtml.doc");

    // Add a hadler for the MergeField event.
    doc.getMailMerge().addMergeFieldEventHandler(new HandleMergeFieldInsertHtml());

    // Load some Html from file.
    FileReader fileReader = new FileReader(getMyDir() + "MailMerge.HtmlData.html");
    BufferedReader buffReader = new BufferedReader(fileReader);
    String temp = "";
    String htmltext = "";
    while ((temp = buffReader.readLine()) != null)
        htmltext += temp;
    buffReader.close();

    // Execute mail merge.
    doc.getMailMerge().execute(new String[] {"htmlField1"}, new String[] {htmltext});

    // Save resulting document with a new name.
    doc.save(getMyDir() + "MailMerge.InsertHtml Out.doc");
}

/// <summary>
/// This is called when merge field is actually merged with data in the document.
/// </summary>
private class HandleMergeFieldInsertHtml implements MergeFieldEventHandler
{
    public void mergeField(Object sender, MergeFieldEventArgs e) throws Exception
    {
        // All merge fields that expect HTML data should be marked with some prefix, e.g. 'html'.
        if (e.getDocumentFieldName().startsWith("html"))
        {
            // Insert the text for this merge field as HTML data, using DocumentBuilder.
            DocumentBuilder builder = new DocumentBuilder(e.getDocument());
            builder.moveToMergeField(e.getDocumentFieldName());
            builder.insertHtml((String)e.getFieldValue());

            // The HTML text itself should not be inserted.
            // We have already inserted it as an HTML.
            e.setText("");
        }
    }
}

getFieldValue

→ inherited from MergeFieldEventArgsBase
public java.lang.Object getFieldValue()
Gets the value of the field from the data source. This property contains a value that has just been selected from your data source for this field by the mail merge engine.

Example:

Shows how to mail merge HTML data into a document.
// File 'MailMerge.InsertHtml.doc' has merge field named 'htmlField1' in it.
// File 'MailMerge.HtmlData.html' contains some valid Html data.
// The same approach can be used when merging HTML data from database.
public void MergeHtml() throws Exception
{
    Document doc = new Document(getMyDir() + "MailMerge.InsertHtml.doc");

    // Add a hadler for the MergeField event.
    doc.getMailMerge().addMergeFieldEventHandler(new HandleMergeFieldInsertHtml());

    // Load some Html from file.
    FileReader fileReader = new FileReader(getMyDir() + "MailMerge.HtmlData.html");
    BufferedReader buffReader = new BufferedReader(fileReader);
    String temp = "";
    String htmltext = "";
    while ((temp = buffReader.readLine()) != null)
        htmltext += temp;
    buffReader.close();

    // Execute mail merge.
    doc.getMailMerge().execute(new String[] {"htmlField1"}, new String[] {htmltext});

    // Save resulting document with a new name.
    doc.save(getMyDir() + "MailMerge.InsertHtml Out.doc");
}

/// <summary>
/// This is called when merge field is actually merged with data in the document.
/// </summary>
private class HandleMergeFieldInsertHtml implements MergeFieldEventHandler
{
    public void mergeField(Object sender, MergeFieldEventArgs e) throws Exception
    {
        // All merge fields that expect HTML data should be marked with some prefix, e.g. 'html'.
        if (e.getDocumentFieldName().startsWith("html"))
        {
            // Insert the text for this merge field as HTML data, using DocumentBuilder.
            DocumentBuilder builder = new DocumentBuilder(e.getDocument());
            builder.moveToMergeField(e.getDocumentFieldName());
            builder.insertHtml((String)e.getFieldValue());

            // The HTML text itself should not be inserted.
            // We have already inserted it as an HTML.
            e.setText("");
        }
    }
}

getField

→ inherited from MergeFieldEventArgsBase
public Field getField()
Gets the object that represents the current merge field.

See Also:
          Aspose.Words Documentation - the home page for the Aspose.Words Product Documentation.
          Aspose.Words Support Forum - our preferred method of support.