com.aspose.words

Class FieldMergingArgsBase

  • java.lang.Object
    • com.aspose.words.FieldMergingArgsBase
See Also:
FieldMergingArgs, ImageFieldMergingArgs

Property Getters/Setters Summary
DocumentgetDocument()
Returns the Document object for which the mail merge is performed.
java.lang.StringgetDocumentFieldName()
Gets the name of the merge field as specified in the document.
FieldMergeFieldgetField()
Gets the object that represents the current merge field.
java.lang.StringgetFieldName()
Gets the name of the merge field in the data source.
java.lang.ObjectgetFieldValue()
Gets the value of the field from the data source.
intgetRecordIndex()
Gets the zero based index of the record that is being merged.
java.lang.StringgetTableName()
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

        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 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() + "\\Artifacts\\MailMerge.InsertHtml.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)
            {
                // Do nothing.
            }
        }
      • getDocumentFieldName

        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 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() + "\\Artifacts\\MailMerge.InsertHtml.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)
            {
                // Do nothing.
            }
        }
      • getField

        public FieldMergeField getField()
        
        Gets the object that represents the current merge field.
      • getFieldName

        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".

        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.
            DataTable dataTable = getStudentCourseDataTable();
            doc.getMailMerge().executeWithRegions(dataTable);
        
            // Save resulting document with a new name.
            doc.save(getMyDir() + "\\Artifacts\\MailMerge.InsertCheckBox.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)
            {
                // 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 DataTable getStudentCourseDataTable() throws Exception
        {
            DataTable dataTable = new DataTable("StudentCourse");
            dataTable.getColumns().add("CourseName");
            for (int i = 0; i < 10; i++)
            {
                DataRow datarow = dataTable.newRow();
                dataTable.getRows().add(datarow);
                datarow.set(0, "Course " + Integer.toString(i));
            }
            return dataTable;
        }
      • getFieldValue

        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 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() + "\\Artifacts\\MailMerge.InsertHtml.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)
            {
                // Do nothing.
            }
        }
      • getRecordIndex

        public int getRecordIndex()
        
        Gets the zero based index of the record that is being merged.
      • getTableName

        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.