I am trying to build an HTML and a Word document from a template using mail merge. The template is a Word document with images. I would like to set the template up so that the images can be displayed in both the HTML and the Word formats. Additionally, I would like the image references in the template to be URL’s of the form (http://www.somewhere.com/images/). What would you suggest as the best course of action? I am using version 3.0 of Aspose.Word.
Thanks for your request. You can try specifying ImageFolderAlias as shown in the following code:
// Open document
Document doc = new Document("C:\\Temp\\in.doc");
// specify alias of the folder. in our case this is part of url
doc.getSaveOptions().setHtmlExportImagesFolderAlias("http://localhost/test/");
//Save document in HTML
doc.save("C:\\Temp\\out.html");
Thanks for the reply but I still need help.
I want to reference images in my template (‘doc’ in your reply) in the form: Image:image1.gif.
I want to use MailMerge to replace those tags with images of the form: ‘http://www.somewhere.com/images/image1.gif’. I want to use a single template to produce both HTML and MS Word documents.
Using the HtmlExportImagesFolderAlias allows me to set the http://www.somewhere.com/images prefix of the URL so thats a part of the solution works.
Use of a MergeImageFieldEventHandler allows me access to the field named, Image:image1.gif, but whats my next move from here?
Thanks for your request. Maybe you can try inserting linked images into a document during mail merge. Please see the following code and attached documents.
// Open template
Document doc = new Document("C:\\Temp\\in.doc");
// Add MailMergeImage event handler
doc.getMailMerge().addMergeImageFieldEventHandler(new HandleMergeField_InsertImageLink());
// Execute mail merge
doc.getMailMerge().execute(new String[] { "myImage" }, new Object[] { "http://www.aspose.com/Images/aspose-logo.jpg" });
// Save document in HTML and doc
doc.save("C:\\Temp\\out.doc");
doc.save("C:\\Temp\\out.html");
public class HandleMergeField_InsertImageLink implements MergeImageFieldEventHandler
{
public void mergeImageField(Object sender, MergeImageFieldEventArgs e) throws Exception
{
// Create document builder
DocumentBuilder builder = new DocumentBuilder(e.getDocument());
// move documentBuilder cursor to the mergefield
builder.moveToField(e.getField(), true);
// Insert linked image
//Create shape
Shape shape = new Shape(e.getDocument(), ShapeType.IMAGE);
//Set paht to the linked image
shape.getImageData().setSourceFullName(e.getFieldValue().toString());
//Set size of the image
shape.setHeight(75);
shape.setWidth(188);
//Inset image into teh docuemnt
builder.insertNode(shape);
e.getField().remove();
}
}