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");