How to delete headings with hyperlinks with all content associated with them

Hello,

how to run through all the cells of the column "Name" of the table of the document and delete titles if they do not coincide with the content of these cells, and all the contents associated with them from another document?

More clearly in file attachment. Thanks.
Hi there,

Thanks for your inquiry. Could you please attach your input and expected output Word documents here for our reference? We will then provide you more information on this along with code.

Hi, Tahir

Sending input and expected output files. The output file section "Name 1" was removed.
Hi there,

Thanks for sharing the documents. Please use the following code example to achieve your requirements. This code example removes the contents under heading "Name 1". Hope this helps you.

String removeContents = "Name 1";
Document doc =
new Document(MyDir + "input 2.docx");
doc.updateFields();
DocumentBuilder builder =
new DocumentBuilder(doc);

builder.moveToDocumentEnd();
builder.startBookmark(
"_TocEnd");
builder.endBookmark(
"_TocEnd");

// Get list of bookmark listed in TOC
ArrayList tocitems = new ArrayList();
for (Field field : doc.getRange().getFields())
{
if (field.getType() == FieldType.FIELD_PAGE_REF)
{

String fieldText = field.getFieldCode();
if (fieldText.contains("_Toc"))
{
fieldText = fieldText.replace(
"PAGEREF", "").replace("\\h", "").trim();
tocitems.add(fieldText);
}
}
}
tocitems.add(
"_TocEnd");

for (int i = 0; i < tocitems.size() - 1; i++)
{
BookmarkStart bookmarkStart = doc.getRange().getBookmarks().get(tocitems.get(i).toString()).getBookmarkStart();
BookmarkStart bookmarkEnd = doc.getRange().getBookmarks().get(tocitems.get(i +
1).toString()).getBookmarkStart();

Paragraph paragraph = (Paragraph)bookmarkStart.getParentNode().deepClone(
true);

if(paragraph.toString(SaveFormat.TEXT).trim().equals(removeContents))
{
//Bookmark the contents and delete them
builder.moveTo(bookmarkStart);
builder.startBookmark("BM_Delete");
builder.moveTo(bookmarkEnd);
builder.endBookmark("BM_Delete");

doc.getRange().getBookmarks().get("BM_Delete").setText("");
doc.getRange().getBookmarks().get("BM_Delete").getBookmarkStart().getParentNode().remove();
}

}

doc.updateFields();
doc.save(
MyDir + "17.2.0.docx");
Hi, Tahir.

Thanks for answer. The code in this example removes the header from the TOC with the content of the end of this list. What to write, so he removed headings with the associated content from the middle of the list of self-assembled?

Attached is the input file and expected output file. In the expected file section Name 2.2 was removed with related content.

Thank you.
Hi there,

Thanks for your inquiry. Please use the following modified code example to get the desired output. Hope this helps you.

In Microsoft Word, fields are not automatically updated when a document is opened, but you can update fields in a document at any time by pressing F9. In Aspose.Words, you can update the fields e.g. TOC field using Document.updateFields method.

Please let us know if you have any more queries.

String removeContents = "Name 2.2";
Document doc = new Document(MyDir + "TestInput.docx");
doc.updateFields();

DocumentBuilder builder = new DocumentBuilder(doc);

builder.moveToDocumentEnd();
builder.startBookmark("_TocEnd");
builder.endBookmark("_TocEnd");

// Get list of bookmark listed in TOC
ArrayList tocitems = new ArrayList();
for (Field field : doc.getRange().getFields())
{
if (field.getType() == FieldType.FIELD_PAGE_REF)
{

String fieldText = field.getFieldCode();
if (fieldText.contains("_Toc"))
{
fieldText = fieldText.replace("PAGEREF", "").replace("\\h", "").trim();
tocitems.add(fieldText);
}
}
}
tocitems.add("_TocEnd");

for (int i = 0; i < tocitems.size() - 1; i++)
{
BookmarkStart bookmarkStart = doc.getRange().getBookmarks().get(tocitems.get(i).toString()).getBookmarkStart();
BookmarkStart bookmarkEnd = doc.getRange().getBookmarks().get(tocitems.get(i + 1).toString()).getBookmarkStart();

Paragraph paragraph = (Paragraph)bookmarkStart.getParentNode().deepClone(true);

if(paragraph.toString(SaveFormat.TEXT).trim().equals(removeContents))
{
//Bookmark the contents
builder.moveTo(bookmarkStart);
builder.startBookmark("BM_Delete");
builder.moveTo(bookmarkEnd);
builder.endBookmark("BM_Delete");
builder.insertParagraph();
}
}

doc.updatePageLayout();
//Remove the bookmarked contents.
doc.getRange().getBookmarks().get("BM_Delete").setText("");
doc.getRange().getBookmarks().get("BM_Delete").getBookmarkStart().getParentNode().remove();
doc.updateFields();
doc.save(MyDir + "17.2.0.docx");
Thank you so much!
Your code to help resolve this problem, but there was another problem.
So, if we remove a section and it's only one how to remove all of the top headings?
There are the input file and expected output file in attachment. In the "Part 1" was the only one "Name 1" and we have removed all the headlines.
Thank you.
Hi there,

Thanks for your inquiry. Please use the same approach to remove the contents. In this case, you just need to bookmark the contents that you want to remove. Please enclose the contents between BookmarkStart and BookmarkEnd nodes and set the text of bookmark to empty string using Bookmark.Text property.

Please let us know if you have any more queries.