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.
Unfortunately, activation email could not send to your email. Please try again.

removing the text found between 2 tags from the original document

Thread ID:

Created:

Updated:

Platform:

Replies:

81936 May 25,2009 04:20 AM May 28,2009 08:12 AM Windows Forms 4
loading
Tags: DocIO
Administrator [Syncfusion]
Asked On May 25, 2009 04:20 AM

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

Bhuvaneswari P [Syncfusion]
Replied On May 25, 2009 08:10 AM

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

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

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

Administrator [Syncfusion]
Replied On May 27, 2009 10:37 AM

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

Bhuvaneswari P [Syncfusion]
Replied On May 28, 2009 08:12 AM

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

CONFIRMATION

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.

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.

;