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

Free Support Forum - aspose.com

Read text from Mergefield

Hello,

I have a problem with getting text contained within a mergefield in a Word Document.
If you take a look at the attached Word Document: (‘Mergefield_IN.docx’) and press ALT+F9 you will see that within the mergefield is another conditional mergefield and a lot of text with a table with even more mergefields.
Do you have a solution for extracting the entire contained text within the mergefield in Java code, or save it in another word file?
I tried the following solution:

DocumentBuilder builder = new DocumentBuilder(doc);

for (com.aspose.words.Field field: doc.getRange().getFields())

{

if (field.getType() == FieldType.FIELD_MERGE_FIELD)

{

builder.moveToField(field, true);

String name = field.getFieldCode();

builder.write(name);

}

}


This sadly did not work as it simply printed MERGEFIELD for every mergefield and not the text contained within.

Thank you very much in advance.

Best regards,
Dolten
Hi Dolten,

Thanks for your inquiry. You are getting the expected output. The text is inside IF field. Please use overloaded method of Field.getFieldCode (Boolean) as shown below. This method returns text between field start and field separator (or field end if there is no separator).

Document doc = new Document(MyDir + "Mergefield_IN.docx");
for (com.aspose.words.Field field: doc.getRange().getFields())
{
if (field.getType() == FieldType.FIELD_IF)
{
System.out.println(field.getFieldCode(true));
}
}

Please note that a field in a Word document is a complex structure consisting of multiple nodes that include field start, field code, field separator, field result and field end. Fields can be nested, contain rich content and span multiple paragraphs or sections in a document. The Field class is a "facade" object that provides properties and methods that allow to work with a field as a single object.

The Start, Separator and End properties point to the field start, separator and end nodes of the field respectively.

If you still face any problem, please share your expected output. We will then provide you more information about your query.

Thank you very much for your quick reply.
The goal ist basically to get the entire Mergefield as plain text.
My expected output is shown in the attached document (‘Mergefield_OUT.docx’).

Best regards,
Dolten

Hi Dolten,

Thanks for sharing the detail. We suggest you please read following article:
How to Replace or Modify Hyperlinks and Replace Fields with Static Text

Please use following code example to get the desired output. Hope this helps you.

Document doc = new Document(MyDir + "Mergefield_IN.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
for (com.aspose.words.Field field: doc.getRange().getFields())
{
if (field.getType() == FieldType.FIELD_IF)
{
builder.moveToField(field, false);
builder.write("{ ");
builder.moveToField(field, true);
builder.write("} ");
field.getStart().remove();
field.getEnd().remove();
field.getSeparator().remove();
}
}

for (com.aspose.words.Field field: doc.getRange().getFields())
{
if (field.getType() == FieldType.FIELD_MERGE_FIELD)
{
builder.moveToField(field, true);
builder.write("{ "+field.getFieldCode()+" }");
System.out.println(field.getFieldCode(true));
}
}

doc.getRange().getFields().clear();
doc.save(MyDir + "output.docx");