Inserted Page Field is not removing in pdf save

Hi,

While I am processing an uploaded document, I want track the number of pages that document skipped from page number. For that i am tried to take the last page number using below code

DocumentBuilder builder = new DocumentBuilder(document);
    builder.moveToDocumentEnd();
    // Insert a PAGE field and update it.
    Field page = builder.insertField("PAGE");
    document.updatePageLayout();
    page.update();
    int lasPgNo = Integer.parseInt(page.getResult());
    // Remove PAGE field.
    page.remove();

I am getting the last page number and from above code. After I will save the document with word and pdf. In saved pdf document not removing the inserted page field and I can see page number at the end. I am attaching the input document, output document and sample code for the issue. Please help me to remove that unwanted page field from downloaded pdf document. Or please html me with any alternative solution to find number of skipped pages
PageFieldRemoveIssue.zip (200.4 KB)

Thank you

@Gptrnt You should call Document.updatePageLayout method after removing the field and before saving to PDF. When you call Document.updatePageLayout first time document layout is cached and changes made in the document does not take effect when save to Fixed Page formats.
Also, I think in your case you can use PageSutup.getPageStartingNumber to achieve what you need.

My code is working now. Thank you so much for your valuable help.