Hello,
Thank you for your inquiry. We appreciate your interest in our products.
To delete empty paragraphs from a Word document using the latest version of Spire.Doc (
Spire.Doc for Cpp Version: 11.9.0), you can refer to the code snippet below:
- Code: Select all
// The path of the input file
wstring inputFile = L"input.docx";
// Create a new Document object
intrusive_ptr<Document> document = new Document();
// Load the document from the specified file
document->LoadFromFile(inputFile.c_str());
for (int i = 0; i < document->GetSections()->GetCount(); i++) {
// Get the current section
intrusive_ptr<Section> section = document->GetSections()->GetItemInSectionCollection(i);
for (int j = 0; j < section->GetBody()->GetChildObjects()->GetCount(); j++) {
// Get the current child object in the section's body
intrusive_ptr<DocumentObject> secChildObject = section->GetBody()->GetChildObjects()->GetItem(j);
// Check if the child object is a paragraph
if (secChildObject->GetDocumentObjectType() == DocumentObjectType::Paragraph) {
// Cast the child object to a Paragraph
intrusive_ptr<Paragraph> para = Object::Dynamic_cast<Paragraph>(secChildObject);
// Get the text of the paragraph
std::wstring paraText = para->GetText();
// Check if the paragraph is empty
if (paraText.empty()) {
// Remove the empty paragraph
section->GetBody()->GetChildObjects()->Remove(secChildObject);
j--; // Decrement the loop counter to compensate for the removed paragraph
}
}
}
}
// The path of the output file
wstring outputFile = L"result.docx";
// Save the modified document to the specified file format
document->SaveToFile(outputFile.c_str(), FileFormat::Docx2013);
// Close the document
document->Close();
If you have any further questions or need additional assistance, please don't hesitate to reach out to us.
Thank you for choosing Spire.Doc for your document processing needs.
Best regards,
Annika
E-iceblue support team