We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

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