Word Replace Fields With Static Text

Hi,

I’m trying to modify the “ReplaceFieldsWithStaticText.Program.FieldsHelper” from the Samples packs to flatten out all date fields in a document (FieldCreateDate, FieldDate, FieldPrintDate etc) no matter where it occurs.

The sample works fine with the document provided, but I’m having trouble making it work across the whole document. If I pass the parent document node as a “CompositeNode”, would it do all the nodes (header/footer, tables etc)? E.g.

FieldsHelper.ConvertFieldsToStaticText(doc, FieldType.FieldCreateDate);

I had tried this and it didn’t seem to find the fields on the header/footer when I was debugging.

I also changed the ConvertFieldsToStaticText to only have 1 parameter (CompositeNode) and sequentially called the FieldsHelper for the date fields e.g.

helper = new FieldsHelper(FieldType.FieldCreateDate);
compositeNode.Accept(helper);
helper = new FieldsHelper(FieldType.FieldDate);
compositeNode.Accept(helper);
helper = new FieldsHelper(FieldType.FieldPrintDate);
compositeNode.Accept(helper);
… etc

Is this an acceptable way to do it? I’m not sure if I should be saving the composite node after every call or is Accept() good enough. Is there a better way to achieve this?

Lastly, I just want to check that the sample code flattens the field with current displayed text not the actual current value. For example if I have a {FileName} field for a document “1.docx” and close + save the document, then in Windows Explorer rename the document to “2.docx”, then when I open the document in Aspose and convert the field to static text, it will show “1.docx” as that was the last evaluated field eventhough the document is now 2.docx (assuming I haven’t called Document.UpdateFields()).

Thanks

Hi Gary,

Thanks for your inquiry. In case you are using an older version of Aspose.Words, I would suggest you please upgrade to the latest version (v13.4.0) from here and let us know how it goes on your side.

GaryO:
The sample works fine with the document provided, but I’m having trouble making it work across the whole document. If I pass the parent document node as a “CompositeNode”, would it do all the nodes (header/footer, tables etc)? E.g.

Yes, this code works for header/footer when you pass Document Node in ConvertFieldsToStaticText method. Please see the following code snippet for your kind reference.

FieldsHelper.ConvertFieldsToStaticText(doc, FieldType.FieldIf);
FieldsHelper.ConvertFieldsToStaticText(doc, FieldType.FieldMergeField);
FieldsHelper.ConvertFieldsToStaticText(doc, FieldType.FieldCreateDate);

Please read following documentation link for your kind reference.
https://docs.aspose.com/words/net/updating-and-removing-a-field/

GaryO:
Lastly, I just want to check that the sample code flattens the field with current displayed text not the actual current value. For example if I have a {FileName} field for a document “1.docx” and close + save the document, then in Windows Explorer rename the document to “2.docx”, then when I open the document in Aspose and convert the field to static text, it will show “1.docx” as that was the last evaluated field eventhough the document is now 2.docx (assuming I haven’t called Document.UpdateFields()).

Please update all fields by using MS Word and check the output of FileName field. In your case, I suggest you please call the Document.Update Fields method before calling ConvertFieldsToStaticText method. Please read following documentation link for your kind reference.
https://reference.aspose.com/words/net/aspose.words.fields/field/

If the problem still remains, please attach your input Word document here for testing. I will investigate the issue on my side and provide you more information.

Thanks for your help!

Hi Gary,

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.