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

Selecting a TextBodyPart using Find

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.

5 Replies

PR Poorani Rajendran Syncfusion Team 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



PV Pieter van Kampen 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               


PR Poorani Rajendran Syncfusion Team 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
 



PV Pieter van Kampen September 13, 2019 10:13 AM UTC

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


PR Poorani Rajendran Syncfusion Team 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
 


Loader.
Up arrow icon