|
java.lang.Object
MergeFieldEventArgsBase
com.aspose.words.MergeFieldEventArgs
public class MergeFieldEventArgs - extends MergeFieldEventArgsBase
Provides data for the MergeField event.
The MergeField event occurs during mail merge when a simple mail merge
field is encountered in the document. You can respond to this event to return
text for the mail merge engine to insert into the document. 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("");
}
}
} - See Also:
- MergeFieldEventHandler
|
Property Getters/Setters Detail |
getText/setText | |
public java.lang.String getText() / public void setText(java.lang.String value)
|
-
Gets or sets the text that will be inserted into the document for the current merge field.
When your event handler is called, this property is set to null. If you leave Text as null, the mail merge engine will insert FieldValue in place of the merge field. If you set Text to a non empty string, the string will be inserted into the document in place of the merge field. If you set Text to an empty string, the mail merge engine will remove the merge field. 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("");
}
}
}
-
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("");
}
}
}
-
Gets the name of the data table for the current merge operation or empty string if the name is not available.
-
Gets the zero based index of the record that is being merged.
-
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".
-
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("");
}
}
}
-
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 'htmlF
|