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.

Tue Nov 23, 2021 5:05 am

Hi,

I have a problem while updating fields in spire.doc.

Code: Select all
new Field(doc)
            {
                Type = FieldType.FieldPageRef,
                Code = $@"PAGEREF {GetKey(guid)} \h"
            }


The corresponding fields are not updated despite setting

Code: Select all
document.IsUpdateFields = true;


Actual Version is Spire.Office 6.10.3

Can you please help me quickly? I need the function urgently.

Another point is the documentation, it is very tedious to set the Field.Code property correctly. Can you give me a reference to the corresponding documentation?

MaSchuessler
 
Posts: 9
Joined: Thu Jul 22, 2021 6:34 am

Tue Nov 23, 2021 9:33 am

Hello,

Thank you for your inquiry.
I did an initial test using Spire. Office 6.9.10 and did reproduce your issue. But I found that by saving the Word file as a stream through the following code, and then load the stream and update the field, the result file is correct. Please try it. If the issue still exists after trying, please provide your sample Word file and complete test code for our investigation. You could attach them here or send them to us via email (support@e-iceblue.com).
Code: Select all
Document document = new Document();
document.LoadFromFile("input.docx");
Field fields = new Field(document);
fields.Type = FieldType.FieldPageRef;
fields.Code = $@"PAGEREF {GetKey(guid)} \h";
Paragraph paragraph = document.Sections[0].AddParagraph();
paragraph.ChildObjects.Add(fields);
FieldMark fieldSeparator = new FieldMark(document, FieldMarkType.FieldSeparator);
paragraph.ChildObjects.Add(fieldSeparator);
FieldMark fieldEnd = new FieldMark(document, FieldMarkType.FieldEnd);
paragraph.ChildObjects.Add(fieldEnd);
MemoryStream stream = new MemoryStream();
document.SaveToStream(stream, FileFormat.Docx);
document.LoadFromStream(stream, FileFormat.Docx);
document.IsUpdateFields = true;
document.SaveToFile("output.docx", FileFormat.Docx2013);

In addition, regarding the Field.Code, we follow Microsoft Word 's rules, so you can create the fields you want in the Word file, and then use the shortcut key "Ctrl+F9" to view the Field.Code.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1643
Joined: Wed Apr 07, 2021 2:50 am

Tue Nov 23, 2021 10:58 am

Hello Annika,

this helped a lot! Thank you very much!

Can you please give me a hint when the issue is fixed officially?

Best Marcus

MaSchuessler
 
Posts: 9
Joined: Thu Jul 22, 2021 6:34 am

Wed Nov 24, 2021 1:25 am

Hi,

You're welcome.
Once the issue is resolved, I will inform you immediately.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1643
Joined: Wed Apr 07, 2021 2:50 am

Tue Dec 07, 2021 1:51 am

Hello,

Thanks for your patience.
After investigation by our developers, it was found that this issue was caused by an unending field. The following is the modified code, please test it on your side and tell us your test results. Looking forward to your test feedback.
Code: Select all
Document document = new Document();
document.LoadFromFile("input.docx");
Field fields = new Field(document);
fields.Type = FieldType.FieldPageRef;
fields.Code = $@"PAGEREF {GetKey(guid)} \h";
Paragraph paragraph = document.Sections[0].AddParagraph();
paragraph.ChildObjects.Add(fields);
FieldMark fieldSeparator = new FieldMark(document, FieldMarkType.FieldSeparator);
paragraph.ChildObjects.Add(fieldSeparator);
FieldMark fieldEnd = new FieldMark(document, FieldMarkType.FieldEnd);
paragraph.ChildObjects.Add(fieldEnd);

//Set the end identifier for the field
fields.End = fieldEnd;
document.IsUpdateFields = true;
document.SaveToFile("output.docx", FileFormat.Docx2013);

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1643
Joined: Wed Apr 07, 2021 2:50 am

Mon Mar 27, 2023 3:57 am

This code is only adding the field with page data however the page number is presented on the file only after "right click -> Update field" in word

Is there any way to force an update on the field?

Spire.Docs 10.11.0
Dot.net Framework 4.8

