Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Wed Dec 27, 2023 2:52 pm

Win10/ Visual Studio 2022 (v143)/ Standart C++14/Standart C ISO C17

19 Section* section = document->GetSections()->GetItem(i);

error: there is no suitable function for conversion "boost::intrusive_ptr<Spire::Doc::DocumentObject>" to "Spire::Doc::Section *"

SPIRE.jpg

EugeneMinovich
 
Posts: 4
Joined: Wed Dec 27, 2023 12:36 pm

Thu Dec 28, 2023 1:45 am

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
User avatar

Annika.Zhou
 
Posts: 1657
Joined: Wed Apr 07, 2021 2:50 am

Thu Dec 28, 2023 6:57 am

Thanks for the answer. Sorry, I forgot to mention that I'm using the latest version of Spire.Doc (Spire.Doc for Cpp version: 11.9.0). I tried a dynamic cast to Spire::Doc::Section * as in line
26 "Paragraph* para = Dynamic_cast<Paragraph*>(secChildObject);" namely,
19 "Section* section = dynamic_cast<Section*>(document->GetSections()->GetItem(i));" I'm getting the same error.
And option
19 “Section*section = Dynamic_cast<Section*>(document->GetSections()->GetItem(i).get());”
solves the problem of this error, but errors appear in further instructions.

EugeneMinovich
 
Posts: 4
Joined: Wed Dec 27, 2023 12:36 pm

Thu Dec 28, 2023 7:53 am

Hello,

Thank you for your feedback.
Regarding the use of Spire.Doc for Cpp, we would like to clarify that our library internally utilizes the Boost library to wrap document model objects. Therefore, it is necessary to use "boost::intrusive_ptr" to handle these objects properly.
We apologize for any confusion caused by the previous instructions. To address this, please refer to the code snippet provided in my previous response, which demonstrates the correct usage of "boost::intrusive_ptr".
Thank you for your understanding and cooperation.

Best regards,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1657
Joined: Wed Apr 07, 2021 2:50 am

Thu Dec 28, 2023 11:28 am

Thank you. The code you provided works. Sorry, I didn't notice in your answer that you provided a significantly modified code example.
Best regards
Eugene

EugeneMinovich
 
Posts: 4
Joined: Wed Dec 27, 2023 12:36 pm

Fri Dec 29, 2023 1:13 am

Hello,

You're welcome.
If you encounter other issues related to our products in the future, please feel free to contact us.
Have a nice day.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1657
Joined: Wed Apr 07, 2021 2:50 am

Return to Spire.Doc