We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Is empty document?

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?

Hi there,


Thanks for your inquiry.

You can use the following method to test if a document is empty:

<span style=“font-size:
10.0pt;font-family:“Courier New”;color:blue;mso-no-proof:yes”>public<span style=“font-size:10.0pt;font-family:“Courier New”;mso-no-proof:yes”> static void
IsDocumentEmpty(Document doc)<o:p></o:p>

{

return string.IsNullOrEmpty(doc.ToTxt().Trim());

}


If we can help with anything else, please feel free to ask.

Thanks

Thank you Adam,

However, this doesn't seem to work if the document just has an image. I wonder in what other scenarios this wont work; tables, Shapes?

Any idea on how to handle these and other scenarios? I have attached an example document with the image for your reference.

Thanks again.

Hi there,


Thanks for this additional information.

Your right, this method wouldn’t work in that case. The reason being is that the term “empty” can mean different things depending upon what you are looking for.

Please try using the code below instead, this should be better for your case.

<span style=“font-size:
10.0pt;font-family:“Courier New”;color:blue;mso-no-proof:yes”>public<span style=“font-size:10.0pt;font-family:“Courier New”;mso-no-proof:yes”> static bool
IsDocumentEmpty(Document doc)<o:p></o:p>

{

doc.EnsureMinimum();

return doc.GetChildNodes(NodeType.Any, true).Count == 3;

}


Thanks,