The code and files attached

Document doc = new Document(@"...\input.docx");

Section addsec = doc.AddSection();
doc.Sections.Add(addsec);


Field fields1 = new Field(doc);
fields1.Type = FieldType.FieldPageRef;
fields1.Code = $@"PAGEREF bookmarkTest01 \h";
Paragraph paragraph = addsec.AddParagraph();
paragraph.ChildObjects.Add(fields1);
FieldMark fieldSeparator1 = new FieldMark(doc, FieldMarkType.FieldSeparator);
paragraph.ChildObjects.Add(fieldSeparator1);
FieldMark fieldEnd1 = new FieldMark(doc, FieldMarkType.FieldEnd);
paragraph.ChildObjects.Add(fieldEnd1);

//Set the end identifier for the field
fields1.End = fieldEnd1;
doc.IsUpdateFields = true;
doc.SaveToFile(".../output.docx", FileFormat.Docx);


Thanks.

inheritaustralia
 
Posts: 5
Joined: Wed Nov 24, 2021 6:25 am

Mon Mar 27, 2023 7:54 am

inheritaustralia wrote:This code is only adding the field with page data however the page number is presented on the file only after "right click -> Update field" in word

Is there any way to force an update on the field?

Spire.Docs 10.11.0
Dot.net Framework 4.8

The code and files attached

Document doc = new Document(@"...\input.docx");

Section addsec = doc.AddSection();
doc.Sections.Add(addsec);


Field fields1 = new Field(doc);
fields1.Type = FieldType.FieldPageRef;
fields1.Code = $@"PAGEREF bookmarkTest01 \h";
Paragraph paragraph = addsec.AddParagraph();
paragraph.ChildObjects.Add(fields1);
FieldMark fieldSeparator1 = new FieldMark(doc, FieldMarkType.FieldSeparator);
paragraph.ChildObjects.Add(fieldSeparator1);
FieldMark fieldEnd1 = new FieldMark(doc, FieldMarkType.FieldEnd);
paragraph.ChildObjects.Add(fieldEnd1);

//Set the end identifier for the field
fields1.End = fieldEnd1;
doc.IsUpdateFields = true;
doc.SaveToFile(".../output.docx", FileFormat.Docx);


Thanks.

Hi,

Thanks for your feedback.
After testing, I reproduced your issue and logged it into our issue tracking system with the ticket number SPIREDOC-9213, our developers will investigate and fix it. Sorry for the inconvenience caused, once the issue is fixed, I will inform you asap.

Best regards,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Fri Aug 18, 2023 2:14 am

inheritaustralia wrote:This code is only adding the field with page data however the page number is presented on the file only after "right click -> Update field" in word

Is there any way to force an update on the field?

Spire.Docs 10.11.0
Dot.net Framework 4.8

The code and files attached

Document doc = new Document(@"...\input.docx");

Section addsec = doc.AddSection();
doc.Sections.Add(addsec);


Field fields1 = new Field(doc);
fields1.Type = FieldType.FieldPageRef;
fields1.Code = $@"PAGEREF bookmarkTest01 \h";
Paragraph paragraph = addsec.AddParagraph();
paragraph.ChildObjects.Add(fields1);
FieldMark fieldSeparator1 = new FieldMark(doc, FieldMarkType.FieldSeparator);
paragraph.ChildObjects.Add(fieldSeparator1);
FieldMark fieldEnd1 = new FieldMark(doc, FieldMarkType.FieldEnd);
paragraph.ChildObjects.Add(fieldEnd1);

//Set the end identifier for the field
fields1.End = fieldEnd1;
doc.IsUpdateFields = true;
doc.SaveToFile(".../output.docx", FileFormat.Docx);


Thanks.

Hi,

Thanks for your patience.
Glad to inform you that we just released Spire.Doc 11.8.9 hotfix, which has fixed your issue SPIREDOC-9213. Please download from the following links and have a test.
Website: https://www.e-iceblue.com/Download/download-word-for-net-now.html
Nuget: https://www.nuget.org/packages/Spire.Doc/11.8.9

Best regards,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Return to Spire.Doc