We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Delete the text between two tags and keep the tags using Java

Step1: Trying to delete start and end tag with content. https://forum.aspose.com/t/trying-to-delete-context-of-some-tags-using-aspose-words/213621/8 as suggested in link.

Step2: Trying to insert same tag single or muliple time based on dynamic count inside document in same location which is not possible currently im trying to find bookmark added in previous step and inserted in tags but in is inserted in start of document.

please refer sample project Source.zip (79.0 KB)

@saranyasrinivasan92

We already shared the code example with you in your other thread. Please check the following code example. This code example does not remove the inserted bookmarks from the document. You can use these bookmarks to insert the content. You just need to move the cursor to the bookmark and insert the desired content. We suggest you please read the following articles.
Navigation with Cursor
Working with Bookmarks

string StartTag = @"$abcstart{";
string EndTag = @"}abcend$";
Document doc = new Document(MyDir + "RegaxInputFile.docx"); 
FindReplaceOptions options = new FindReplaceOptions();
options.ReplacingCallback = new FindAndInsertBookmark("bookmark", true);
options.Direction = FindReplaceDirection.Backward;
options.MatchCase = false;
doc.Range.Replace(StartTag, "", options);

options.ReplacingCallback = new FindAndInsertBookmark("bookmark", false);
doc.Range.Replace(EndTag, "", options);

doc.UpdatePageLayout();
foreach (Bookmark bookmark in doc.Range.Bookmarks)
    bookmark.Text = "";

doc.Save(MyDir + "20.6.docx");

Please check attchemnt I am finding bookmark and replacing but it is inserting on top of document. tags inserted order also on reverse. Please helpInsertusingBookMark.zip (71.1 KB)

@saranyasrinivasan92

In your code example, you are inserting the start and end tag again into document. We suggest you please do not delete the tags while deleting the content between them. To achieve this, you need to insert BookmarkStart node after start tag and BookmarkEnd node before end tag.

I have to achieve three case :
Input : I will get dynamic tag count and one input document
Example
For Tag ABC dynamic count is 2
Finding in input document $abcstart{ }abcend$$$ and get foundcount
case 1: if dynamiccount and found count is same no need to delete tags and i can proceed deleting insert tags and rewrite with someother text
case 2: if dynamiccount > foundcount i have to insert tags based on dynamiccount
case 3: if dynamiccount < foundcount i have to delete tags based on dynamiccount

Please can you suggest with sample code?

@saranyasrinivasan92

Please ZIP and attach your input and expected output documents for all three cases. We will investigate the cases and guide your according to your requirement.

Case 1:
if dynamiccount and found count is same no need to delete tags and i can proceed deleting insert tags and rewrite with someother text
So Give input tagcount as 4 then will get same count in document so just to delete tags data Case1.zip (30.3 KB)

Case 2: if dynamiccount > foundcount i have to insert tags based on dynamiccount
So Give input tagcount as 5 then will get lesser count in document so just to Insert tags data Case2.zip (30.3 KB)

Case 3: if dynamiccount > foundcount i have to insert tags based on dynamiccount
So Give input tagcount as 3 then will get greater count in document so just to delete tags data Case3.zip (30.3 KB)

Sample code : Source.zip (40.3 KB)

Please suggest with better solution

@saranyasrinivasan92

Please use the same code to find and insert bookmark and delete the content. In this case, please insert the BookmarkStart Node after the start tag and insert BookmarkEnd node before the end tag.

In this case, please use the same approach of ‘Case 1’. After deleting the content between tags, move the cursor to desired location and insert new tags according to dynamic count.

I think, it is if dynamiccount < foundcount.

You can use the same approach of ‘Case 1’. You need to to use Range.Replace method to replace the tags with empty string. This will remove the tags from the document. If you want to add some tags, please move the cursor to desired location and add number of tags according to your requirement.