java.lang.ObjectFieldMergingArgsBase
com.aspose.words.ImageFieldMergingArgs
public class ImageFieldMergingArgs
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 There are three properties available To insert an image mail merge field into a document in Word, select Insert/Field command,
then select MergeField and type Image:MyFieldName. Example:
public void mailMergeImageFromBlob() throws Exception {
Document doc = new Document(getMyDir() + "MailMerge.MergeImage.doc");
// Set up the event handler for image fields.
doc.getMailMerge().setFieldMergingCallback(new HandleMergeImageFieldFromBlob());
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // Loads the driver
// Open the database connection.
String connString = "jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb)};" +
"DBQ=" + getDatabaseDir() + "Northwind.mdb" + ";UID=Admin";
// DSN-less DB connection.
java.sql.Connection conn = java.sql.DriverManager.getConnection(connString);
// Create and execute a command.
java.sql.Statement statement = conn.createStatement();
java.sql.ResultSet resultSet = statement.executeQuery("SELECT * FROM Employees");
com.aspose.words.DataTable table = new com.aspose.words.DataTable(resultSet, "Employees");
// Perform mail merge.
doc.getMailMerge().executeWithRegions(table);
// Close the database.
conn.close();
doc.save(getMyDir() + "MailMerge.MergeImage Out.doc");
}
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);
}
}
Property Getters/Setters Summary | ||
---|---|---|
Document | getDocument() | → inherited from FieldMergingArgsBase |
Returns the |
||
java.lang.String | getDocumentFieldName() | → inherited from FieldMergingArgsBase |
Gets the name of the merge field as specified in the document. | ||
Field | getField() | → inherited from FieldMergingArgsBase |
Gets the object that represents the current merge field. | ||
java.lang.String | getFieldName() | → inherited from FieldMergingArgsBase |
Gets the name of the merge field in the data source. | ||
java.lang.Object | getFieldValue() | → inherited from FieldMergingArgsBase |
Gets the value of the field from the data source. | ||
java.awt.image.BufferedImage | getImage() | |
void | setImage(java.awt.image.BufferedImage value) | |
Specifies the image that the mail merge engine must insert into the document. | ||
java.lang.String | getImageFileName() | |
void | setImageFileName(java.lang.String value) | |
Sets the file name of the image that the mail merge engine must insert into the document. | ||
MergeFieldImageDimension | getImageHeight() | |
void | setImageHeight(MergeFieldImageDimension value) | |
Specifies the image height for the image to insert into the document. | ||
java.io.InputStream | getImageStream() | |
void | setImageStream(java.io.InputStream value) | |
Specifies the stream for the mail merge engine to read an image from. | ||
MergeFieldImageDimension | getImageWidth() | |
void | setImageWidth(MergeFieldImageDimension value) | |
Specifies the image width for the image to insert into the document. | ||
int | getRecordIndex() | → inherited from FieldMergingArgsBase |
Gets the zero based index of the record that is being merged. | ||
java.lang.String | getTableName() | → inherited from FieldMergingArgsBase |
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 |
---|
getDocument | → inherited from FieldMergingArgsBase |
public Document getDocument() |
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 mailMergeInsertHtml() throws Exception { Document doc = new Document(getMyDir() + "MailMerge.InsertHtml.doc"); // Add a handler for the MergeField event. doc.getMailMerge().setFieldMergingCallback(new HandleMergeFieldInsertHtml()); // Load some Html from file. StringBuilder htmlText = new StringBuilder(); BufferedReader reader = new BufferedReader(new FileReader(getMyDir() + "MailMerge.HtmlData.html")); String line; while ((line = reader.readLine()) != null) { htmlText.append(line); htmlText.append("\r\n"); } // Execute mail merge. doc.getMailMerge().execute(new String[]{"htmlField1"}, new String[]{htmlText.toString()}); // Save resulting document with a new name. doc.save(getMyDir() + "MailMerge.InsertHtml Out.doc"); } private class HandleMergeFieldInsertHtml implements IFieldMergingCallback { /** * This is called when merge field is actually merged with data in the document. */ public void fieldMerging(FieldMergingArgs 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(""); } } public void imageFieldMerging(ImageFieldMergingArgs e) throws Exception { // Do nothing. } }
getDocumentFieldName | → inherited from FieldMergingArgsBase |
public java.lang.String getDocumentFieldName() |
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 mailMergeInsertHtml() throws Exception { Document doc = new Document(getMyDir() + "MailMerge.InsertHtml.doc"); // Add a handler for the MergeField event. doc.getMailMerge().setFieldMergingCallback(new HandleMergeFieldInsertHtml()); // Load some Html from file. StringBuilder htmlText = new StringBuilder(); BufferedReader reader = new BufferedReader(new FileReader(getMyDir() + "MailMerge.HtmlData.html")); String line; while ((line = reader.readLine()) != null) { htmlText.append(line); htmlText.append("\r\n"); } // Execute mail merge. doc.getMailMerge().execute(new String[]{"htmlField1"}, new String[]{htmlText.toString()}); // Save resulting document with a new name. doc.save(getMyDir() + "MailMerge.InsertHtml Out.doc"); } private class HandleMergeFieldInsertHtml implements IFieldMergingCallback { /** * This is called when merge field is actually merged with data in the document. */ public void fieldMerging(FieldMergingArgs 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(""); } } public void imageFieldMerging(ImageFieldMergingArgs e) throws Exception { // Do nothing. } }
getField | → inherited from FieldMergingArgsBase |
public Field getField() |
getFieldName | → inherited from FieldMergingArgsBase |
public java.lang.String getFieldName() |
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".
Example:
Shows how to insert checkbox form fields into a document during mail merge.// File 'MailMerge.InsertCheckBox.doc' is a template // containing the table with the following fields in it: // <<TableStart:StudentCourse>> <<CourseName>> <<TableEnd:StudentCourse>>. public void mailMergeInsertCheckBox() throws Exception { Document doc = new Document(getMyDir() + "MailMerge.InsertCheckBox.doc"); // Add a handler for the MergeField event. doc.getMailMerge().setFieldMergingCallback(new HandleMergeFieldInsertCheckBox()); // Execute mail merge with regions. com.aspose.words.DataTable dataTable = getStudentCourseDataTable(); doc.getMailMerge().executeWithRegions(dataTable); // Save resulting document with a new name. doc.save(getMyDir() + "MailMerge.InsertCheckBox Out.doc"); } private class HandleMergeFieldInsertCheckBox implements IFieldMergingCallback { /** * This is called for each merge field in the document * when Document.MailMerge.ExecuteWithRegions is called. */ public void fieldMerging(FieldMergingArgs e) throws Exception { if (e.getDocumentFieldName().equals("CourseName")) { // Insert the checkbox for this merge field, using DocumentBuilder. DocumentBuilder builder = new DocumentBuilder(e.getDocument()); builder.moveToMergeField(e.getFieldName()); builder.insertCheckBox(e.getDocumentFieldName() + Integer.toString(mCheckBoxCount), false, 0); builder.write((String) e.getFieldValue()); mCheckBoxCount++; } } public void imageFieldMerging(ImageFieldMergingArgs args) throws Exception { // Do nothing. } /** * Counter for CheckBox name generation */ private int mCheckBoxCount; } /** * Create DataTable and fill it with data. * In real life this DataTable should be filled from a database. */ private static com.aspose.words.DataTable getStudentCourseDataTable() throws Exception { java.sql.ResultSet resultSet = createCachedRowSet(new String[]{"CourseName"}); for (int i = 0; i < 10; i++) addRow(resultSet, new String[]{"Course " + Integer.toString(i)}); return new com.aspose.words.DataTable(resultSet, "StudentCourse"); }
getFieldValue | → inherited from FieldMergingArgsBase |
public java.lang.Object getFieldValue() |
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 mailMergeInsertHtml() throws Exception { Document doc = new Document(getMyDir() + "MailMerge.InsertHtml.doc"); // Add a handler for the MergeField event. doc.getMailMerge().setFieldMergingCallback(new HandleMergeFieldInsertHtml()); // Load some Html from file. StringBuilder htmlText = new StringBuilder(); BufferedReader reader = new BufferedReader(new FileReader(getMyDir() + "MailMerge.HtmlData.html")); String line; while ((line = reader.readLine()) != null) { htmlText.append(line); htmlText.append("\r\n"); } // Execute mail merge. doc.getMailMerge().execute(new String[]{"htmlField1"}, new String[]{htmlText.toString()}); // Save resulting document with a new name. doc.save(getMyDir() + "MailMerge.InsertHtml Out.doc"); } private class HandleMergeFieldInsertHtml implements IFieldMergingCallback { /** * This is called when merge field is actually merged with data in the document. */ public void fieldMerging(FieldMergingArgs 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(""); } } public void imageFieldMerging(ImageFieldMergingArgs e) throws Exception { // Do nothing. } }
getImage/setImage | |
public java.awt.image.BufferedImage getImage() / public void setImage(java.awt.image.BufferedImage value) |
getImageFileName/setImageFileName | |
public java.lang.String getImageFileName() / public void setImageFileName(java.lang.String value) |
getImageHeight/setImageHeight | |
public MergeFieldImageDimension getImageHeight() / public void setImageHeight(MergeFieldImageDimension value) |
The value of this property initially comes from the corresponding MERGEFIELD's code, contained in the
template document. To override the initial value, you should assign an instance of
To indicate that the original value of the image height should be applied, you should assign the null
value to this property or set the
getImageStream/setImageStream | |
public java.io.InputStream getImageStream() / public void setImageStream(java.io.InputStream value) |
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. doc.getMailMerge().setFieldMergingCallback(new HandleMergeImageFieldFromBlob()); Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // Loads the driver // Open the database connection. String connString = "jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb)};" + "DBQ=" + getDatabaseDir() + "Northwind.mdb" + ";UID=Admin"; // DSN-less DB connection. java.sql.Connection conn = java.sql.DriverManager.getConnection(connString); // Create and execute a command. java.sql.Statement statement = conn.createStatement(); java.sql.ResultSet resultSet = statement.executeQuery("SELECT * FROM Employees"); com.aspose.words.DataTable table = new com.aspose.words.DataTable(resultSet, "Employees"); // Perform mail merge. doc.getMailMerge().executeWithRegions(table); // Close the database. conn.close(); doc.save(getMyDir() + "MailMerge.MergeImage Out.doc"); } 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); } }
getImageWidth/setImageWidth | |
public MergeFieldImageDimension getImageWidth() / public void setImageWidth(MergeFieldImageDimension value) |
The value of this property initially comes from the corresponding MERGEFIELD's code, contained in the
template document. To override the initial value, you should assign an instance of
To indicate that the original value of the image width should be applied, you should assign the null
value to this property or set the
getRecordIndex | → inherited from FieldMergingArgsBase |
public int getRecordIndex() |
getTableName | → inherited from FieldMergingArgsBase |
public java.lang.String getTableName() |
Example:
Shows how to define custom logic in a handler implementing IFieldMergingCallback that is executed for unmerged regions in the document.public static class EmptyRegionsHandler implements IFieldMergingCallback { /** * Called for each field belonging to an unmerged region in the document. */ public void fieldMerging(FieldMergingArgs args) throws Exception { // Change the text of each field of the ContactDetails region individually. if ("ContactDetails".equals(args.getTableName())) { // Set the text of the field based off the field name. if ("Name".equals(args.getFieldName())) args.setText("(No details found)"); else if ("Number".equals(args.getFieldName())) args.setText("(N/A)"); } // Remove the entire table of the Suppliers region. Also check if the previous paragraph // before the table is a heading paragraph and if so remove that too. if ("Suppliers".equals(args.getTableName())) { Table table = (Table)args.getField().getStart().getAncestor(NodeType.TABLE); // Check if the table has been removed from the document already. if (table.getParentNode() != null) { // Try to find the paragraph which precedes the table before the table is removed from the document. if (table.getPreviousSibling() != null && table.getPreviousSibling().getNodeType() == NodeType.PARAGRAPH) { Paragraph previousPara = (Paragraph)table.getPreviousSibling(); if (isHeadingParagraph(previousPara)) previousPara.remove(); } table.remove(); } } } /** * Returns true if the paragraph uses any Heading style e.g Heading 1 to Heading 9 */ private boolean isHeadingParagraph(Paragraph para) throws Exception { return (para.getParagraphFormat().getStyleIdentifier() >= StyleIdentifier.HEADING_1 && para.getParagraphFormat().getStyleIdentifier() <= StyleIdentifier.HEADING_9); } public void imageFieldMerging(ImageFieldMergingArgs args) throws Exception { // Do Nothing } }