I am inserting child documents into a master document, or technically, merging a bunch of different files together. I am placing content in content controls, and removing those content controls if there is no content to be put in them. The issue is that removing these content controls is not deleting the space that they occupied and is leaving empty paragraphs. Can I remove all of the leftover whitespace from the removed content control?
private void PopulateContentControls(Document doc, List<TemplateItem> children, HashSet<string> processedContent)
{
var controls = doc.GetChildNodes(NodeType.StructuredDocumentTag, true);
_logger.LogDebug(AppGlobals.DebugMessages.StartProcessingContentControls, controls.Count);
foreach (StructuredDocumentTag sdt in controls.Cast<StructuredDocumentTag>())
{
try
{
_logger.LogDebug(AppGlobals.DebugMessages.ProcessingSDT, sdt.Tag);
var correspondingChild = children.FirstOrDefault(c => c.Name == sdt.Tag);
if (correspondingChild != null)
{
_logger.LogInformation(AppGlobals.DebugMessages.FoundMatchingChild, sdt.Tag, correspondingChild.Name);
if (sdt.GetChildNodes(NodeType.Run, true).FirstOrDefault() is Run run)
{
_logger.LogDebug(AppGlobals.DebugMessages.RunNodePresent);
run.Text = correspondingChild.Content;
processedContent.Add(correspondingChild.Content);
}
else
{
_logger.LogWarning(AppGlobals.DebugMessages.NoRunNodePresent);
sdt.RemoveAllChildren();
sdt.AppendChild(new Run(doc, correspondingChild.Content));
processedContent.Add(correspondingChild.Content);
}
}
else
{
_logger.LogInformation(AppGlobals.DebugMessages.NoMatchingChild, sdt.Tag);
sdt.RemoveAllChildren();
sdt.Remove();
}
CleanParagraphs(sdt.ParentNode);
}
catch (Exception ex)
{
_logger.LogError(ex, AppGlobals.ErrorMessages.ProcessSDTError, sdt.Tag);
}
}
}
private void CleanParagraphs(Node node)
{
if (node is Paragraph paragraph)
{
bool hasNonEmptyRun = false;
foreach (Run run in paragraph.Runs.Cast<Run>().ToList())
{
if (string.IsNullOrWhiteSpace(run.Text))
{
run.Remove();
}
else
{
hasNonEmptyRun = true;
}
}
if (!hasNonEmptyRun)
{
paragraph.Remove();
}
}
}