Our “old” code is with Words 15.2, now trying out the newest verison (20.10).
However, the function
RemoveBookmarkWithContent
previously found in aspose support (I I recall right) is now failing.
Is there a newer / better way to remove a bookmark, including its content, which possibley can be nested tables, paragraphs ect and NOT just a simple text), or is there an updated version of this function ?
@tokebreer2,
You can remove a Bookmark from Word document along with the Bookmarked content by using the following C# code of Aspose.Words for .NET:
Document doc = new Document("C:\\temp\\input.docx");
Bookmark bookmark = doc.Range.Bookmarks["bm"];
bookmark.Text = "";
bookmark.Remove();
doc.Save("C:\\temp\\output.docx");
Hello @awais.hafeez If I select only table then why its showing an empty cell Ideally it should not be there right…?
Like it should remove even that
@Raghul214 Could you please elaborate your problem in more details and attach your input, output and expected output documents along with code that will allow us to reproduce the problem on our side? We will check the issue and provide you more information.
Output_Document.docx (11.1 KB)
This is my output Document
BaseDocument.docx (15.8 KB)
This is my base document why an empty cell is there in the output Ideally that should also be deleted right
@Raghul214 The problem occurs because bookmark starts inside table cell and ends after the table:
You can use the following code to remove the bookmarked table:
Document doc = new Document(@"C:\Temp\in.docx");
Bookmark bk = doc.Range.Bookmarks["YourBookmarkName"];
bk.BookmarkStart.GetAncestor(NodeType.Table).Remove();
doc.Save(@"C:\Temp\out.docx");
Ok if it start inside table cell and end inside table cell what will happen
@Raghul214 The above code will work the same, because it simply removes the parent table of the bookmark start.
The following code will also work the same, i.e. an empty cell will remain the document:
Document doc = new Document(@"C:\Temp\in.docx");
Bookmark bk = doc.Range.Bookmarks["YourBookmarkName"];
bk.Text = "";
doc.Save(@"C:\Temp\out.docx");
in.docx (15.0 KB)
out.docx (11.2 KB)
1 Like
Hello @alexey.noskov I am using
baseBookmark.Text = string.Empty;
builder.MoveToBookmark(baseBookmark.Name, true, true);
var extractedNodes = ExtractContentHelper.ExtractContent(bookmark.BookmarkStart, bookmark.BookmarkEnd, true);
var extractedContent = ExtractContentHelper.GenerateDocument(generatedDoc, extractedNodes);
builder.InsertDocument(extractedContent, ImportFormatMode.UseDestinationStyles);
bookmark variable is from another document I am trying to replace one content with another bookmark content
for following code
<<if [Code == "abc"]>>
Static Content Check --- I added bookmark here
<</if>>
Static Content Check added - latest bookamrk
Output is
<<if [Code == "abc"]>>
<</if>>Static Content Check added - latest bookamrk
@Raghul214 Could you please attach your sample input, output and expected output documents? Unfortunately, it is not quite clear from the above post what the problem is.
There is a possibility it can contain any other content like image thats why i user remove
@Raghul214 Thank you for additional information. I cannot reproduce the problem using the following simple code:
string bookmarkName = "BM4";
Document dst = new Document(@"C:\Temp\Base_Template.docx");
DocumentBuilder builder = new DocumentBuilder(dst);
Document src = new Document(@"C:\Temp\Doc_2.docx");
Bookmark bookmark = src.Range.Bookmarks[bookmarkName];
var extractedNodes = ExtractContentHelper.ExtractContent(bookmark.BookmarkStart, bookmark.BookmarkEnd, true);
var extractedContent = ExtractContentHelper.GenerateDocument(src, extractedNodes);
// Remove old content from the destination document.
dst.Range.Bookmarks[bookmarkName].Text = "";
builder.MoveToBookmark(bookmarkName, true, true);
builder.InsertDocumentInline(extractedContent, ImportFormatMode.UseDestinationStyles, new ImportFormatOptions());
dst.Save(@"C:\Temp\out.docx");
This won’t work with table, image and all…
string bookmarkName = "BM4";
Document dst = new Document(@"C:\Temp\Base_Template.docx");
DocumentBuilder builder = new DocumentBuilder(dst);
Document src = new Document(@"C:\Temp\Doc_2.docx");
Bookmark bookmark = src.Range.Bookmarks[bookmarkName];
var extractedNodes = ExtractContentHelper.ExtractContent(bookmark.BookmarkStart, bookmark.BookmarkEnd, true);
var extractedContent = ExtractContentHelper.GenerateDocument(src, extractedNodes);
// Remove old content from the destination document.
dst.Range.Bookmarks[bookmarkName].Text = "";
builder.MoveToBookmark(bookmarkName, true, true);
bookmarkName.Remove();
builder.InsertDocumentInline(extractedContent, ImportFormatMode.UseDestinationStyles, new ImportFormatOptions());
dst.Save(@"C:\Temp\out.docx");
After moving to that bookmark place i am removing that so that text, image ,table all will be handled
@Raghul214 I have tested the scenario with tables and shapes and everything is working fine. You should make sure the content wrapped with bookmark properly.
See I have a document doc 1 - having aspose syntax with if condition i bookmarked inner content of if…
Another document doc 2 - having generated document so only static content will be there …
Now if i remove the bookmark in doc1 and insert the content of bookmark extracted from doc2 then only its not inserting in correct place…
<<if [Code == "Code"]>>
<</if>>Static Content Check
this is the output
@Raghul214 As I have mentioned, I cannot reproduce the problem on my side. Here is the output produced on my side using the above provided code:
out.docx (22.3 KB)