IMailMergeDataSource

Last post 07-30-2010, 2:46 AM by SaschaRue. 5 replies.
Sort Posts: Previous Next
  •  07-30-2010, 1:11 AM 251214

    IMailMergeDataSource Java

    Hi,
    is there a way to insert HTML formatted text (like in DocumentBuilder.insertHTML) when using a IMailMergeDataSource? The getValue() method only returns an object array and set this to a html string doesn't work (as it is not formatted in word).
    Any ideas?
     
  •  07-30-2010, 1:49 AM 251228 in reply to 251214

    Re: IMailMergeDataSource

    Hi

     

    Thanks for your inquiry. Sure, you can. You can use MergeFieldEvenHandler to achieve this:

    http://www.aspose.com/documentation/java-components/aspose.words-for-java/com/aspose/words/mergefieldeventhandler.html

     

    Here is simple code example:

     

    // Open template

    Document doc = new Document("C:\\Temp\\in.doc");

    // Add MergeFieldEventHandler.

    doc.getMailMerge().addMergeFieldEventHandler(new HandleMergeField_InsertHtml());

    // Execute mail merge.

    doc.getMailMerge().execute(new String[] { "myField" }, new Object[] { "<b>This is simple HTML snippet</b>" });

    // Save output.

    doc.save("C:\\Temp\\out.doc");

     

    ==================================================================

     

    import com.aspose.words.DocumentBuilder;

    import com.aspose.words.MergeFieldEventArgs;

    import com.aspose.words.MergeFieldEventHandler;

     

    public class HandleMergeField_InsertHtml implements MergeFieldEventHandler

    {

        public void mergeField(Object sender, MergeFieldEventArgs e) throws Exception

        {

            if(e.getFieldValue()==null) return;

     

            if(e.getFieldValue().toString().indexOf("</") != -1)

            {

                DocumentBuilder builder = new DocumentBuilder(e.getDocument());

                builder.moveToField(e.getField(), false);

                builder.insertHtml(e.getFieldValue().toString());

                e.getField().remove();

            }

            else

            {

                e.setText(e.getFieldValue().toString());

            }

        }

    }

     

    Hope this helps.

     

    Best regards.


    Alexey Noskov
    Developer/Technical Support
    Aspose Auckland Team
     
  •  07-30-2010, 2:10 AM 251237 in reply to 251228

    Re: IMailMergeDataSource

    But I am using an IMailMergeDataSource. Is this event handler called after the method call getValue() of the IMailMergeDataSource? And is the mail merge field than not replaced by the value of the object array that is returned from this method? (In this case builder.moveToField(e.getField(), false); wouldn't find the field any more.
    Heres a snippet of my code:

    doc.getMailMerge().executeWithRegions(new IMailMergeDataSource() {

                            int recordIndex = -1;

                            @Override
                            public String getTableName() throws Exception {
                                // As we dont have a data source, the table name is the collection name
                                return "collectionString";
                            }

                            @Override
                            public boolean getValue(String fieldName, Object[] value) throws Exception {
                                if (!fieldName.startsWith("
    collectionString")) {
                                    value[0] = "";
                                    return false;
                                }
                                // as we know, we only have SimpleObject2 in the collection, just easy
                                // add the result
                                SimpleObject2 object = (SimpleObject2) coll.get(recordIndex);
                                // Cut the collection string
                                value[0] = getProperty(object, fieldName, true);
                                return true;
                            }

                            @Override
                            public boolean moveNext() throws Exception {
                                if (isEof()) {
                                    return false;
                                }
                                recordIndex++;
                                return !isEof();
                            }

                            /**
                             * Returns whether the last entry of the given collection is reached
                             *
                             * @return Is the last entry of the collection reached
                             */
                            private boolean isEof() {
                                return recordIndex >= coll.size();
                            }

                        });


    Here I am setting value[0] to the string value.
    Will adding a MergeFieldEventHandler also handles these fields?
     
  •  07-30-2010, 2:16 AM 251242 in reply to 251237

    Re: IMailMergeDataSource

    Hi

     

    Thanks for your inquiry. There is no difference what kind of data source you use. MergeFieldEventHandler fires when mergefield in the document is filled with data.

     

    Best regards.


    Alexey Noskov
    Developer/Technical Support
    Aspose Auckland Team
     
  •  07-30-2010, 2:35 AM 251245 in reply to 251242

    Re: IMailMergeDataSource

    Hi,
    unfortunately, this doesn't work with my code. The MergeFieldEventHandler is called, but the value of the field in the generated word document ist still the value that is returned from the getValue() method of the IMailMergeDataSource. I attached my code. Could you tell me what I am making wrong?
    Thank you in advance
     
  •  07-30-2010, 2:46 AM 251253 in reply to 251245

    Re: IMailMergeDataSource

    Ok,
    it works (<ul> is not really a valid stand alone html tag, definitely time for weekend :))
    Thanks for your time
     
View as RSS news feed in XML