Limiting the no of pages in docx

Is there any best way to check the no of pages in docx ?

Is there any way to limit the no of pages to some numbers while adding contents programmatically in docx ? (Reaching the limit we have to throw an exception.)
Hi there,

Thank you for your inquiry. Just to let you know our support team have a short break early this week but rest assured your issue will be look into as soon as they return.

Thanks,

Hi Sharon,


Thanks for your inquiry. You can use Document.PageCount property to get the number of pages in the document as calculated by the most recent page layout operation.

Secondly, after inserting a document element such as Paragraph or Table, you can add a temporary PAGE field to check whether the max number of pages per document limit has reached or not. Please see the following code for example:
Document doc = new Document(“c:\temp\in.docx”);
DocumentBuilder builder = new DocumentBuilder(doc);

builder.writeln(“add some content”);

// Insert a PAGE field and update it.
Field page = builder.insertField(“PAGE”);
builder.getDocument().updatePageLayout();
page.update();

// Determine the current Page Number
int pageNumber = Integer.parseInt(page.getResult());

// Remove PAGE field.
page.remove();

doc.save(“c:\temp\out.docx”);

I hope, this helps.

Best regards,