Document.UpdateFields does not update Page References field using Java

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) { }
      }

@vdulko

Please use the following code example to get the desired output. You need to insert the bookmark first and then insert the PageRef field. Hope this helps you.

final String bookmarkName = "Boorkmark123";

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.startBookmark(bookmarkName);
builder.write("This is bookmark text");
builder.endBookmark(bookmarkName);

//builder.moveToDocumentStart();
Table table = builder.startTable();
builder.insertCell();
table.setPreferredWidth(PreferredWidth.fromPercent(5));

IntStream.rangeClosed(1, 25)
        .forEach(i -> addPageRef(builder, bookmarkName));

builder.endTable();

doc.updatePageLayout();
doc.updateFields();
doc.save(MyDir + "output.docx");

@tahir.manzoor I’m not sure this is the suitable solution. We do have an internal model which is being converted to Aspose model, and PageRefs do need to occur before the bookmark in Word output (a kind of TOC, for example).

@vdulko

We have logged this problem in our issue tracking system as WORDSNET-20834 . You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

1 Like

@vdulko

It is to inform you that the issue which you are facing is actually not a bug in Aspose.Words. So, we have closed this issue (WORDSNET-20834) as ‘Not a Bug’.

Please pass second parameter of DocumentBuilder.insertField as false to get the desired output.

private static void addPageRef(DocumentBuilder builder, String bookmarkName) {
    try {
        FieldPageRef field = (FieldPageRef) builder.insertField(FieldType.FIELD_PAGE_REF, false);
        field.setBookmarkName(bookmarkName);
        builder.insertBreak(BreakType.LINE_BREAK);
    } catch (Exception e) { }
}