Sign In  Sign Up Live-Chat

Images not appearing in Mail Merge

Last post 09-11-2008, 1:35 PM by KLETCO. 2 replies.
Sort Posts: Previous Next
  •  09-10-2008, 8:12 AM 143332

    Images not appearing in Mail Merge

    We are using mail merge and would like to display images dynamically.  This is using Aspose.Words with Java.We have tried this several different ways and nothing is working.

    Our handler:
        private class HandleMergeRequestImage implements MergeImageFieldEventHandler
        {
            public void mergeImageField(Object sender, MergeImageFieldEventArgs e)
            {
                java.awt.image.BufferedImage image = e.getImage();
               
                if (e.getFieldValue() != null && !"".equals(e.getFieldValue()))
                    e.setImageFileName((String)e.getFieldValue());
            }
        }


    This method loads the correct fields:
        private void addQuotedRequestedValuesField(Map<String, Object> fields, String field, String quotedValue, String requestedValue) throws Exception {
            fields.put("QV_"+field, quotedValue);
            fields.put("RV_"+field, requestedValue);
           
            if (!requestedValue.equals(quotedValue))
                fields.put("Image:"+field, "http://localhost:8080/image/icons/edit1.gif");
            else
                fields.put("Image:"+field, "");
        }

    So, for example, we might call the addQuotedRequestedValuesField method this way:
            this.addQuotedRequestedValuesField(fields, "MCFP", code, requestedValue);

    later on, we call a mail merge like this:
            // Perform Mail Merge
            // ==================
            String[] tags = new String[fields.size()];
            Object[] values = new String[fields.size()];
           
            fields.keySet().toArray(tags);
            fields.values().toArray(values);
       
            // Throws: Exception
            document.getMailMerge().execute(tags, values);


    When debugging the image handler, getFieldValue() is always null.  Why?
    Thanks,
    Kim

    This message was posted using Page2Forum from Aspose.Words for .NET and Java - Documentation
     
  •  09-10-2008, 11:16 AM 143371 in reply to 143332

    Re: Images not appearing in Mail Merge

    Hi

     

    Thanks for your inquiry. It seems the problem occurs because you put “Image:fieldName” value into the field names array, but you should put only “fieldName” without “Image”.

    Also if you store full path to image in your data source then you don’t need mergeImageField event handler at all. See the following code:

     

    //Create names and values arrays

    String[] names = { "myImg" };

    Object[] values = { "http://www.aspose.com/Images/aspose-logo.jpg" };

    //Open template

    Document doc = new Document("in.doc");

    //Execute mail merge

    doc.getMailMerge().execute(names, values);

    //Save output doc

    doc.save("out.doc");

     

    In the template I have only one mergefield with name “Image:myImg”.

     

    Also see the following link to learn more about using MergeImageFiled event handler.

    http://www.aspose.com/documentation/file-format-components/aspose.words-for-.net-and-java/the-mergeimagefield-event.html

     

    Best regards.


    Alexey Noskov
    Developer/Technical Support
    Aspose Auckland Team
     
  •  09-11-2008, 1:35 PM 143640 in reply to 143371

    Re: Images not appearing in Mail Merge

    Thank you!  This worked.  There are 2 developers here kicking ourselves now over how easy that was... and wondering how we missed that in the documentation.
     
View as RSS news feed in XML