public class MappedDataFieldCollection
This is implemented as a collection of string keys into string values. The keys are the names of mail merge fields in the document and the values are the names of fields in your data source.
Example:
Shows how to map data columns and MERGEFIELDs with different names so the data is transferred between them during a mail merge.public void mappedDataFieldCollection() throws Exception { // Create a document and table that we will merge Document doc = createSourceDocMappedDataFields(); DataTable dataTable = createSourceTableMappedDataFields(); // We have a column "Column2" in the data table that doesn't have a respective MERGEFIELD in the document // Also, we have a MERGEFIELD named "Column3" that does not exist as a column in the data source // If data from "Column2" is suitable for the "Column3" MERGEFIELD, // we can map that column name to the MERGEFIELD in the "MappedDataFields" key/value pair MappedDataFieldCollection mappedDataFields = doc.getMailMerge().getMappedDataFields(); // A data source column name is linked to a MERGEFIELD name by adding an element like this mappedDataFields.add("MergeFieldName", "DataSourceColumnName"); // So, values from "Column2" will now go into MERGEFIELDs named "Column3" as well as "Column2", if there are any mappedDataFields.add("Column3", "Column2"); // The MERGEFIELD name is the "key" to the respective data source column name "value" Assert.assertEquals(mappedDataFields.get("MergeFieldName"), "DataSourceColumnName"); Assert.assertTrue(mappedDataFields.containsKey("MergeFieldName")); Assert.assertTrue(mappedDataFields.containsValue("DataSourceColumnName")); // Now if we run this mail merge, the "Column3" MERGEFIELDs will take data from "Column2" of the table doc.getMailMerge().execute(dataTable); // We can count and iterate over the mapped columns/fields Assert.assertEquals(mappedDataFields.getCount(), 2); Iterator<Map.Entry<String, String>> enumerator = mappedDataFields.iterator(); try { while (enumerator.hasNext()) { Map.Entry<String, String> dataField = enumerator.next(); System.out.println(MessageFormat.format("Column named {0} is mapped to MERGEFIELDs named {1}", dataField.getValue(), dataField.getKey())); } } finally { if (enumerator != null) enumerator.remove(); } // We can also remove some or all of the elements mappedDataFields.remove("MergeFieldName"); Assert.assertFalse(mappedDataFields.containsKey("MergeFieldName")); Assert.assertFalse(mappedDataFields.containsValue("DataSourceColumnName")); mappedDataFields.clear(); Assert.assertEquals(mappedDataFields.getCount(), 0); // Removing the mapped key/value pairs has no effect on the document because the merge was already done with them in place doc.save(getArtifactsDir() + "MailMerge.MappedDataFieldCollection.docx"); } /// <summary> /// Create a document with 2 MERGEFIELDs, one of which does not have a corresponding column in the data table. /// </summary> private static Document createSourceDocMappedDataFields() throws Exception { Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Insert two MERGEFIELDs that will accept data from that table builder.insertField(" MERGEFIELD Column1"); builder.write(", "); builder.insertField(" MERGEFIELD Column3"); return doc; } /// <summary> /// Create a data table with 2 columns, one of which does not have a corresponding MERGEFIELD in our source document. /// </summary> private static DataTable createSourceTableMappedDataFields() { // Create a data table that will be used in a mail merge DataTable dataTable = new DataTable("MyTable"); dataTable.getColumns().add("Column1"); dataTable.getColumns().add("Column2"); dataTable.getRows().add(new Object[]{"Value1", "Value2"}); return dataTable; }
Property Getters/Setters Summary | ||
---|---|---|
int | getCount() | |
Gets the number of elements contained in the collection.
|
||
java.lang.String | get(java.lang.String documentFieldName) | |
void | set(java.lang.StringdocumentFieldName, java.lang.Stringvalue) | |
Gets or sets the name of the field in the data source associated with the specified mail merge field. |
Method Summary | ||
---|---|---|
void | add(java.lang.String documentFieldName, java.lang.String dataSourceFieldName) | |
Adds a new field mapping.
|
||
void | clear() | |
Removes all elements from the collection.
|
||
boolean | containsKey(java.lang.String documentFieldName) | |
Determines whether a mapping from the specified field in the document exists in the collection.
|
||
boolean | containsValue(java.lang.String dataSourceFieldName) | |
Determines whether a mapping from the specified field in the data source exists in the collection.
|
||
java.util.Iterator<java.util.Map.Entry<java.lang.String, java.lang.String>> | iterator() | |
Returns a dictionary iterator object that can be used to iterate over all items in the collection.
|
||
void | remove(java.lang.String documentFieldName) | |
Removes a field mapping.
|
public int getCount()
Example:
Shows how to map data columns and MERGEFIELDs with different names so the data is transferred between them during a mail merge.public void mappedDataFieldCollection() throws Exception { // Create a document and table that we will merge Document doc = createSourceDocMappedDataFields(); DataTable dataTable = createSourceTableMappedDataFields(); // We have a column "Column2" in the data table that doesn't have a respective MERGEFIELD in the document // Also, we have a MERGEFIELD named "Column3" that does not exist as a column in the data source // If data from "Column2" is suitable for the "Column3" MERGEFIELD, // we can map that column name to the MERGEFIELD in the "MappedDataFields" key/value pair MappedDataFieldCollection mappedDataFields = doc.getMailMerge().getMappedDataFields(); // A data source column name is linked to a MERGEFIELD name by adding an element like this mappedDataFields.add("MergeFieldName", "DataSourceColumnName"); // So, values from "Column2" will now go into MERGEFIELDs named "Column3" as well as "Column2", if there are any mappedDataFields.add("Column3", "Column2"); // The MERGEFIELD name is the "key" to the respective data source column name "value" Assert.assertEquals(mappedDataFields.get("MergeFieldName"), "DataSourceColumnName"); Assert.assertTrue(mappedDataFields.containsKey("MergeFieldName")); Assert.assertTrue(mappedDataFields.containsValue("DataSourceColumnName")); // Now if we run this mail merge, the "Column3" MERGEFIELDs will take data from "Column2" of the table doc.getMailMerge().execute(dataTable); // We can count and iterate over the mapped columns/fields Assert.assertEquals(mappedDataFields.getCount(), 2); Iterator<Map.Entry<String, String>> enumerator = mappedDataFields.iterator(); try { while (enumerator.hasNext()) { Map.Entry<String, String> dataField = enumerator.next(); System.out.println(MessageFormat.format("Column named {0} is mapped to MERGEFIELDs named {1}", dataField.getValue(), dataField.getKey())); } } finally { if (enumerator != null) enumerator.remove(); } // We can also remove some or all of the elements mappedDataFields.remove("MergeFieldName"); Assert.assertFalse(mappedDataFields.containsKey("MergeFieldName")); Assert.assertFalse(mappedDataFields.containsValue("DataSourceColumnName")); mappedDataFields.clear(); Assert.assertEquals(mappedDataFields.getCount(), 0); // Removing the mapped key/value pairs has no effect on the document because the merge was already done with them in place doc.save(getArtifactsDir() + "MailMerge.MappedDataFieldCollection.docx"); } /// <summary> /// Create a document with 2 MERGEFIELDs, one of which does not have a corresponding column in the data table. /// </summary> private static Document createSourceDocMappedDataFields() throws Exception { Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Insert two MERGEFIELDs that will accept data from that table builder.insertField(" MERGEFIELD Column1"); builder.write(", "); builder.insertField(" MERGEFIELD Column3"); return doc; } /// <summary> /// Create a data table with 2 columns, one of which does not have a corresponding MERGEFIELD in our source document. /// </summary> private static DataTable createSourceTableMappedDataFields() { // Create a data table that will be used in a mail merge DataTable dataTable = new DataTable("MyTable"); dataTable.getColumns().add("Column1"); dataTable.getColumns().add("Column2"); dataTable.getRows().add(new Object[]{"Value1", "Value2"}); return dataTable; }
public java.lang.String get(java.lang.String documentFieldName) / public void set(java.lang.String documentFieldName, java.lang.String value)
Example:
Shows how to map data columns and MERGEFIELDs with different names so the data is transferred between them during a mail merge.public void mappedDataFieldCollection() throws Exception { // Create a document and table that we will merge Document doc = createSourceDocMappedDataFields(); DataTable dataTable = createSourceTableMappedDataFields(); // We have a column "Column2" in the data table that doesn't have a respective MERGEFIELD in the document // Also, we have a MERGEFIELD named "Column3" that does not exist as a column in the data source // If data from "Column2" is suitable for the "Column3" MERGEFIELD, // we can map that column name to the MERGEFIELD in the "MappedDataFields" key/value pair MappedDataFieldCollection mappedDataFields = doc.getMailMerge().getMappedDataFields(); // A data source column name is linked to a MERGEFIELD name by adding an element like this mappedDataFields.add("MergeFieldName", "DataSourceColumnName"); // So, values from "Column2" will now go into MERGEFIELDs named "Column3" as well as "Column2", if there are any mappedDataFields.add("Column3", "Column2"); // The MERGEFIELD name is the "key" to the respective data source column name "value" Assert.assertEquals(mappedDataFields.get("MergeFieldName"), "DataSourceColumnName"); Assert.assertTrue(mappedDataFields.containsKey("MergeFieldName")); Assert.assertTrue(mappedDataFields.containsValue("DataSourceColumnName")); // Now if we run this mail merge, the "Column3" MERGEFIELDs will take data from "Column2" of the table doc.getMailMerge().execute(dataTable); // We can count and iterate over the mapped columns/fields Assert.assertEquals(mappedDataFields.getCount(), 2); Iterator<Map.Entry<String, String>> enumerator = mappedDataFields.iterator(); try { while (enumerator.hasNext()) { Map.Entry<String, String> dataField = enumerator.next(); System.out.println(MessageFormat.format("Column named {0} is mapped to MERGEFIELDs named {1}", dataField.getValue(), dataField.getKey())); } } finally { if (enumerator != null) enumerator.remove(); } // We can also remove some or all of the elements mappedDataFields.remove("MergeFieldName"); Assert.assertFalse(mappedDataFields.containsKey("MergeFieldName")); Assert.assertFalse(mappedDataFields.containsValue("DataSourceColumnName")); mappedDataFields.clear(); Assert.assertEquals(mappedDataFields.getCount(), 0); // Removing the mapped key/value pairs has no effect on the document because the merge was already done with them in place doc.save(getArtifactsDir() + "MailMerge.MappedDataFieldCollection.docx"); } /// <summary> /// Create a document with 2 MERGEFIELDs, one of which does not have a corresponding column in the data table. /// </summary> private static Document createSourceDocMappedDataFields() throws Exception { Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Insert two MERGEFIELDs that will accept data from that table builder.insertField(" MERGEFIELD Column1"); builder.write(", "); builder.insertField(" MERGEFIELD Column3"); return doc; } /// <summary> /// Create a data table with 2 columns, one of which does not have a corresponding MERGEFIELD in our source document. /// </summary> private static DataTable createSourceTableMappedDataFields() { // Create a data table that will be used in a mail merge DataTable dataTable = new DataTable("MyTable"); dataTable.getColumns().add("Column1"); dataTable.getColumns().add("Column2"); dataTable.getRows().add(new Object[]{"Value1", "Value2"}); return dataTable; }
public void add(java.lang.String documentFieldName, java.lang.String dataSourceFieldName)
documentFieldName
- Case-sensitive name of the mail merge field in the document.dataSourceFieldName
- Case-sensitive name of the field in the data source.Example:
Shows how to map data columns and MERGEFIELDs with different names so the data is transferred between them during a mail merge.public void mappedDataFieldCollection() throws Exception { // Create a document and table that we will merge Document doc = createSourceDocMappedDataFields(); DataTable dataTable = createSourceTableMappedDataFields(); // We have a column "Column2" in the data table that doesn't have a respective MERGEFIELD in the document // Also, we have a MERGEFIELD named "Column3" that does not exist as a column in the data source // If data from "Column2" is suitable for the "Column3" MERGEFIELD, // we can map that column name to the MERGEFIELD in the "MappedDataFields" key/value pair MappedDataFieldCollection mappedDataFields = doc.getMailMerge().getMappedDataFields(); // A data source column name is linked to a MERGEFIELD name by adding an element like this mappedDataFields.add("MergeFieldName", "DataSourceColumnName"); // So, values from "Column2" will now go into MERGEFIELDs named "Column3" as well as "Column2", if there are any mappedDataFields.add("Column3", "Column2"); // The MERGEFIELD name is the "key" to the respective data source column name "value" Assert.assertEquals(mappedDataFields.get("MergeFieldName"), "DataSourceColumnName"); Assert.assertTrue(mappedDataFields.containsKey("MergeFieldName")); Assert.assertTrue(mappedDataFields.containsValue("DataSourceColumnName")); // Now if we run this mail merge, the "Column3" MERGEFIELDs will take data from "Column2" of the table doc.getMailMerge().execute(dataTable); // We can count and iterate over the mapped columns/fields Assert.assertEquals(mappedDataFields.getCount(), 2); Iterator<Map.Entry<String, String>> enumerator = mappedDataFields.iterator(); try { while (enumerator.hasNext()) { Map.Entry<String, String> dataField = enumerator.next(); System.out.println(MessageFormat.format("Column named {0} is mapped to MERGEFIELDs named {1}", dataField.getValue(), dataField.getKey())); } } finally { if (enumerator != null) enumerator.remove(); } // We can also remove some or all of the elements mappedDataFields.remove("MergeFieldName"); Assert.assertFalse(mappedDataFields.containsKey("MergeFieldName")); Assert.assertFalse(mappedDataFields.containsValue("DataSourceColumnName")); mappedDataFields.clear(); Assert.assertEquals(mappedDataFields.getCount(), 0); // Removing the mapped key/value pairs has no effect on the document because the merge was already done with them in place doc.save(getArtifactsDir() + "MailMerge.MappedDataFieldCollection.docx"); } /// <summary> /// Create a document with 2 MERGEFIELDs, one of which does not have a corresponding column in the data table. /// </summary> private static Document createSourceDocMappedDataFields() throws Exception { Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Insert two MERGEFIELDs that will accept data from that table builder.insertField(" MERGEFIELD Column1"); builder.write(", "); builder.insertField(" MERGEFIELD Column3"); return doc; } /// <summary> /// Create a data table with 2 columns, one of which does not have a corresponding MERGEFIELD in our source document. /// </summary> private static DataTable createSourceTableMappedDataFields() { // Create a data table that will be used in a mail merge DataTable dataTable = new DataTable("MyTable"); dataTable.getColumns().add("Column1"); dataTable.getColumns().add("Column2"); dataTable.getRows().add(new Object[]{"Value1", "Value2"}); return dataTable; }
public void clear()
Example:
Shows how to map data columns and MERGEFIELDs with different names so the data is transferred between them during a mail merge.public void mappedDataFieldCollection() throws Exception { // Create a document and table that we will merge Document doc = createSourceDocMappedDataFields(); DataTable dataTable = createSourceTableMappedDataFields(); // We have a column "Column2" in the data table that doesn't have a respective MERGEFIELD in the document // Also, we have a MERGEFIELD named "Column3" that does not exist as a column in the data source // If data from "Column2" is suitable for the "Column3" MERGEFIELD, // we can map that column name to the MERGEFIELD in the "MappedDataFields" key/value pair MappedDataFieldCollection mappedDataFields = doc.getMailMerge().getMappedDataFields(); // A data source column name is linked to a MERGEFIELD name by adding an element like this mappedDataFields.add("MergeFieldName", "DataSourceColumnName"); // So, values from "Column2" will now go into MERGEFIELDs named "Column3" as well as "Column2", if there are any mappedDataFields.add("Column3", "Column2"); // The MERGEFIELD name is the "key" to the respective data source column name "value" Assert.assertEquals(mappedDataFields.get("MergeFieldName"), "DataSourceColumnName"); Assert.assertTrue(mappedDataFields.containsKey("MergeFieldName")); Assert.assertTrue(mappedDataFields.containsValue("DataSourceColumnName")); // Now if we run this mail merge, the "Column3" MERGEFIELDs will take data from "Column2" of the table doc.getMailMerge().execute(dataTable); // We can count and iterate over the mapped columns/fields Assert.assertEquals(mappedDataFields.getCount(), 2); Iterator<Map.Entry<String, String>> enumerator = mappedDataFields.iterator(); try { while (enumerator.hasNext()) { Map.Entry<String, String> dataField = enumerator.next(); System.out.println(MessageFormat.format("Column named {0} is mapped to MERGEFIELDs named {1}", dataField.getValue(), dataField.getKey())); } } finally { if (enumerator != null) enumerator.remove(); } // We can also remove some or all of the elements mappedDataFields.remove("MergeFieldName"); Assert.assertFalse(mappedDataFields.containsKey("MergeFieldName")); Assert.assertFalse(mappedDataFields.containsValue("DataSourceColumnName")); mappedDataFields.clear(); Assert.assertEquals(mappedDataFields.getCount(), 0); // Removing the mapped key/value pairs has no effect on the document because the merge was already done with them in place doc.save(getArtifactsDir() + "MailMerge.MappedDataFieldCollection.docx"); } /// <summary> /// Create a document with 2 MERGEFIELDs, one of which does not have a corresponding column in the data table. /// </summary> private static Document createSourceDocMappedDataFields() throws Exception { Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Insert two MERGEFIELDs that will accept data from that table builder.insertField(" MERGEFIELD Column1"); builder.write(", "); builder.insertField(" MERGEFIELD Column3"); return doc; } /// <summary> /// Create a data table with 2 columns, one of which does not have a corresponding MERGEFIELD in our source document. /// </summary> private static DataTable createSourceTableMappedDataFields() { // Create a data table that will be used in a mail merge DataTable dataTable = new DataTable("MyTable"); dataTable.getColumns().add("Column1"); dataTable.getColumns().add("Column2"); dataTable.getRows().add(new Object[]{"Value1", "Value2"}); return dataTable; }
public boolean containsKey(java.lang.String documentFieldName)
documentFieldName
- Case-sensitive name of the mail merge field in the document.Example:
Shows how to map data columns and MERGEFIELDs with different names so the data is transferred between them during a mail merge.public void mappedDataFieldCollection() throws Exception { // Create a document and table that we will merge Document doc = createSourceDocMappedDataFields(); DataTable dataTable = createSourceTableMappedDataFields(); // We have a column "Column2" in the data table that doesn't have a respective MERGEFIELD in the document // Also, we have a MERGEFIELD named "Column3" that does not exist as a column in the data source // If data from "Column2" is suitable for the "Column3" MERGEFIELD, // we can map that column name to the MERGEFIELD in the "MappedDataFields" key/value pair MappedDataFieldCollection mappedDataFields = doc.getMailMerge().getMappedDataFields(); // A data source column name is linked to a MERGEFIELD name by adding an element like this mappedDataFields.add("MergeFieldName", "DataSourceColumnName"); // So, values from "Column2" will now go into MERGEFIELDs named "Column3" as well as "Column2", if there are any mappedDataFields.add("Column3", "Column2"); // The MERGEFIELD name is the "key" to the respective data source column name "value" Assert.assertEquals(mappedDataFields.get("MergeFieldName"), "DataSourceColumnName"); Assert.assertTrue(mappedDataFields.containsKey("MergeFieldName")); Assert.assertTrue(mappedDataFields.containsValue("DataSourceColumnName")); // Now if we run this mail merge, the "Column3" MERGEFIELDs will take data from "Column2" of the table doc.getMailMerge().execute(dataTable); // We can count and iterate over the mapped columns/fields Assert.assertEquals(mappedDataFields.getCount(), 2); Iterator<Map.Entry<String, String>> enumerator = mappedDataFields.iterator(); try { while (enumerator.hasNext()) { Map.Entry<String, String> dataField = enumerator.next(); System.out.println(MessageFormat.format("Column named {0} is mapped to MERGEFIELDs named {1}", dataField.getValue(), dataField.getKey())); } } finally { if (enumerator != null) enumerator.remove(); } // We can also remove some or all of the elements mappedDataFields.remove("MergeFieldName"); Assert.assertFalse(mappedDataFields.containsKey("MergeFieldName")); Assert.assertFalse(mappedDataFields.containsValue("DataSourceColumnName")); mappedDataFields.clear(); Assert.assertEquals(mappedDataFields.getCount(), 0); // Removing the mapped key/value pairs has no effect on the document because the merge was already done with them in place doc.save(getArtifactsDir() + "MailMerge.MappedDataFieldCollection.docx"); } /// <summary> /// Create a document with 2 MERGEFIELDs, one of which does not have a corresponding column in the data table. /// </summary> private static Document createSourceDocMappedDataFields() throws Exception { Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Insert two MERGEFIELDs that will accept data from that table builder.insertField(" MERGEFIELD Column1"); builder.write(", "); builder.insertField(" MERGEFIELD Column3"); return doc; } /// <summary> /// Create a data table with 2 columns, one of which does not have a corresponding MERGEFIELD in our source document. /// </summary> private static DataTable createSourceTableMappedDataFields() { // Create a data table that will be used in a mail merge DataTable dataTable = new DataTable("MyTable"); dataTable.getColumns().add("Column1"); dataTable.getColumns().add("Column2"); dataTable.getRows().add(new Object[]{"Value1", "Value2"}); return dataTable; }
public boolean containsValue(java.lang.String dataSourceFieldName)
dataSourceFieldName
- Case-sensitive name of the field in the data source.Example:
Shows how to map data columns and MERGEFIELDs with different names so the data is transferred between them during a mail merge.public void mappedDataFieldCollection() throws Exception { // Create a document and table that we will merge Document doc = createSourceDocMappedDataFields(); DataTable dataTable = createSourceTableMappedDataFields(); // We have a column "Column2" in the data table that doesn't have a respective MERGEFIELD in the document // Also, we have a MERGEFIELD named "Column3" that does not exist as a column in the data source // If data from "Column2" is suitable for the "Column3" MERGEFIELD, // we can map that column name to the MERGEFIELD in the "MappedDataFields" key/value pair MappedDataFieldCollection mappedDataFields = doc.getMailMerge().getMappedDataFields(); // A data source column name is linked to a MERGEFIELD name by adding an element like this mappedDataFields.add("MergeFieldName", "DataSourceColumnName"); // So, values from "Column2" will now go into MERGEFIELDs named "Column3" as well as "Column2", if there are any mappedDataFields.add("Column3", "Column2"); // The MERGEFIELD name is the "key" to the respective data source column name "value" Assert.assertEquals(mappedDataFields.get("MergeFieldName"), "DataSourceColumnName"); Assert.assertTrue(mappedDataFields.containsKey("MergeFieldName")); Assert.assertTrue(mappedDataFields.containsValue("DataSourceColumnName")); // Now if we run this mail merge, the "Column3" MERGEFIELDs will take data from "Column2" of the table doc.getMailMerge().execute(dataTable); // We can count and iterate over the mapped columns/fields Assert.assertEquals(mappedDataFields.getCount(), 2); Iterator<Map.Entry<String, String>> enumerator = mappedDataFields.iterator(); try { while (enumerator.hasNext()) { Map.Entry<String, String> dataField = enumerator.next(); System.out.println(MessageFormat.format("Column named {0} is mapped to MERGEFIELDs named {1}", dataField.getValue(), dataField.getKey())); } } finally { if (enumerator != null) enumerator.remove(); } // We can also remove some or all of the elements mappedDataFields.remove("MergeFieldName"); Assert.assertFalse(mappedDataFields.containsKey("MergeFieldName")); Assert.assertFalse(mappedDataFields.containsValue("DataSourceColumnName")); mappedDataFields.clear(); Assert.assertEquals(mappedDataFields.getCount(), 0); // Removing the mapped key/value pairs has no effect on the document because the merge was already done with them in place doc.save(getArtifactsDir() + "MailMerge.MappedDataFieldCollection.docx"); } /// <summary> /// Create a document with 2 MERGEFIELDs, one of which does not have a corresponding column in the data table. /// </summary> private static Document createSourceDocMappedDataFields() throws Exception { Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Insert two MERGEFIELDs that will accept data from that table builder.insertField(" MERGEFIELD Column1"); builder.write(", "); builder.insertField(" MERGEFIELD Column3"); return doc; } /// <summary> /// Create a data table with 2 columns, one of which does not have a corresponding MERGEFIELD in our source document. /// </summary> private static DataTable createSourceTableMappedDataFields() { // Create a data table that will be used in a mail merge DataTable dataTable = new DataTable("MyTable"); dataTable.getColumns().add("Column1"); dataTable.getColumns().add("Column2"); dataTable.getRows().add(new Object[]{"Value1", "Value2"}); return dataTable; }
public java.util.Iterator<java.util.Map.Entry<java.lang.String, java.lang.String>> iterator()
Example:
Shows how to map data columns and MERGEFIELDs with different names so the data is transferred between them during a mail merge.public void mappedDataFieldCollection() throws Exception { // Create a document and table that we will merge Document doc = createSourceDocMappedDataFields(); DataTable dataTable = createSourceTableMappedDataFields(); // We have a column "Column2" in the data table that doesn't have a respective MERGEFIELD in the document // Also, we have a MERGEFIELD named "Column3" that does not exist as a column in the data source // If data from "Column2" is suitable for the "Column3" MERGEFIELD, // we can map that column name to the MERGEFIELD in the "MappedDataFields" key/value pair MappedDataFieldCollection mappedDataFields = doc.getMailMerge().getMappedDataFields(); // A data source column name is linked to a MERGEFIELD name by adding an element like this mappedDataFields.add("MergeFieldName", "DataSourceColumnName"); // So, values from "Column2" will now go into MERGEFIELDs named "Column3" as well as "Column2", if there are any mappedDataFields.add("Column3", "Column2"); // The MERGEFIELD name is the "key" to the respective data source column name "value" Assert.assertEquals(mappedDataFields.get("MergeFieldName"), "DataSourceColumnName"); Assert.assertTrue(mappedDataFields.containsKey("MergeFieldName")); Assert.assertTrue(mappedDataFields.containsValue("DataSourceColumnName")); // Now if we run this mail merge, the "Column3" MERGEFIELDs will take data from "Column2" of the table doc.getMailMerge().execute(dataTable); // We can count and iterate over the mapped columns/fields Assert.assertEquals(mappedDataFields.getCount(), 2); Iterator<Map.Entry<String, String>> enumerator = mappedDataFields.iterator(); try { while (enumerator.hasNext()) { Map.Entry<String, String> dataField = enumerator.next(); System.out.println(MessageFormat.format("Column named {0} is mapped to MERGEFIELDs named {1}", dataField.getValue(), dataField.getKey())); } } finally { if (enumerator != null) enumerator.remove(); } // We can also remove some or all of the elements mappedDataFields.remove("MergeFieldName"); Assert.assertFalse(mappedDataFields.containsKey("MergeFieldName")); Assert.assertFalse(mappedDataFields.containsValue("DataSourceColumnName")); mappedDataFields.clear(); Assert.assertEquals(mappedDataFields.getCount(), 0); // Removing the mapped key/value pairs has no effect on the document because the merge was already done with them in place doc.save(getArtifactsDir() + "MailMerge.MappedDataFieldCollection.docx"); } /// <summary> /// Create a document with 2 MERGEFIELDs, one of which does not have a corresponding column in the data table. /// </summary> private static Document createSourceDocMappedDataFields() throws Exception { Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Insert two MERGEFIELDs that will accept data from that table builder.insertField(" MERGEFIELD Column1"); builder.write(", "); builder.insertField(" MERGEFIELD Column3"); return doc; } /// <summary> /// Create a data table with 2 columns, one of which does not have a corresponding MERGEFIELD in our source document. /// </summary> private static DataTable createSourceTableMappedDataFields() { // Create a data table that will be used in a mail merge DataTable dataTable = new DataTable("MyTable"); dataTable.getColumns().add("Column1"); dataTable.getColumns().add("Column2"); dataTable.getRows().add(new Object[]{"Value1", "Value2"}); return dataTable; }
public void remove(java.lang.String documentFieldName)
documentFieldName
- Case-sensitive name of the mail merge field in the document.Example:
Shows how to map data columns and MERGEFIELDs with different names so the data is transferred between them during a mail merge.public void mappedDataFieldCollection() throws Exception { // Create a document and table that we will merge Document doc = createSourceDocMappedDataFields(); DataTable dataTable = createSourceTableMappedDataFields(); // We have a column "Column2" in the data table that doesn't have a respective MERGEFIELD in the document // Also, we have a MERGEFIELD named "Column3" that does not exist as a column in the data source // If data from "Column2" is suitable for the "Column3" MERGEFIELD, // we can map that column name to the MERGEFIELD in the "MappedDataFields" key/value pair MappedDataFieldCollection mappedDataFields = doc.getMailMerge().getMappedDataFields(); // A data source column name is linked to a MERGEFIELD name by adding an element like this mappedDataFields.add("MergeFieldName", "DataSourceColumnName"); // So, values from "Column2" will now go into MERGEFIELDs named "Column3" as well as "Column2", if there are any mappedDataFields.add("Column3", "Column2"); // The MERGEFIELD name is the "key" to the respective data source column name "value" Assert.assertEquals(mappedDataFields.get("MergeFieldName"), "DataSourceColumnName"); Assert.assertTrue(mappedDataFields.containsKey("MergeFieldName")); Assert.assertTrue(mappedDataFields.containsValue("DataSourceColumnName")); // Now if we run this mail merge, the "Column3" MERGEFIELDs will take data from "Column2" of the table doc.getMailMerge().execute(dataTable); // We can count and iterate over the mapped columns/fields Assert.assertEquals(mappedDataFields.getCount(), 2); Iterator<Map.Entry<String, String>> enumerator = mappedDataFields.iterator(); try { while (enumerator.hasNext()) { Map.Entry<String, String> dataField = enumerator.next(); System.out.println(MessageFormat.format("Column named {0} is mapped to MERGEFIELDs named {1}", dataField.getValue(), dataField.getKey())); } } finally { if (enumerator != null) enumerator.remove(); } // We can also remove some or all of the elements mappedDataFields.remove("MergeFieldName"); Assert.assertFalse(mappedDataFields.containsKey("MergeFieldName")); Assert.assertFalse(mappedDataFields.containsValue("DataSourceColumnName")); mappedDataFields.clear(); Assert.assertEquals(mappedDataFields.getCount(), 0); // Removing the mapped key/value pairs has no effect on the document because the merge was already done with them in place doc.save(getArtifactsDir() + "MailMerge.MappedDataFieldCollection.docx"); } /// <summary> /// Create a document with 2 MERGEFIELDs, one of which does not have a corresponding column in the data table. /// </summary> private static Document createSourceDocMappedDataFields() throws Exception { Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Insert two MERGEFIELDs that will accept data from that table builder.insertField(" MERGEFIELD Column1"); builder.write(", "); builder.insertField(" MERGEFIELD Column3"); return doc; } /// <summary> /// Create a data table with 2 columns, one of which does not have a corresponding MERGEFIELD in our source document. /// </summary> private static DataTable createSourceTableMappedDataFields() { // Create a data table that will be used in a mail merge DataTable dataTable = new DataTable("MyTable"); dataTable.getColumns().add("Column1"); dataTable.getColumns().add("Column2"); dataTable.getRows().add(new Object[]{"Value1", "Value2"}); return dataTable; }