C++: Remove Paragraphs in Word

While editing a Word document, there are numerous reasons to delete certain paragraphs in it. For example, it could be as simple as restructuring the document, or removing incorrect or irrelevant information to ensure document accuracy. In this article, you will learn how to programmatically remove paragraphs in a Word document using Spire.Doc for C++.

Install Spire.Doc for C++

There are two ways to integrate Spire.Doc for C++ into your application. One way is to install it through NuGet, and the other way is to download the package from our website and copy the libraries into your program. Installation via NuGet is simpler and more recommended. You can find more details by visiting the following link.

Integrate Spire.Doc for C++ in a C++ Application

Remove All Paragraphs in a Word Document in C++

To remove all paragraphs, you need to loop through all sections in a document and then delete all paragraphs in each section using Section->GetParagraphs()->Clear() method. The following are the detailed steps.

  • Create a Document instance.
  • Load a Word document using Document->LoadFromFile() method.
  • Traverse through each section of the document and then remove all paragraphs in the section using Section->GetParagraphs()->Clear() method.
  • Save the result document using Document->SaveToFile() method.
  • C++
#include "Spire.Doc.o.h"

using namespace Spire::Doc;

int main() {
	//Specify the input and output file paths
	std::wstring inputFile = L"Data\\sample.docx";
	std::wstring outputFile = L"Output\\RemoveAllParagraphs.docx";

	//Create a Document instance
	Document* document = new Document();

	//Load a Word document from disk
	document->LoadFromFile(inputFile.c_str());

	//Remove paragraphs from every section in the document
	for (int i = 0; i < document->GetSections()->GetCount(); i++)
	{
		Section* section = document->GetSections()->GetItem(i);
		section->GetParagraphs()->Clear();
	}

	//Save the result document
	document->SaveToFile(outputFile.c_str(), FileFormat::Docx2013);
	document->Close();
	delete document;
}

C++: Remove Paragraphs in Word

Remove a Specific Paragraph in a Word Document in C++

If you find a paragraph that contains duplicate or useless information, Spire.Doc for C++ allows you to delete the specified paragraph using Section->GetParagraphs()->RemoveAt() method. The following are the detailed steps.

  • Create a Document instance.
  • Load a Word document using Document->LoadFromFile() method.
  • Get a specified section of the document using Document->GetSections()->GetItem() method.
  • Remove a specified paragraph in the section using Section->GetParagraphs()->RemoveAt() method.
  • Save the result document using Document->SaveToFile() method.
  • C++
#include "Spire.Doc.o.h"

using namespace Spire::Doc;

int main() {
	//Specify the input and output file paths
	std::wstring inputFile = L"Data\\sample.docx";
	std::wstring outputFile = L"Output\\RemoveSpecificParagraph.docx";

	//Create a Document instance
	Document* document = new Document();

	//Load a Word document from disk
	document->LoadFromFile(inputFile.c_str());

	//Get the first section of the document
	Section* sec = document->GetSections()->GetItem(0);

	//Remove the third paragraph in the section
	sec->GetParagraphs()->RemoveAt(2);

	//Save the result document
	document->SaveToFile(outputFile.c_str(), FileFormat::Docx2013);
	document->Close();
	delete document;
}

C++: Remove Paragraphs in Word

Remove Blank Paragraphs in a Word Document in C++

When there are many empty paragraphs/lines in a document, it quite necessary to remove them to improve readability. The following are the steps to remove all blank paragraphs/lines in a Word document.

  • Create a Document instance.
  • Load a Word document using Document->LoadFromFile() method.
  • Traverse through all paragraphs in the document and determine whether the paragraph is a blank paragraph.
  • Remove blank paragraphs from the document using section->GetBody()->GetChildObjects()->Remove() method.
  • Save the result document using Document->SaveToFile() method.
  • C++
#include "Spire.Doc.o.h"

using namespace Spire::Doc;

int main() {
	//Specify the input and output file paths
	std::wstring inputFile = L"Data\\Test.docx";
	std::wstring outputFile = L"Output\\RemoveEmptyLines.docx";

	//Create a Document instance
	Document* document = new Document();

	//Load a Word document from disk
	document->LoadFromFile(inputFile.c_str());

	//Traverse each paragraph in the Word document
	for (int i = 0; i < document->GetSections()->GetCount(); i++)
	{
		Section* section = document->GetSections()->GetItem(i);

		for (int j = 0; j < section->GetBody()->GetChildObjects()->GetCount(); j++)
		{
			DocumentObject* secChildObject = section->GetBody()->GetChildObjects()->GetItem(j);
			if (secChildObject->GetDocumentObjectType() == DocumentObjectType::Paragraph)
			{
				Paragraph* para = dynamic_cast<Paragraph*>(secChildObject);
				std::wstring paraText = para->GetText();

				//Determine if the paragraph is a blank paragraph
				if (paraText.empty())
				{
					//Remove blank paragraphs
					section->GetBody()->GetChildObjects()->Remove(secChildObject);
					j--;
				}
			}

		}
	}

	//Save the result document
	document->SaveToFile(outputFile.c_str(), FileFormat::Docx2013);
	document->Close();
	delete document;
}

C++: Remove Paragraphs in Word

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.