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. Image for the cookie policy date
close icon

removing the text found between 2 tags from the original document

Hi

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]);

BodyPart.Copy(BodySel);
BodyPart.PasteAfter(docMaster.Sections[0].Paragraphs[0]);

//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.


Thanks
Mark

4 Replies

BP Bhuvaneswari P Syncfusion Team 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;
document.Sections[0].Paragraphs.RemoveAt(k);
}

if (en.EntityType == EntityType.Table)
{
WTable table = en as WTable;
table.ResetCells(0,0);


}
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,
Bhuvana


BP Bhuvaneswari P Syncfusion Team 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);
paraEnd.AppendBookmarkEnd("begin");
BookmarksNavigator na = new BookmarksNavigator(document);
na.MoveToBookmark("begin");
//Remove the content
na.DeleteBookmarkContent(false);
//Remove the bookmark
document.Bookmarks.Remove(na.CurrentBookmark);

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

Best Regards,
Bhuvana


AD Administrator Syncfusion Team May 27, 2009 02:37 PM UTC

Hi

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);
na.MoveToBookmark(ReasonsTag);
na.ReplaceBookmarkContent(tx);


BP Bhuvaneswari P Syncfusion Team 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:
http://files.syncfusion.com/samples/DocIO.Windows/DocIO_81936.zip

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,
Bhuvana

Loader.
Live Chat Icon For mobile
Up arrow icon