|
java.lang.Object
com.aspose.words.MailMerge
public class MailMerge - extends java.lang.Object
Represents the mail merge functionality.
For mail merge operation to work, the document should contain Word MERGEFIELD and
optionally NEXT fields. During mail merge operation, merge fields in the document are
replaced with values from your data source. There are two distinct ways to use mail merge: with mail merge regions and without. The simplest mail merge is without regions and it is very similar to how mail merge
works in Word. Use Execute methods to merge information from some
data source such as java.sql.ResultSet or an array of objects into your document. The
MailMerge object processes all records of the data source and copies and appends
content of the whole document for each record. Note that when MailMerge object encounters a NEXT field, it selects next record
in the data source and continues merging without copying any content. Use ExecuteWithRegions methods to merge information into a
document with mail merge regions defined. You can use
java.sql.ResultSet, array of ResultSets or ResultSetHashMap
as data sources for this operation. You need to use mail merge regions if you want to dynamically grow portions inside the
document. Without mail merge regions whole document will be repeated for every record of
the data source. Example: Executes mail merge from a java.sql.ResultSet.
// Open the template document
Document doc = new Document(getMyDir() + "MailingLabelsDemo.doc");
// 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);
// Get the data.
Statement statement = conn.createStatement();
ResultSet table = statement.executeQuery("SELECT TOP 50 * FROM Customers ORDER BY Country, CompanyName");
// Perform mail merge.
doc.getMailMerge().execute(table);
// Close the database.
conn.close();
doc.save(getMyDir() + "MailMerge.ExecuteResultSet Out.doc"); - See Also:
- Document, Document.MailMerge
|
Property Getters/Setters Detail |
getRemoveEmptyParagraphs/setRemoveEmptyParagraphs | |
public boolean getRemoveEmptyParagraphs() / public void setRemoveEmptyParagraphs(boolean value)
|
-
Specifies whether paragraphs that contained mail merge fields with no
data should be removed from the document.
Example: Shows how to make sure empty paragraphs that result from merging fields with no data are removed from the document.
doc.getMailMerge().setRemoveEmptyParagraphs(true);
getUseNonMergeFields/setUseNonMergeFields | |
public boolean getUseNonMergeFields() / public void setUseNonMergeFields(boolean value)
|
-
When true, specifies that in addition to MERGEFIELD fields, mail merge is performed into some other types of fields.
Normally, mail merge is only performed into MERGEFIELD fields, but several customers had their reporting
built using other fields and had many documents created this way. To simplify migration (and because this
approach was independetly used by several customers) the ability to mail merge into other fields was introduced. When UseNonMergeFields is set to true, mail merge will be performed into the following fields: MERGEFIELD FieldName MACROBUTTON NOMACRO FieldName IF 0 = 0 "{FieldName}" "" Example: Shows how to perform mail merge into merge fields and into additional fields types.
doc.getMailMerge().setUseNonMergeFields(true);
-
Returns a collection that represents mapped data fields for the mail merge operation.
Mapped data fields allow to automatically map between names of fields in your data source
and names of mail merge fields in the document.
Example: Shows how to add a mapping when a merge field in a document and a data field in a data source have different names.
doc.getMailMerge().getMappedDataFields().add("MyFieldName_InDocument", "MyFieldName_InDataSource");
-
Performs a mail merge from a custom data source.
Use this method to fill mail merge fields in the document with values from
any data source such as a list or hashtable or objects. You need to write your
own class that implements the IMailMergeDataSource interface. - Parameters:
dataSource - An object that implements the custom mail merge data source interface.
Example: Performs mail merge from a custom data source.
public void MailMergeCustomDataSource() throws Exception
{
// Create some data that we will use in the mail merge.
CustomerList customers = new CustomerList();
customers.add(new Customer("Thomas Hardy", "120 Hanover Sq., London"));
customers.add(new Customer("Paolo Accorti", "Via Monte Bianco 34, Torino"));
// Open the template document.
Document doc = new Document(getMyDir() + "MailMerge.CustomDataSource.doc");
// To be able to mail merge from your own data source, it must be wrapped
// into an object that implements the IMailMergeDataSource interface.
CustomerMailMergeDataSource customersDataSource = new CustomerMailMergeDataSource(customers);
// Now you can pass your data source into Aspose.Words.
doc.getMailMerge().execute(customersDataSource);
doc.save(getMyDir() + "MailMerge.CustomDataSource Out.doc");
}
/// <summary>
/// An example of a "data entity" class in your application.
/// </summary>
public class Customer
{
public Customer(String fullName, String address)
{
mFullName = fullName;
mAddress = address;
}
public String getFullName()
{
return mFullName;
}
public void setFullName(String value)
{
mFullName = value;
}
public String getAddress()
{
return mAddress;
}
public void setAddress(String value)
{
mAddress = value;
}
private String mFullName;
private String mAddress;
}
/// <summary>
/// An example of a typed collection that contains your "data" objects.
/// </summary>
public class CustomerList extends ArrayList<Customer>
{
public Customer get(int index)
{
return super.get(index);
}
public Customer set(int index, Customer element)
{
return super.set(index, element);
}
}
/// <summary>
/// A custom mail merge data source that you implement to allow Aspose.Words
/// to mail merge data from your Customer objects into Microsoft Word documents.
/// </summary>
public class CustomerMailMergeDataSource imple
|