Hello,
we’ve encountered a problem when one updateFields() method call is not enough for Aspose to correctly calculate page numbers in Page Reference fields (references to bookmarks).
When there is only one updateFields call, page references in Word display ‘3’ as a page number. If you right click -> update field on the reference, it becomes ‘1’ (the correct result). If second updateFields() call is being made, everything works correctly.
Our guess is that when calculating page number during the first updateField() call, page reference has e.g. value of “Error! Bookmark not define.” taking 4 rows to display due to narrow cell, therefore actual bookmark does occur on page 3. However, there error is incorrect, and bookmark exists on page 1.
Aspose Word version 20.7
@Test
public void example() throws Exception {
final String bookmarkName = "Boorkmark123";
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Table table = builder.startTable();
builder.insertCell();
table.setPreferredWidth(PreferredWidth.fromPercent(5));
IntStream.rangeClosed(1, 25)
.forEach(i -> addPageRef(builder, bookmarkName));
builder.endTable();
builder.startBookmark(bookmarkName);
builder.write("This is bookmark text");
builder.endBookmark(bookmarkName);
doc.updatePageLayout();
doc.updateFields();
//doc.updateFields();
doc.save("/path-to-doc");
}
private static void addPageRef(DocumentBuilder builder, String bookmarkName) {
try {
FieldPageRef field = (FieldPageRef) builder.insertField(FieldType.FIELD_PAGE_REF, true);
field.setBookmarkName(bookmarkName);
builder.insertBreak(BreakType.LINE_BREAK);
} catch (Exception e) { }
}