Free Support Forum -

Determine number of pages and delete last page


We are creating a dynamic document where the line items are populated in the word document

1) Is there a way we can know how many pages are created in the final document when the lines items are populated on it from the database.

2) If we want to delete the last page is it possible?

3) Appreciate if you could send me the code to delete the last page.

I am not a technical person and cannot provide you with a code sample. Appreciate if you could answer the above questions.

Hi Manish,

Thanks for your inquiry.

1) You can use Document.PageCount Property to get the number of pages in the document as calculated by the most recent page layout operation.

2) I am afraid, there is no concept Page in MS Word document. Pages are created on the fly when you open a Word document with MS Word. However, you can try using the following code to remove all nodes from last Page:

Document doc = new Document(MyDir + @“in.docx”);

LayoutCollector layout = new LayoutCollector(doc);

int pageCount = doc.PageCount;

NodeCollection nodes = doc.GetChildNodes(NodeType.Any, true);

Node lastNode = nodes[nodes.Count - 1];

Node currentNode = lastNode;

bool isRemoving = true;

while (currentNode != null && isRemoving)


if (layout.GetEndPageIndex(currentNode) == (pageCount - 1))

isRemoving = false;

Node prevNode = currentNode.PreviousPreOrder(currentNode.Document);


currentNode = prevNode;


doc.Save(MyDir + @"17.1.0.docx");

Best regards,


Aboove code does not work for us as we have Table Borders since the Rows are generated Dynamically . WE cannot use Page Borders.

Please check our issue and sample code at

Also for your reference we have sent the Standalone Application with this issue also in below ticket . Please check it :


Please follow this thread for further proceedings.

Best regards,