Remove a Specific Word Document Page by Index or Page Number - Java

Hi Team,

In Aspose, How to remove page from word document using page number using aspose?

for eg: Word document contains four page; i need to remove specific page as 3 or 4 from document.

Thanks & Regards,
Shyamala B

@Shyamu,

You can build logic on the following code of Aspose.Words for Java API to remove any specific page from Word document:

Document doc = new Document("C:\\Temp\\remove a page from Word.docx");

Document finalDocument = (Document) doc.deepClone(false);
finalDocument.removeAllChildren();

int totalPages = doc.getPageCount();
int pageToRemove = 1;

if (pageToRemove == 1) {
    Document lastRegion = doc.extractPages(pageToRemove, totalPages - pageToRemove);
    finalDocument.appendDocument(lastRegion, ImportFormatMode.KEEP_SOURCE_FORMATTING);
} else if (pageToRemove == totalPages) {
    Document firstRegion = doc.extractPages(0, pageToRemove - 1);
    finalDocument.appendDocument(firstRegion, ImportFormatMode.KEEP_SOURCE_FORMATTING);
} else {
    Document firstRegion = doc.extractPages(0, pageToRemove - 1);
    Document lastRegion = doc.extractPages(pageToRemove, totalPages - pageToRemove);

    finalDocument.appendDocument(firstRegion, ImportFormatMode.KEEP_SOURCE_FORMATTING);
    finalDocument.appendDocument(lastRegion, ImportFormatMode.KEEP_SOURCE_FORMATTING);
}

finalDocument.save("C:\\Temp\\awjava-21.5.docx");

Thanks awais !!

Shared solution which helps to complete my requirement as expected

1 Like

Hi Team,

Using this post solution code to remove a specific word document page, iā€™m getting incorrect page number in footer section after removed Page 3

Please use an attached input document and generate a final document
Input Document : Page 1 - Portrait Orientation, Page 2 - Landscape Orientation, Page 3 - Portrait Orientation.
Requirement - Remove Last Page as ā€œ3ā€.
Footer Page number format : Page {current page num} of {total num of pages}

Test_Word.docx (21.2 KB)

Kindly provide a solution for this issue

Thanks in Advance !!

@Shyamu I have logged this issue in our issue tracking system as WORDSNET- 24508. You will be notified via this forum thread once this issue has been resolved.

Hi Vadim,

Is this issue resolved?

@Shyamu Yes, the issue is already resolved. The fix will be included into the next 23.1 version of Aspose.Words for Java. We will be sure to notify you once it is available.

The issues you have found earlier (filed as WORDSNET-24508) have been fixed in this Aspose.Words for Java 23.1 update.