How to achieve cross reference dynamically through Aspose

As I understand from word behavior, cross reference fields are updated automatically but not dynamically. That is it required user intervention to update it like by pressing F9 or right clicking and choose update filed options.

My requirement is like the below.
List of countries

  1. America
  2. Australia
  3. Canada

Some text here for 3. Canada(Cross referenced field)

So now an user enter a country in between 2 and 3 the list number dynamically changes but not the referred field until or unless we trigger update,

  1. America
  2. Australia
  3. Brazil
  4. Canada

Some text here for 3. Canada
Any suggestion to achieve this. Attached sample output.

Hi Manikandan,

Thanks for your inquiry. Please note that Aspose.Words mimics the same behavior as MS Word does.

Please use Document.UpdateFields method to update the values of fields in the whole document. When you open, modify and then save a document, Aspose.Words does not update fields automatically, it keeps them intact. Therefore, you would usually want to call this method before saving if you have modified the document programmatically and want to make sure the proper (calculated) field values appear in the saved document.

Hope this answers your query. Please let us know if you have any more queries.

Hi Tahir,

Thanks for your response, Below is code snippet i tried for cross reference using Aspose words.
I have couple of queries(written after the code snippet )

Document doc = new Document();
Section currentSection = new Section(doc); 
doc.AppendChild(currentSection);
Body body = new Body(doc);
currentSection.AppendChild(body);
currentSection.PageSetup.SectionStart = SectionStart.Continuous;

List lst = doc.Lists.Add(ListTemplate.NumberDefault);
Paragraph ph1 = new Paragraph(doc);
Paragraph ph2 = new Paragraph(doc);
Paragraph ph3 = new Paragraph(doc);
Paragraph ph4 = new Paragraph(doc);
Paragraph ph5 = new Paragraph(doc);

ph1.ListFormat.List = lst;
ph2.ListFormat.List = lst;
ph3.ListFormat.List = lst;
ph4.ListFormat.List = lst;
Run rn1 = new Run(doc); 
Run rn2 = new Run(doc);
Run rn3 = new Run(doc);
Run rn4 = new Run(doc);
Run rn5 = new Run(doc);

rn1.Text = "Africa";
rn2.Text = "America";
rn3.Text = "Asia";
rn4.Text = "Antartica";
ph1.AppendChild(rn1); 
ph2.AppendChild(rn2);
ph3.AppendChild(new BookmarkStart(doc, "Hello_Bookmark"));
ph3.AppendChild(rn3);
ph3.AppendChild(new BookmarkEnd(doc, "Hello_Bookmark"));
ph4.AppendChild(rn4);

body.AppendChild(ph1);
body.AppendChild(ph2);
body.AppendChild(ph3);
body.AppendChild(ph4);

rn5.Text = "Some reference text here ";
ph5.AppendChild(rn5);
FieldRef fir = (FieldRef)ph5.AppendField(FieldType.FieldRef, true) ;
fir.BookmarkName = "Hello_Bookmark";

fir.InsertParagraphNumber = true;
fir.InsertHyperlink = true;
fir.IncludeNoteOrComment = true;
fir.Update(); 
body.AppendChild(ph5);
doc.Save("ListOutput.docx");

Here are my questions,

  1. In word, to achieve cross reference we may use Numbered Item/Heading/Bookmark/Endnote/Footnote/Equation but when using Fieldref class in Aspose words I could only see cross reference achieved through bookmark
  2. And if i choose IncludeNoteorComment instead of InsertParagraphNumber and if i change the label text in the list(ie. from America to Northamerica) the referenced value doesn’t get updated even we try to update manually.
  3. And finally, the output file generated using Aspose when edited, does we have any way to update all cross referred field without user input. If not can you suggest any possible alternatives on this regard.

Hi Manikandan,

Thanks for your inquiry.

*srimani:
Here are my questions,

  1. In word, to achieve cross reference we may use Numbered Item/Heading/Bookmark/Endnote/Footnote/Equation but when using Fieldref class in Aspose words I could only see cross reference achieved through bookmark*

Please create some cross references using MS Word and press Alt + F9 to see the field codes. MS Word creates the Ref field with bookmarks. The bookmarks are hidden and start with an underscore character (_).

You can create the same reference feild using Aspose.Words. E.g. To create a cross reference of type heading, you need to bookmark the heading text and use that bookmark’s name in reference field. MS Word does the same. Please check the hidden bookmarks in the document after creating the cross reference

srimani:
2. And if i choose IncludeNoteorComment instead of InsertParagraphNumber and if i change the label text in the list(ie. from America to Northamerica) the referenced value doesn’t get updated even we try to update manually.

In this case, the list number will not be change. If you want to get the updated text, please use FieldRef.InsertHyperlink.

FieldRef.InsertParagraphNumber property is used to get or set whether to insert the paragraph number of the referenced paragraph exactly as it appears in the document.

FieldRef.IncludeNoteOrComment property is used to gets or set whether to increment footnote, endnote, and annotation numbers that are marked by the bookmark, and insert the corresponding footnote, endnote, and comment text.

srimani:
3. And finally, the output file generated using Aspose when edited, does we have any way to update all cross referred field without user input. If not can you suggest any possible alternatives on this regard.

Yes, you can use the members of FieldRef class to update cross reference fields.