Free Support Forum - aspose.com

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.


http://www.aspose.com/community/forums/349710/extract-text-for-a-specific-page-in-word-file/showthread.aspx#349710


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”);<o:p></o:p>

<o:p> </o:p>

//Get start node<o:p></o:p>

Node startNode = doc.Range.Bookmarks[“start”].BookmarkEnd;<o:p></o:p>

//Get End node<o:p></o:p>

Node endNode = doc.Range.Bookmarks[“end”].BookmarkStart;<o:p></o:p>

<o:p> </o:p>

Node curNode = startNode.NextPreOrder(doc);<o:p></o:p>

while (curNode!=null)<o:p></o:p>

{<o:p></o:p>

if (curNode.Equals(endNode))<o:p></o:p>

break;<o:p></o:p>

<o:p> </o:p>

//move to next node<o:p></o:p>

Node nextNode = curNode.NextPreOrder(doc);<o:p></o:p>

//Check whether current contains end node<o:p></o:p>

if (curNode.IsComposite)<o:p></o:p>

{<o:p></o:p>

if (!(curNode as CompositeNode).ChildNodes.Contains(endNode) &&<o:p></o:p>

!(curNode as CompositeNode).ChildNodes.Contains(startNode))<o:p></o:p>

{<o:p></o:p>

nextNode = curNode.NextSibling;<o:p></o:p>

curNode.Remove();<o:p></o:p>

}<o:p></o:p>

}<o:p></o:p>

else<o:p></o:p>

{<o:p></o:p>

curNode.Remove();<o:p></o:p>

}<o:p></o:p>

curNode = nextNode;<o:p></o:p>

}<o:p></o:p>

<o:p> </o:p>

//Save output document<o:p></o:p>

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”);<o:p></o:p>

//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);<o:p></o:p>

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)