We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Add new INCLUDEPICTURE as result of MERGEFIELD

Hello,

I'm trying to add a new includepicture in the document as a result of a mergefield. to explain my idea :
Let's suppose that we have a document with the merge field
{MERGEFILED LOGO \* MERGEFORMAT }
and
LOGO = { INCLUDEPICTURE "c:/...logo.jpg" }

So I tried this :

LoadOptions options = new LoadOptions();
options.setLoadFormat(LoadFormat.DOCX);
options.setPreserveIncludePictureField(Boolean.TRUE);
Document document = new Document(templateInputStream, options );
FieldIncludePicture includePicture = new FieldIncludePicture();
includePicture.setSourceFullName("c:/...logo.jpg");
document.getMailMerge().execute(new String[]{"LOGO"},
new Object[]{includePicture });

but I'm having a null pointer exception :
java.lang.NullPointerException
at com.aspose.words.Field.getStart(Unknown Source)
at com.aspose.words.Field.getType(Unknown Source)
at com.aspose.words.zz6M.(Unknown Source)
at com.aspose.words.Field.zz6P(Unknown Source)
at com.aspose.words.Field.zz6N(Unknown Source)
at com.aspose.words.Field.zz6w(Unknown Source)
at com.aspose.words.FieldIncludePicture.setSourceFullName(Unknown Source)

The problem is that in my word document, I'm having a mergefield that I must convert to includepicture without modifying the word template (I can't add mergefield with include picture in the word template).

Thank you for your help.

Hi,


Thanks for your inquiry. Please try using the following code:

Document doc = new Document(getMyDir() + “input.docx”);
DocumentBuilder builder = new DocumentBuilder(doc);

// Insert a temporary Bookmark just before target mergefield
builder.moveToMergeField(“LOGO”, false, false);
BookmarkStart bmStart = builder.startBookmark(“LOGO”);
BookmarkEnd bmEnd = builder.endBookmark(“LOGO”);
// Get a handle to target mergefield
FieldMergeField mergeField = null;
for(Field field : doc.getRange().getFields()){
if (field.getType() == FieldType.FIELD_MERGE_FIELD){
if (((FieldMergeField)field).getFieldName().equals(“LOGO”)){
mergeField = (FieldMergeField) field;
break;
}
}
}

// Encapsulate mergefield within Bookmark
if (mergeField != null) {
mergeField.getEnd().getParentNode().insertAfter(bmEnd, mergeField.getEnd());
}

// Perform mail merge
doc.getMailMerge().execute(new String[]{“LOGO”}, new Object[]{“D:\\Temp\\aspose.words.jpg”});

// Insert a new IncludePicture field just after Bookmark
builder.moveToBookmark(“LOGO”, false, true);
FieldIncludePicture includePicture = (FieldIncludePicture) builder.insertField(
FieldType.FIELD_INCLUDE_PICTURE,
false);
includePicture.setSourceFullName(doc.getRange().getBookmarks().get(“LOGO”).getText());
includePicture.update();

// Remove Bookmark’s content and Bookmark itself
doc.getRange().getBookmarks().get(“LOGO”).setText("");
doc.getRange().getBookmarks().get(“LOGO”).remove();

doc.save(getMyDir() + “awjava-16.1.0.docx”);

Hope, this helps.

Best regards,