- Home
- Forum
- ASP.NET Web Forms (Classic)
- ReplaceSingleLine doesn’t replace embedded tables
ReplaceSingleLine doesn’t replace embedded tables
I have a document where I need to replace certain sections. The section are identified by a start tag and an end tag. Things work well unless there is a Table in one of the sections. All the text of the section is replaced but the Table structure remains. How can I get everything to be replaced? My code needs to be able to replace any kind of content between the start and end tag.
I start with this:
I expect to get this:
But I get this:
Here is my code:
WordDocument document = new WordDocument(Server.MapPath("Template.docx"), FormatType.Docx);document.ReplaceSingleLine(new Regex(@"\[TableStart\].*?\[TableEnd\]"), "");document.Save("Sample.docx", FormatType.Docx, HttpContext.Current.Response, HttpContentDisposition.Attachment);document.Close();
Docx Files attached.
Attachment: Files_e0b2814.zip
Please refer to the below code snippet.
|
WordDocument document = new WordDocument(@"Template.docx", FormatType.Docx);
String[] bookmarkNames = { "Table1","Table2","Table3"};
foreach (string bookmarkName in bookmarkNames)
{
//Creates the bookmark navigator instance to access the bookmark
BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document);
//Moves the virtual cursor to the location before the end of the bookmark
bookmarkNavigator.MoveToBookmark(bookmarkName);
//Create a empty text body part.
TextBodyPart textBodyPart = new TextBodyPart(document);
//Replaces the bookmark content with text body part
bookmarkNavigator.ReplaceBookmarkContent(textBodyPart);
}
document.Save("Result.docx", FormatType.Docx);
document.Close(); |
We have modified your input template document by inserting bookmark start and bookmark end to replace the content. Please refer to the modified document in below link.
https://www.syncfusion.com/downloads/support/directtrac/general/doc/Template891176955.docx
Please refer to the below UG documentation to know how to work with bookmarks using DocIO.
https://help.syncfusion.com/file-formats/docio/working-with-bookmarks#replacing-content-in-a-bookmark
https://help.syncfusion.com/file-formats/docio/working-with-bookmarks
Regards,
Bookmarks are not very useful in my real-world use case because I need to be able to see and edit them in the document. Word does not make this easy to do. However, I do see the value of using them to accomplish removing the content between tags which includes tables. I will try to incorporate Bookmarks by automatically inserting them into the document where my tags are while I am processing the document.
Thanks
Thank you for your update.
Please let us know if you have any other questions and we will be happy to assist you always.
Regards,
- 3 Replies
- 2 Participants
-
CR Chris Reddick
- Feb 3, 2022 10:40 PM UTC
- Feb 7, 2022 10:52 AM UTC