Remove Pages from Word using C#

This article describes how to delete page in Word using C#. It shares details for setting up the IDE, provides a step-by-step procedure, and sample code demonstrating how to remove the page in Word using C#. You will get suggestions to enhance the sample code for creating custom output.

Steps to Delete Page in Word using C#

  1. Set up the environment to use Aspose.Words for .NET to delete a page
  2. Load the DOCX file into memory using the Document object
  3. Define the page number to remove, count the existing pages, and confirm that the target page exists
  4. Stop the process if the document has only one page, as it cannot be deleted
  5. If the first page is selected for deletion, keep only the following pages
  6. Otherwise, copy all the pages before the target page into a new document
  7. Copy the remaining pages after the target page, if any, to another document
  8. Join the two documents without adding any extra page break
  9. Save the output Word file

The given steps summarize how to remove a page from a document in Word using C#. Load the source Word file containing the page to delete, count the pages, confirm the target page’s presence, and apply checks to avoid deleting the wrong page. Copy all the pages before the target page into one document and put the remaining pages after the target page into another document. Finally, create the resultant Word file by joining both parts.

Code to Delete a Page from MS Word using C#

This code demonstrates how to delete a page in a Word DOC using C#. If you want to continue page numbers instead of restarting while joining the documents, turn off page number updating in the page extraction options. Similarly, keep the total-pages fields live so headers and footers show the correct total page count after joining the documents.

This article shows the process to remove pages from a Word file. To insert content controls in a Word file, refer to the article Insert Content Control in Word using C#.