We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

removing the text found between 2 tags from the original document

Thread ID:





81936 May 25,2009 08:20 AM UTC May 28,2009 12:12 PM UTC WinForms 4
Tags: DocIO
Administrator [Syncfusion]
Asked On May 25, 2009 08:20 AM UTC


I've already asked this question in 2 posts below, but for some reason, the post doesn't open, it says error 400, webpage not found. I read the reply by Bhuvana, but I guess I didnt make myself clear, let me try to explain again. I am trying to find some text between 2 tags, this text can include anything like tables, bullets, etc etc. Now once I find the text between 2 tags, I am inserting that into a new document. Till here everything works fine, what I need to do is to DELETE THE CONTENT FROM THE ORIGINAL CONTENT THAT I FOUND, for e.g. in the below code, document is the original WordDocument from where I am finding the content, and inserting that to a new WordDocument called docMaster, now I want to delete the content that I found from the "document" WordDocument from the "document" itself, code is as below

private void Save()
WordDocument document = new WordDocument(Server.MapPath("~/test.doc"),FormatType.Doc);
WordDocument docMaster = new WordDocument(Server.MapPath("~/dummy.doc"),FormatType.Doc);

TextSelection SelBegin = document.Find("", true, true);
TextSelection SelEnd = document.Find("", true, true);

WParagraph paraStart = SelBegin.GetAsOneRange().OwnerParagraph;
SelBegin.GetAsOneRange().Text = SelBegin.GetAsOneRange().Text.Replace(SelBegin.GetAsOneRange().Text, String.Empty);

WParagraph paraEnd = SelEnd.GetAsOneRange().OwnerParagraph;
SelEnd.GetAsOneRange().Text = SelEnd.GetAsOneRange().Text.Replace(SelEnd.GetAsOneRange().Text, String.Empty);

TextBodyPart BodyPart = new TextBodyPart(document);
TextBodySelection BodySel = new TextBodySelection(paraStart.Items[0], paraEnd.Items[paraStart.Items.Count - 1]);


//I want to delete the contents of BodySel from the document WordDocument

document.Save(Server.MapPath("~/test.doc"), FormatType.Doc);
docMaster.Save(Server.MapPath("~/dummy.doc"), FormatType.Doc);

Please check the commented line in the code, i.e. remove the text that I found from the test.doc file.


Bhuvaneswari P [Syncfusion]
Replied On May 25, 2009 12:10 PM UTC

Hi Mark,

Thanks for the details.

Now I understand your requirements. Please refer the below code snippet to delete the contents of BodySel from the document WordDocument.

Entity en =document.LastSection.Paragraphs[document.LastSection.Paragraphs.IndexOf(paraStart)];
while (en.NextSibling != null)
if (en.EntityType == EntityType.Paragraph)
int k = document.Sections[0].Paragraphs.IndexOf(en as WParagraph);
en = en.NextSibling as Entity;

if (en.EntityType == EntityType.Table)
WTable table = en as WTable;

en = en.NextSibling as Entity;

Also, if you want to find and replace the the text alone you can use ReplaceSingleLine() method. This will not remove the table; it will remove the text and replace them with the given string. In your case you want to replace the Table also, this may not be helping you. However, you can use it, if it’s needed.

Regex s=new Regex(@".*", RegexOptions.Multiline);
document.ReplaceSingleLine(s ," ");
TextSelection[] sel= document.FindSingleLine(s);

Please try these methods and let us know if this helps you.

Best Regards,

Bhuvaneswari P [Syncfusion]
Replied On May 26, 2009 05:08 AM UTC

Hi Mark,

One more best way to remove the content between the find text is using the Bookmark concept.

Insert the bookmark for the paraStart and paraEnd and now remove the bookmark content and Bookmark. This will minimize the task to enumerate each item in the content and checking the elements type to delete from the document.

Insert bookmark between the find text
BookmarkStart s = new BookmarkStart(document, "begin");
paraStart.Items.Insert(0, s as Entity);
BookmarksNavigator na = new BookmarksNavigator(document);
//Remove the content
//Remove the bookmark

Please try this method also and let us know if this helps you.

Best Regards,

Administrator [Syncfusion]
Replied On May 27, 2009 02:37 PM UTC


I tried to use the Bookmark approach, it work, but again when I tried to replace the bookmarkcontent, it gave me an error that

Not supported deleting content between bookmarks in different paragraphs.

Code is simple as below:

BookmarksNavigator na = new BookmarksNavigator(document);

Bhuvaneswari P [Syncfusion]
Replied On May 28, 2009 12:12 PM UTC

Hi Mark,

Thanks for the update.

I am afraid that I am unable to reproduce the issue. It replaces the bookmark content without any exception. Please ensure the items are added in the TextBodyPart before passing to Bookmark ReplaceContent method.

Here is the sample for your reference:

Please have a look into the sample and try to reproduce the issue in it and send to us. This would be helpful for us to investigate the issue further.

Best Regards,


This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon