Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Wed Nov 14, 2018 10:17 am

Hello,

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?

TGellert
 
Posts: 5
Joined: Wed Nov 14, 2018 9:38 am

Thu Nov 15, 2018 3:09 am

Hi,

Thanks for your inquiry.
To help us investigate your issue quickly and accurately, please share your input Word as well as your custom method RecurseElements.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Thu Nov 15, 2018 10:52 am

Code and Word document attached.

I also included the generated .pdf and .docx. Additionally you will find the generated .docx where I did a recalculate fields manually to see the expected result...

TGellert
 
Posts: 5
Joined: Wed Nov 14, 2018 9:38 am

Fri Nov 16, 2018 3:31 am

Hi,

Thanks for your information.
After testing your case with Spire.Doc Pack(hot fix) Version:6.11.7, the field was updated correctly. Please try to use this version.
Besides, attached are my full code and result file for your kind reference:
Code: Select all
            var fileStream = File.OpenRead(@"F:\Test Antrag unvollst刵dig -Template.docx");
            Dictionary<string, string> data = new Dictionary<string, string>();
            data.Add("AGemeinde", "Bispingen");
            data.Add("AOrt", "Fehler");
            data.Add("Antragsnr", "A201800020");
            data.Add("AStrasse", "Hörpeler Bahnhof");
            data.Add("AHausnr", "34");
            data.Add("BriefanredeVPartner", "Sehr geehrte Damen und Herren,");
            data.Add("WEinheiten", "6");
            data.Add("BeaName", "Gellert");
            using (var doc = new Document(fileStream, FileFormat.Docx))
            {
                // custom method replacing the placeholder values
               RecurseElements(doc.ChildObjects, data);
                using (MemoryStream ms = new MemoryStream())
                {
                    doc.IsUpdateFields = true;
                    doc.SaveToStream(ms,FileFormat.PDF);
                    File.WriteAllBytes("15596.pdf",ms.ToArray());
                }
            }
            System.Diagnostics.Process.Start("15596.pdf");
        }
        private static void RecurseElements(IEnumerable elements, IDictionary<string, string> data)
        {
            foreach (DocumentObject dobj in elements)
            {
                ProcessStructureDocumentTagInline(dobj, data);
                ProcessStructureDocumentTag(dobj, data);

                if (dobj.ChildObjects != null)
                {
                    RecurseElements(dobj.ChildObjects, data);
                }
            }
        }

        private static void ProcessStructureDocumentTagInline(object dobj, IDictionary<string, string> data)
        {
            var tag = dobj as StructureDocumentTagInline; // oder IStructureDocument: nein!! beißt sich mit der Tabellenauflösung
            if (tag == null)
            {
                return;
            }

            if (tag.SDTProperties.Tag == null)
            {
                return;
            }

            if (!data.ContainsKey(tag.SDTProperties.Tag))
            {
                return;
            }

            var replaced = false;
            for (var i = 0; i < tag.ChildObjects.Count; i++)
            {
                // Irgendwie bekommt Word es hin, einem Inhaltssteuerelement eine Liste mit Kindern zu verpassen,
                // indem z.B. "KOrt" zerlegt wird in "K" und "Ort". Möglicherweise hat das was mit der
                // Dokumenthistorie zur tun?
                var textRange = tag.ChildObjects[i] as TextRange;
                if (textRange != null)
                {
                    textRange.Text = !replaced ? data[tag.SDTProperties.Tag] : string.Empty;
                    replaced = true;
                }
            }
        }

        private static void ProcessStructureDocumentTag(object dobj, IDictionary<string, string> data)
        {
            var tag = dobj as StructureDocumentTag;
            if (tag == null)
            {
                return;
            }

            if (tag.SDTProperties.Tag == null)
            {
                return;
            }

            if (!data.ContainsKey(tag.SDTProperties.Tag))
            {
                return;
            }
            var replaced = false;
            for (var i = 0; i < tag.ChildObjects.Count; i++)
            {
                // Irgendwie bekommt Word es hin, einem Inhaltssteuerelement eine Liste mit Kindern zu verpassen,
                // indem z.B. "KOrt" zerlegt wird in "K" und "Ort". Möglicherweise hat das was mit der
                // Dokumenthistorie zur tun?
                var paragraph = tag.ChildObjects.FirstItem as Paragraph;
                if (paragraph != null)
                {
                    paragraph.Text = !replaced ? data[tag.SDTProperties.Tag] : string.Empty;
                    replaced = true;
                }
            }
        }


Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Fri Nov 16, 2018 7:36 am

Hello Betsy,

thanks for the fast reply. I downloaded your result file, but it still has the problem, the result ist the same as in my tests.

In the template, please notice the region:
Wohneinheiten: [WEinheiten]
{ IF WEinheitenRef <= 1 "Text1" "" }
{ IF WEinheitenRef > 1 "Text3" "" }

In the template the content of the formular label WEinheiten is "WEinheiten", the condition in the first field formula evaluates to true, so "Text1" ist shown. The condition in the second field formula evaluates to false, so "" ist shown.

In the generated documents (pdf or docx) the [WEinheiten] ist set to 6. There is a surrounding TextReference WEinheitenRef arround WEinheiten, so it can be referenced inside the field formula. In my generated pdf and also in your generated 15596.pdf both conditions in the field formula evaluate to false, so in both lines an empty text is emitted.

When you look a my attached generated docx, there is "Text1" shown on the line of the first field and "" on the line of the second field. It's the same result as in the template itself. When you now select the whole text (Ctrl-A) and manually recalculate all fields, you will get the expected output:
The condition in the first field formula (6 <= 1) evaluates to false, so "" ist shown. The condition in the second field formula (6 > 1) evaluates to true, so "Text3" ist shown.

The 15596.pdf should have the same output, to be correct:
Wohneinheiten: 6

Text3

TGellert
 
Posts: 5
Joined: Wed Nov 14, 2018 9:38 am

Fri Nov 16, 2018 8:44 am

Hi,

Sorry that I didn't notice the issue before.
Now I have logged the issue in our bug tracking system. We will let you know once there is any update.
We apologize for the inconvenience.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Fri Dec 14, 2018 7:59 am

Hi,

Thanks for your waiting.
Now the issue is fixed in Spire.Doc Pack(hot fix) Version:6.12.4.
Looking forward to your feedback.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Mon Dec 17, 2018 6:43 am

Hi,

Greetings from E-iceblue.
Do you use the hotfix? Has your issue been resolved?

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Mon Dec 17, 2018 2:38 pm

Hello Betsy,

thanks a lot for the fix. I will check it within the next weeks and give feedback, but it will be january after the big christmas holiday.

Yours Thorsten

TGellert
 
Posts: 5
Joined: Wed Nov 14, 2018 9:38 am

Tue Dec 18, 2018 2:39 am

Hi Thorsten,

Thanks for your response.
Wish you a wonderful Christmas!
If there is any question while using our product, just feel free to contact us.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Fri Jan 11, 2019 1:40 pm

Hello Betsy,

the new version fixes the issue, it's working now as expected. Thanks

TGellert
 
Posts: 5
Joined: Wed Nov 14, 2018 9:38 am

Mon Jan 14, 2019 1:57 am

Hi Thorsten,

Thanks for your feedback.
Any question, please feel free to contact us.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Return to Spire.Doc