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
Login to view the files attached to this post.