Insert MailMerge Field into a Shape (textbox)

I am using Aspose Word to create a mail merge document that has the mailmerge fields embedded. I need some of the MaileMerge fields to be vertical text. I can successfully embed the MaileMerge fields using DocumentBuilder.insertField() and I can create a Shape that can display vertical text. But - can you help me get an ‘insertField’ into a ‘Shape’ object?

Hi John,

Thanks for your inquiry. Please use the following code example to achieve your requirements. Hope this helps you. Please let us know if you have any more queries.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape textBox = new Shape(doc, ShapeType.TEXT_BOX);
textBox.getTextBox().setLayoutFlow(LayoutFlow.TOP_TO_BOTTOM);
textBox.setHeight(100);
textBox.setWidth(100);
textBox.appendChild(new Paragraph(doc));
Paragraph para = textBox.getFirstParagraph();
para.appendChild(new Run(doc, ""));
doc.getFirstSection().getBody().getFirstParagraph().appendChild(textBox);
builder.moveTo(textBox.getFirstParagraph());
builder.insertField("MERGEFIELD MyFieldName \\ MERGEFORMAT");
doc.save(MyDir + "Out.docx");
String[] input_names = new String[] { "MyFieldName" };
Object[] input_data = new Object[] { "Aspose.Words" };
doc.getMailMerge().execute(input_names, input_data);
doc.save(MyDir + "Mailmerge.Out.docx");

Thanks for that code – it works great
some follow on questions…

  1. I need to have the textbox drawn without borders – any ideas?
  2. When I am creating this document it is a very linear situation with mail merge tags being used on each line. Once I use your source code and move the cursor how do I get the cursor back to where it was before I create the text box?

I need to draw this textbox where the current cursor is in the document. I’ve been trying to move it around but with no success.

Hi John,

Thanks for your inquiry.

*john.nelson:

  1. I need to have the textbox drawn without borders – any ideas?*

Please set the value of Shape.Stroked as false to achieve your requirements. I suggest you please read the members of Shape class from here:
https://reference.aspose.com/words/java/com.aspose.words/shape

*john.nelson:

  1. When I am creating this document it is a very linear situation with mail merge tags being used on each line. Once I use your source code and move the cursor how do I get the cursor back to where it was before I create the text box?*

Please read the following documentation links for your kind reference.
https://docs.aspose.com/words/java/document-builder-overview/
https://docs.aspose.com/words/java/navigation-with-cursor/
https://docs.aspose.com/words/java/use-documentbuilder-to-insert-document-elements/

Please check the DocumentBuilder.MoveToXXX methods from here:
https://reference.aspose.com/words/java/com.aspose.words/DocumentBuilder

In your case I suggest you please insert the bookmark at the current position of cursor and then insert the mail merge field inside text box. After inserting the mail merge field, use the DocumentBuilder.MoveToBookmark method to move the cursor back to same location.

Hope this helps you. Please let us know if you have any more queries.

ok after reading, researching, and trying many different things I still cannot get the output that I want. I simply want to write data to a Word document, then write a mailmerge field into a textbox, and then keep on writing at that same cursor place. Here is my code and this will create the textbox with the mailmerge field at the very beginning of the document.

builder.startBookmark("FineBookmark");
builder.endBookmark("FineBookmark");

Shape textBox = new Shape(doc, ShapeType.TEXT_BOX);
textBox.setStroked(false);
textBox.getTextBox().setLayoutFlow(LayoutFlow.BOTTOM_TO_TOP);
textBox.setHeight(100);
textBox.setWidth(20);

textBox.appendChild(new Paragraph(doc));
Paragraph para = textBox.getFirstParagraph();
para.appendChild(new Run(doc, ""));

doc.getFirstSection().getBody().getFirstParagraph().appendChild(textBox);
builder.moveTo(textBox.getFirstParagraph());

builder.insertField("MERGEFIELD XAxisName \* MERGEFORMAT");
builder.moveToBookmark("FineBookmark");

builder.write(" ");
builder.insertField("MERGEFIELD Image:myImage \* MERGEFORMAT");
builder.write("\r\n");
builder.insertField("MERGEFIELD YAxisName \* MERGEFORMAT");

Any ideas to help me get this cursor figured out?

Hi John,

Thanks for your inquiry. Please manually create your expected Word documents using Microsoft Word and attach it here for our reference. Please share target documents before performing mail merge and after mail merge.

We will investigate how you want your final Word output be generated like. We will then provide you more information on this along with code.