Hello,
I was wondering what’s the best way to check if a document is empty. I know is never really empty since it always has Section, Body, and Paragraph. But would like to know if is just that. For example:
public static bool IsEmptyDocument(Document srcDoc)
{
//Remove empty paragraphs from the end of document. Don't want blank paragraphs added to main document.
while (!((CompositeNode)srcDoc.LastSection.Body.LastChild).HasChildNodes)
{
srcDoc.LastSection.Body.LastParagraph.Remove();
if (srcDoc.LastSection.Body.LastChild == null)
break;
}
if (srcDoc.FirstSection.Body.FirstParagraph.Equals(srcDoc.LastSection.Body.LastChild))
if (IsEmptyParagraph(srcDoc.FirstSection.Body.FirstParagraph))
return true;
else
return false;
else
return false;
}
public static bool IsEmptyParagraph(Node node)
{
bool rc = false;
if (node.NodeType == NodeType.Paragraph)
{
Aspose.Words.Paragraph para = (Aspose.Words.Paragraph)node;
if (para.ChildNodes.Count == 0)
{
rc = true;
}
else
{
if (para.Range.Text == "\r")
{
rc = true; // assume true unless we find embedded excel object
foreach (Node childNode in para.ChildNodes)
{
if (childNode.NodeType == NodeType.Shape)
{
rc = false;
break;
}
}
}
}
}
return rc;
}
Is there a better way?