We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Does Document.updateFields() or Field.update() work with REF or PAGEREF fields

I am using Java 19.2 on OSX. When I call either of the subject functions immediately before Document.save() the values of my REF and PAGEREF fields are not updated. If I open this document in MS Word and then print preview the fields are then correctly populated. What is the correct way to ensure that REF and PAGEREF fields are updated with the referenced text or page number before saving?

@neallester,

Please ZIP and upload your input document, Aspose.Words generated DOCX file showing the undesired behavior and piece of source code here for testing. We will then investigate the issue on our end and provide you more information.

My first try at reproducing the issue I am having with a simple code sample and document failed (that is, I didn’t reproduce the problem). I will update when I am able to either reproduce the issue or determine the bug in my code which was producing this apparent behavior.

@neallester,

Sure, we will wait for your further input on this topic. Please also upgrade to the latest version of Aspose.Words for Java i.e. 19.3 and see if you still observe the same problem on your end?

I upgraded to 19.3; no difference in behavior.

I determined why the REF field was not updating correctly; I had used the same bookmark twice and REF field update() apparently chose a different bookmark than MS Word.

However, the PAGEREF is still not updating correctly. While I haven’t yet reproduced this behavior in a simple test document, I have found another circumstance where the REF updates but the PAGEREF doesn’t:

		ClassLoader classLoader = getClass().getClassLoader();
		License license = new License();
		license.setLicense(classLoader.getResourceAsStream("Aspose.Words.lic"));
		String fileName = classLoader.getResource("<a class="attachment" href="/uploads/discourse_instance3/24950">fields.zip</a> (24.2 KB)
template.docx").toString().substring(5);
		com.aspose.words.Document document = new com.aspose.words.Document(fileName);
		document.removeAllChildren();
		String bookmarkName = "aBookmark";
		com.aspose.words.DocumentBuilder builder = new DocumentBuilder(document);
		builder.insertParagraph();
		builder.write ("before f1|");
		Field f1 = builder.insertField("REF " + bookmarkName + " \\* Charformat \\h");
		builder.write ("|after f1");
		Section section = new Section (builder.getDocument());
		builder.getDocument().appendChild(section);
		Body body = new Body (builder.getDocument());
		section.appendChild(body);
		builder.moveToDocumentEnd();

		builder.write ("before f2|");
		Field f2 = builder.insertField("PAGEREF " + bookmarkName + " \\* Charformat \\h");
		builder.write ("|after f2");
		builder.insertParagraph();
		builder.startBookmark(bookmarkName);
		builder.write("This text is bookmarked");
		builder.endBookmark(bookmarkName);
		f1.update();
		f2.update();
		document.updateFields();
		document.save("fields.docx");

When I open the document in MS Word the REF field displays the correct value, but the PAGEREF field displays “Error! Bookmark not defined.”. After right clicking and selecting “Update Field” the correct page number (2) is shown in the document.

I’ve attached the template and output document.fields.zip (24.2 KB)

@neallester,

For this case, please use the following overload of insertField method:

builder.insertField("REF " + bookmarkName + " \\* Charformat \\h", null)

Complete code is as follows:

Document document = new Document("E:\\fields\\fields.docx");

document.removeAllChildren();
String bookmarkName = "aBookmark";
com.aspose.words.DocumentBuilder builder = new DocumentBuilder(document);
builder.insertParagraph();
builder.write ("before f1|");
Field f1 = builder.insertField("REF " + bookmarkName + " \\* Charformat \\h", null);
builder.write ("|after f1");
Section section = new Section (builder.getDocument());
builder.getDocument().appendChild(section);
Body body = new Body (builder.getDocument());
section.appendChild(body);
builder.moveToDocumentEnd();

builder.write ("before f2|");
Field f2 = builder.insertField("PAGEREF " + bookmarkName + " \\* Charformat \\h", null);
builder.write ("|after f2");
builder.insertParagraph();
builder.startBookmark(bookmarkName);
builder.write("This text is bookmarked");
builder.endBookmark(bookmarkName);
f1.update();
f2.update();
document.updateFields();

document.save("E:\\fields\\awjava-19.3-null.docx");

Hope, this helps.

Yes, this workaround solved the specific case I reported here and the problem I was unable to duplicate precisely.

Thank you.

@neallester,

Thanks for your feedback. Please let us know anytime you have any further queries.