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: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Selecting a TextBodyPart using Find

Thread ID:

Created:

Updated:

Platform:

Replies:

147368 Sep 7,2019 09:54 PM UTC Sep 13,2019 10:34 AM UTC ASP.NET Core - EJ 2 5
loading
Tags: DocIO
Pieter van Kampen
Asked On September 7, 2019 09:54 PM UTC

In a document with multiple paragraphs and tables, I would like to place some markers, and then find and replace them.

I saw I can do that with bookmarks:

[This is my Text
some more text
Header1Header2Header3
abc
def

more text]


Here [ and ] represent the start of the bookmark. I can find the table in the bookmark, like so:

            if (string.IsNullOrEmpty(bookmarkName) || _docIo.Document.Bookmarks.FindByName(bookmarkName) == null) return;
         //Move to the virtual cursor before the bookmark end location of the bookmark 
         BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(_docIo.Document);
         bookmarkNavigator.MoveToBookmark(bookmarkName);
         //Gets the content for the current bookmark
         TextBodyPart textBodyPart = bookmarkNavigator.GetBookmarkContent();
         WTable table = textBodyPart.BodyItems.OfType<WTable>().FirstOrDefault();


But my customer find it difficult to work with this, as the bookmarks are not visible by default in Word. Even when they are visible, it remains a bit tricky to keep them in the right place when the customer changes the text in the template.
So I am considering to change this to visual markers:

<mytable>This is my Text
some more text
Header1Header2Header3
abc
def
more text</mytable>

Is it possible to get the TextBodyPart including the start and end marker? If I have a TextBodyPart, I can delete the markers, and find the table, and manipulate the content.
I tried using a regex with Find, but did not find a way to select this:

       TextSelection selection = Document.Find(new Regex($"<mytable>.*</mytable>",RegexOptions.Singleline));

This is not working, I read somewhere that this does not find across entities.

Poorani Rajendran [Syncfusion]
Replied On September 9, 2019 12:10 PM UTC

Hi Pieter,

Thank you for contacting Syncfusion support.

To meet your requirement, we suggest you insert placeholder text (“<mytable>” and “</mytable>”) in the document and using Find API gets the text selection and gets the owner paragraph and table from the text selection to manipulate in the Word document. We have prepared the sample application for the same and it can be downloaded from the below link:
https://www.syncfusion.com/downloads/support/forum/147368/ze/CreateWordSample705469038

Note: Please find the input Word document in the “Data” folder of above sample application.

Please let us know if you have any other questions.

Regards,
Poorani Rajendran


Pieter van Kampen
Replied On September 9, 2019 08:15 PM UTC

Thank you so much, that really helps!

Depending on the situation, when the text is not applicable, the text between the start and end marker needs to be completely deleted. How would you do that in this way? In that case, I do not know the number of entities between the start and end marker.

Regards,

Pieter               

Poorani Rajendran [Syncfusion]
Replied On September 10, 2019 08:09 PM UTC

Hi Pieter,

Thank you for your update.

To meet your requirement, we have prepared a sample application and it can be downloaded from the below link.
https://www.syncfusion.com/downloads/support/forum/147368/ze/CreateWordSample-1841755863

Note: Please find the input Word document in the “Data” folder of above sample application.

In the sample application we have done the following things,
1.Loads an existing input Word document.
2.Find the particular text and gets the index of the text ranges.
3.Delete the start and end marker when it doesn’t have any text ranges.
4.Saves and close the Word document instances.

Please refer the below UG documentation link to know more about working with find and replace functionality in Word document using DocIO:
https://help.syncfusion.com/file-formats/docio/working-with-find-and-replace

Please let us know if you have any other questions.

Regards,
Poorani Rajendran
 


Pieter van Kampen
Replied On September 13, 2019 10:13 AM UTC

Thank you so much, this is exactly what I need.

Poorani Rajendran [Syncfusion]
Replied On September 13, 2019 10:34 AM UTC

Hi Pieter,

Thank you for your update.

We are glad to hear that our solution has resolved your requirement.


Regards,
Poorani Rajendran
 


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.

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

Live Chat Icon For mobile
Live Chat Icon