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

Free Support Forum - aspose.com

Detect if a section is empty?

Hello,

How would I go about determining that a section is empty so that I can remove it? I’ve fiddled around with this but can’t seem to work out what it is I need to do:

// If the first section in the resultant document is completely empty remove it.
if (d.FirstSection.Body.GetChildNodes(NodeType.Any, false).Count <= 2)
{
    if (d.FirstSection.GetText().Trim().Length == 0) { 
        d.FirstSection.Remove();

It’s that second if that seems to be stopping it from working.

Hi Simon,

Thanks
for your inquiry. Please use the following code example to achieve your
requirements. Hope this helps you. Please let us know if you have any
more queries.

Document doc = new Document(MyDir + "in.docx");
foreach (Section section in doc.Sections)
{
    Console.WriteLine(section.Body.GetChildNodes(NodeType.Any, true).Count);
    if (section.Body.FirstParagraph.Equals(section.Body.LastParagraph))
    {
        if (section.ToString(SaveFormat.Text).Trim().Equals(String.Empty)
        && section.Body.GetChildNodes(NodeType.Shape, true).Count == 0
        && section.Body.GetChildNodes(NodeType.DrawingML, true).Count == 0)
            section.Remove();
    }
}
doc.Save(MyDir + "Out.docx");

Unfotunately no. The first section (the empty one), it reports a Count of 2 on the Console.WriteLine, but doesn’t enter the next if. However the next section, which prints 75 for the child count then DOES get into the if, so that comparsion:

if(section.Body.FirstParagraph.Equals(section.Body.LastParagraph))

Is not doing what we think it is doing.

Unzipping the xml I can see the first section is simply:

<w:body>
	<w:p>
		<w:pPr>
			<w:pStyle w:val="AONormal" />
		</w:pPr>
	</w:p>
	<w:p>
		<w:pPr>
			<w:sectPr w:rsidSect="008E0A09">
				<w:headerReference w:type="even" r:id="rId5" />
				<w:headerReference w:type="default" r:id="rId6" />
				<w:footerReference w:type="even" r:id="rId7" />
				<w:footerReference w:type="default" r:id="rId8" />
				<w:headerReference w:type="first" r:id="rId9" />
				<w:footerReference w:type="first" r:id="rId10" />
				<w:pgSz w:w="11906" w:h="16838" w:code="9" />
				<w:pgMar w:top="1588" w:right="1134" w:bottom="1021" w:left="1134" w:header="851" w:footer="454" w:gutter="0" />
				<w:cols w:space="708" />
				<w:docGrid w:linePitch="360" />
			</w:sectPr>
		</w:pPr>
	</w:p>
	<w:tbl>
		<w:tblPr>
			<w:tblStyle w:val="TableNormal" />
..................

Hi Simon,

Thanks for your inquiry. Could you please attach your input Word document here for testing? I will investigate the issue on my side and provide you more information.

Hi Simon,

Thanks
for sharing the detail. Please use following code example to achieve your requirements. Hope this helps you. Please let us know if you have any more queries.

Document doc = new Document(MyDir + "in.docx");
foreach (Section section in doc.Sections)
{
    Console.WriteLine(section.Body.GetChildNodes(NodeType.Any, true).Count);
    if (section.Body.ToString(SaveFormat.Text).Trim().Equals(String.Empty)
    && section.Body.GetChildNodes(NodeType.Shape, true).Count == 0
    && section.Body.GetChildNodes(NodeType.DrawingML, true).Count == 0)
        section.Remove();
}
doc.Save(MyDir + "Out.docx");

So I’ve encountered an oddity. If I load a docx, remove sections, and save to docx, they’re gone. If I load a docx, remove sections and save to pdf, they’re still there. Is there something I need to call to get aspose to update it’s internal model or something before saving to pdf?

Here’s what I’m using to remove sections (should be faster than creating strings out of entire sections):

static void RemoveEmptySections(Document doc)
{
    foreach (Section section in doc.Sections)
    {
        bool isEmpty = true;
        var paras = section.Body.GetChildNodes(NodeType.Paragraph, true);
        for (int i = 0; i < paras.Count && isEmpty == true; i++)
        {
            isEmpty = paras[i].ToString(SaveFormat.Text).Trim().Equals(String.Empty);
        }

        if (isEmpty)
        {
            isEmpty = (section.Body.GetChildNodes(NodeType.Shape, true).Count == 0 && section.Body.GetChildNodes(NodeType.DrawingML, true).Count == 0);
        }

        if (isEmpty)
        {
            section.Remove();
        }
    }
}

Update fields seems to do the trick.

Hi Simon,

Thanks
for your inquiry. In case you are using an older version of Aspose.Words, I would suggest you please upgrade to the latest version (v14.11.0) from here and let us know how it goes on your side. Please call Document.UpdatePageLayout method before saving the final output Pdf file. Hope this helps you.

If the problem still remains, please attach
your Word document here for testing. I will investigate the issue on my
side and provide you more information.