Delete Pages from Word Document

Is it posible to delete pages From To specific text? Lets say I have a document with
Text: BEGIN on top and in another page I have another Text: END…
can I delete all the pages in between from BEGIN TO END? Please Advise

Hi
Thanks for your inquiry. MS Word document is flow document and does not contain any information about its layout into lines and pages. So there is no direct way to determine where page starts or ends using Aspose.Words.

Anyhow, you can extract Word document page range to save in another Word document. Please follow up the link where my colleague shared a workaround.

https://forum.aspose.com/t/52186

Hope this will help. In case of any ambiguity, please let me know.

Let me give you another scenario… I have a document where I need to find the first section break of the document or maybe a marker and delete everyting from that point to end of the document…

Hi,

Thanks for your inquiry. You can remove everything between two bookmarks like Start and End. Please follow up the code snippet:

Document doc = new Document(@"Test138\in.doc");

// Get start node
Node startNode = doc.Range.Bookmarks["start"].BookmarkEnd;
// Get End node
Node endNode = doc.Range.Bookmarks["end"].BookmarkStart;

Node curNode = startNode.NextPreOrder(doc);
while (curNode != null)
{
    if (curNode.Equals(endNode))
        break;

    // move to next node
    Node nextNode = curNode.NextPreOrder(doc);
    // Check whether current contains end node
    if (curNode.IsComposite)
    {
        if (!(curNode as CompositeNode).ChildNodes.Contains(endNode) &&
            !(curNode as CompositeNode).ChildNodes.Contains(startNode))
        {
            nextNode = curNode.NextSibling;
            curNode.Remove();
        }
    }
    else
    {
        curNode.Remove();
    }
    curNode = nextNode;
}

// Save output document
doc.Save(@"Test138\out.doc");

Hope this will help. In case of any ambiguity, please let me know.

Thanks for your respose… The approach did not work for me… I am attaching the document I want to process called TestDelete.docx
Notice how there are two bookmarks one at the begining named: begin (under text :BEGINDELETE) and one at the end named: end ( under text: ENDDELETE).
I want to be able to delete everything between both bookmarks including text, tables etc. what I will be left with is only one page with the top section before the BEGINDELETE bookmark.
Can you please try it and let me know what I am doing wrong?
Thank You very much for all of your help

Hi,

Thanks for your inquiry. While using latest Aspose.Words 10.8 (.net 2.0) along with provided input Word document, i observe two bookmarks named as “begin” and “end”. I have attached input/output documents.

Document doc = new Document("d:/temp/TestDelete.docx");
// Get start node
Node startNode = doc.Range.Bookmarks["begin"].BookmarkEnd;
// Note: You may using start bookmark
// Node startNode = doc.Range.Bookmarks["start"].BookmarkEnd;
// Get End node
Node endNode = doc.Range.Bookmarks["end"].BookmarkStart;
Node curNode = startNode.NextPreOrder(doc);
while (curNode != null)
{
    if (curNode.Equals(endNode))
        break;
    // move to next node
    Node nextNode = curNode.NextPreOrder(doc);
    // Check whether current contains end node
    if (curNode.IsComposite)
    {
        if (!(curNode as CompositeNode).ChildNodes.Contains(endNode) &&
            !(curNode as CompositeNode).ChildNodes.Contains(startNode))
        {
            nextNode = curNode.NextSibling;
            curNode.Remove();
        }
    }
    else
    {
        curNode.Remove();
    }
    curNode = nextNode;
}
// Save output document
doc.Save("d:/temp/TestDeleteOut.docx");

In case of any ambiguity, please share further states like Aspose.Words version, .NET Framework etc.

This is very helpful… if I use this approach… can I delete the text used for the bookmark as well? if so how?Thanks

Hi

Thanks for your inquiry. Please follow up the code snippet to remove bookmark text as well.

Before saving the document:

DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToBookmark("begin");
builder.CurrentNode.Remove();
builder.MoveToBookmark("end");
builder.CurrentNode.Remove();
doc.UpdatePageLayout();

// Save output document
doc.Save("d:/temp/TestDeleteOut.docx");

In case of any ambiguity, please let me know.

The issues you have found earlier (filed as WORDSNET-2978) have been fixed in this .NET update and this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(50)