we use Spire.Doc 6.11.5 (commercial version) to generate PDF or Word documents from Word templates. The application has a storage with .docx-files (the templates) and the user selectes one of them to generate a document. The .docx is loaded into Spire.Doc, then the document object tree is iterated. We use label fields from the developer ribbon as placeholders for value replacement, the placeholder string is placed in the tag property of the label field. So when the tree traversal of Document.ChildObjects finds such a label field with a tag property, it parses the placeholder string in the tag property and calculates the corresponding value. The the text of the field ist set to that value. Afterwards the document is saved as PDF or Word document depending of the users choice.
This process works smooth, the placeholders are replaced with the values. But there ist one problem, the calculated fields are not recalculated during the saving to PDF. Document.isUpdateFields ist set to true, but has no effect. The event UpdateFields is not triggered also. In the debugger one can see the calculated fields in the collection Document.Fields, but it seems that the fields are calculated during the loading of the document. As example a { TIME } field has the correct calculated value after loading the .docx.
When we save the .docx which contains the replaced values, the calculated fields still have the values which are calculated after loading the .docx (und which we can see in the debugger). When we open this file with Word and manually recalculate all fields, all field values are calculated the right way based on the replaced placeholder values. So the field formula are correct, just the fields are not recalculated before the saving
I include a small sketch of our code, just the relevant parts, to illustrate the process:
- Code: Select all
using (var doc = new Document(fileStream, FileFormat.Auto))
{
// custom method replacing the placeholder values
this.RecurseElements(doc.ChildObjects, data);
using (MemoryStream ms = new MemoryStream())
{
doc.IsUpdateFields = true;
doc.SaveToStream(ms, outputFormat);
}
byte[] result = ms.ToArray();
return result;
}
Is there any way to trigger the recalculation of the calculated fields?