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.
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:
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.
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.
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();
}
}
}
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.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
Enables storage, such as cookies, related to analytics.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.