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

Free Support Forum - aspose.com

Delete a page from word document with specific word

Hi,
What is the best way to delete a page contains specific word using aspose.word 18.1.
e.g. If My Word document have Title: word on first page i would like to delete the page.

@SanjayBhujbal,

Thanks for your inquiry. Please note that MS Word document is flow document and does not contain any information about its layout into lines and pages. So unfortunately, there is no direct way to delete a particular page from a document. However, if each page in your document is a separate section, then you can just remove the section e.g: doc.Sections[4].Remove();

Alternatively, you can create a separate Section for each page in word document and then remove the section corresponding to a particular page. Please find attached ‘util.txt’ (util.zip (4.8 KB)) and try running the following code:

Document doc = new Document("D:\\temp\\input.docx");

LayoutCollector collector = new LayoutCollector(doc);
int pageNumber = -1;
foreach(Run run in doc.GetChildNodes(NodeType.Run, true))
{
    if (run.Text.Contains("Title:"))
    {
        pageNumber = collector.GetStartPageIndex(run);
    }
}

if (pageNumber > 0)
{
    Document finalDoc = (Document)doc.Clone(false);
    finalDoc.RemoveAllChildren();

    DocumentPageSplitter splitter = new DocumentPageSplitter(doc);

    for (int page = 1; page <= doc.PageCount; page++)
    {
        Document pageDoc = splitter.GetDocumentOfPage(page);
        if (page != pageNumber)
            finalDoc.AppendDocument(pageDoc, ImportFormatMode.KeepSourceFormatting);
    }

    finalDoc.Save("D:\\Temp\\18.6.docx");
}

Thank you, This is very helpful.