Document Visitor does not remove associated hidden section break and makes it visible

Hi,

I’m using your RemoveHiddenContentVisitor code from DocumentVisitor.VisitFormField | Aspose.Words for .NET but it doesn’t work as expected.

I have uploaded a test document TEST.docx (11.9 KB)

The document is simple. Page 1 only has hidden text and a section break. When viewed in word (showing hidden text)

When viewing hidden text is off, the document shows only 1 page:

However, running the RemoveHiddenContentVisitor on the document:

RemoveHiddenContentVisitor removeHiddenContent = new RemoveHiddenContentVisitor();

Aspose.Words.NodeCollection nodeCollectionParagraph = wordDocument.GetChildNodes(NodeType.Paragraph, true);

for (Int32 i = nodeCollectionParagraph.Count - 1; i >= 0; i--)
{
    Aspose.Words.Paragraph wordParagraph = nodeCollectionParagraph[i] as Aspose.Words.Paragraph;
    wordParagraph.Accept(removeHiddenContent);
}

wordDocument.Accept(removeHiddenContent);

The document has the hiddent text correctly removed, but the section break is still present and is now VISIBLE which is not what is expected.

(Display Hidden On)

(Display Hidden Off)

Please advise how to use the RemoveHiddenContentVisitor to remove the section break or at least for it not to become visible.

@GaryO Section break is not a hidden content, it is a formatting mark that shows the end of section. Please see our documentation to learn more about sections:
https://docs.aspose.com/words/net/working-with-sections/

I understand Alexey, but as you can see in the document screenshots I’ve provided above the section break is Hidden before the DocumentVisitor is run (only 1 page visible) and visible after (2 pages visible).

How do you remove the hidden text and the associated section break so that the visible number of pages remain the same.

(i.e. at worst, there should be a blank hidden page, there should never be a visible blank page after removing the hidden text)

@GaryO To remove section break, which is added in the hidden paragraph, you should merge the sections separated by section break. For example see the following code:

Document doc = new Document(@"C:\Temp\in.docx");

// Get paragraphs, which are end of sections.
List<Paragraph> endOfSectionParagraphs = doc.GetChildNodes(NodeType.Paragraph, true).Cast<Paragraph>()
    .Where(p => p.IsEndOfSection && !p.IsEndOfDocument).ToList();

// Merge sections with hiddent end of section paragraphs.
foreach (Paragraph p in endOfSectionParagraphs)
{
    if (p.ParagraphBreakFont.Hidden)
    {
        p.ParentSection.AppendContent((Section)p.ParentSection.NextSibling);
        // remove empty section.
        p.ParentSection.NextSibling.Remove();
    }
}

// Here accept the visitor to remove hidden content. 
// ................

doc.Save(@"C:\Temp\out.docx");

Thanks Alexey. Not quite sure what the implications are but it seems to have done the trick.

1 Like