Exporting Hidden Mergefield Stuff as text

Hello,

i am trying to get a full export of a word document to .txt.
If I press ALT+F9 I see the special MergeFields conditional coding. I’d like to have those (with the remaining document around them) saved to txt

Is there an easy way to achieve that?

Best regards,
Grendel

Hi Grendel,

Thanks for your inquiry. You may write Mergefield code in your document as following. Hopefully it will help you to accomplish the task. However, if there is any difference in your requirements and our understanding, then please share your sample input and expected output Word documents here. We will further look into it and will guide you accordingly.

com.aspose.words.Document doc = new com.aspose.words.Document("in.docx");
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);
    }
}
doc.save("E:/Data/MergeField_text.DOCX");

Best Regards,