Friday, 09 June 2023 01:25

C++: Merge Word Documents

Merging Word documents can be useful when you want to consolidate information from multiple files. For example, if you have a series of reports that relate to a specific project or topic, you may want to merge them into one document for easier access, distribution, and archiving purposes. In this article, we will demonstrate how to merge Word documents into one in C++ 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

Merge Word Documents by Inserting Files in C++

The Document->InsertTextFromFile(LPCWSTR_S fileName, FileFormat fileFormat) method provided by Spire.Doc for C++ enables developers to merge Word documents by inserting the contents of other Word documents into the original Word document. Using this method, the inserted contents will begin on a new page. The detailed steps are as follows:

  • Initialize an instance of the Document class.
  • Load a Word document using the Document->LoadFromFile(LPCWSTR_S fileName) method.
  • Insert another document into the loaded Word document using the Document->InsertTextFromFile(LPCWSTR_S fileName, FileFormat fileFormat) method.
  • Save the result document to a specific location using the Document->SaveToFile(LPCWSTR_S fileName, FileFormat fileFormat) method.
  • C++
#include "Spire.Doc.o.h"

using namespace Spire::Doc;
using namespace std;

int main()
{
	//Specify the input file paths
	wstring inputFile_1 = L"Sample1.docx";
	wstring inputFile_2 = L"Sample2.docx";
	//Specify the output file path
	wstring outputFile = L"MergeWordDocumentsByInsertingFile.docx";

	//Initialize an instance of the Document class
	intrusive_ptr<Document> doc = new Document();
	//Load a Word document
	doc->LoadFromFile(inputFile_1.c_str());

	//Insert another document into the loaded document
	doc->InsertTextFromFile(inputFile_2.c_str(), FileFormat::Auto);

	//Save the result document to a specific location
	doc->SaveToFile(outputFile.c_str(), FileFormat::Docx2013);
	doc->Close();
}

C++: Merge Word Documents

Merge Word Documents by Cloning Contents in C++

If you want to merge documents without starting from a new page, you can clone the contents of other documents and add them to the end of the original document. This approach can avoid creating unwanted section breaks. The detailed steps are as follows:

  • Initialize an instance of the Document class.
  • Load the first Word document using the Document->LoadFromFile(LPCWSTR_S fileName) method.
  • Get the last section of the first Word document using the Document->GetLastSection() method.
  • Initialize an instance of the Document class.
  • Load the second Word document using the Document->LoadFromFile(LPCWSTR_S fileName) method.
  • Iterate through all sections of the second Word document, and then iterate through all child objects of each section.
  • Clone each child object using the DocumentObject->Clone() method.
  • Add the cloned child object to the end of the last section of the first Word document using the Section->GetBody()->GetChildObjects()->Add(intrusive_ptr<IDocumentObject> entity) method.
  • Save the first Word document to a specific location using the Document->SaveToFile(LPCWSTR_S fileName, FileFormat fileFormat) method.
  • C++
#include "Spire.Doc.o.h"

using namespace Spire::Doc;
using namespace std;

int main()
{
	//Specify the input file paths
	wstring inputFile_1 = L"Sample1.docx";
	wstring inputFile_2 = L"Sample2.docx";
	//Specify the output file path
	wstring outputFile =  L"MergeWordDocumentsByCloningContents.docx";

	//Initialize an instance of the Document class
	intrusive_ptr<Document> document1 = new Document();
	//Load the first Word document
	document1->LoadFromFile(inputFile_1.c_str(), FileFormat::Auto);
	//Get the last section of the first Word document
	intrusive_ptr<Section> lastSection = document1->GetLastSection();

	//Initialize an instance of the Document class
	intrusive_ptr<Document> document2 = new Document();
	//Load the second Word document
	document2->LoadFromFile(inputFile_2.c_str(), FileFormat::Auto);

	//Get the number of sections of the second Word document
	int sectionCount = document2->GetSections()->GetCount();
	
	//Iterate through the sections of the second Word document
	for (int i = 0; i < sectionCount; i++)
	{
		intrusive_ptr<Section> section = document2->GetSections()->GetItemInSectionCollection(i);
		int sectionChildObjectsCount = section->GetBody()->GetChildObjects()->GetCount();
		//Iterate through the child objects in each section of the second Word document
		for (int j = 0; j < sectionChildObjectsCount; j++)
		{
			intrusive_ptr<DocumentObject> documentObject = section->GetBody()->GetChildObjects()->GetItem(j);
			//Clone each child object
			intrusive_ptr<DocumentObject> clonedObject = documentObject->Clone();
			//Add each cloned child object to the end of the last section of the first Word document
			lastSection->GetBody()->GetChildObjects()->Add(clonedObject);
		}
	}

	//Save the first Word document to a specific location
	document1->SaveToFile(outputFile.c_str(), FileFormat::Docx);
	document1->Close();
	document2->Close();
}

C++: Merge Word Documents

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.

Published in Document Operation

Comments are a commonly used feature in situations where multiple people are collaborating on the same document simultaneously. By using comments, collaborators can offer their thoughts or ideas directly in the document without making any changes to the actual text. This allows authors to review and consider the feedback before making any revisions. Additionally, comments provide an efficient way to identify specific issues within the document, as each comment is linked directly to the corresponding text or location in the document. In this article, we will explain how to work with comments in Word, specifically, how to add, reply to and delete comments in Word documents 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

Add a Comment to a Paragraph in Word in C++

You can add a comment to a paragraph to provide a suggestion or additional information about the content of the paragraph. The following steps explain how to add a comment to a specific paragraph in a Word document using Spire.Doc for C++:

  • Initialize an instance of the Document class.
  • Load a Word document using the Document->LoadFromFile(LPCWSTR_S fileName) method.
  • Get a specific section of the document using the Document->GetSections()->GetItemInSectionCollection(int index) method.
  • Get a specific paragraph of the section using the Section->GetParagraphs()->GetItemInParagraphCollection(int index) method.
  • Add a comment to the paragraph using the Paragraph->AppendComment(LPCWSTR_S text) method.
  • Set the author and ID for the comment using the Comment->GetFormat()->SetAuthor(LPCWSTR_S value) and Comment->GetFormat()->SetCommentId(int value) methods.
  • Save the result document using the Document->SaveToFile(LPCWSTR_S fileName, FileFormat fileFormat) method.
  • C++
#include "Spire.Doc.o.h"

using namespace Spire::Doc;

int main()
{
	//Initialize an instance of the Document class
	intrusive_ptr<Document> document = new Document();
	//Load a Word document
	document->LoadFromFile(L"Input.docx");

	//Get the first section of the document
	intrusive_ptr<Section> section = document->GetSections()->GetItemInSectionCollection(0);

	//Get the second paragraph of the first section
	intrusive_ptr<Paragraph> paragraph = section->GetParagraphs()->GetItemInParagraphCollection(1);

	//Add a comment to the paragraph
	intrusive_ptr<Spire::Doc::Comment> comment = paragraph->AppendComment(L"This comment is added using Spire.Doc for Cpp.");
	//Set the author and ID for the comment
	comment->GetFormat()->SetAuthor(L"E-iceblue");
	comment->GetFormat()->SetCommentId(0);
	comment->GetFormat()->SetInitial(L"CM");

	//Save the result document
	document->SaveToFile(L"AddCommentToParagraph.docx", FileFormat::Docx2019);
	document->Close();
}

C++: Add, Reply to, or Delete Comments in Word Documents

Add a Comment to a Specific Text in Word in C++

In addition to adding comments to paragraphs, Spire.Doc for C++ is also capable of adding comments to specific text in Word documents. The following steps explain how to add a comment to a specific text in a Word document using Spire.Doc for C++:

  • Initialize an instance of the Document class.
  • Load a Word document using the Document->LoadFromFile(LPCWSTR_S fileName) method.
  • Find the specific text in the document using the Document->FindString(LPCWSTR_S stringValue, bool caseSensitive, bool wholeWord) method.
  • Initialize an instance of the Comment class to create a new comment. Then set the content, author, and ID for the comment.
  • Initialize an instance of the CommentMark class and set its type as CommentMarkType::CommentStart to create a comment start mark. Then set the ID of the comment start mark to be the same as the ID of the comment.
  • Initialize an instance of the CommentMark class and set its type as CommentMarkType::CommentEnd to create a comment end mark. Then set the ID of the comment end mark to be the same as the ID of the comment.
  • Get the owner paragraph of the found text. Then add the comment to the owner paragraph using the Paragraph->GetChildObjects()->Add(intrusive_ptr<IDocumentObject> entity) method.
  • Get the index position of the found text in the owner paragraph using the Paragraph->GetChildObjects()->IndexOf(intrusive_ptr<IDocumentObject> entity) method.
  • Insert the comment start mark before the index position of the found text using the Paragraph->GetChildObjects()->Insert(int index, intrusive_ptr<IDocumentObject> entity) method.
  • Insert the comment end mark after the index position of the found text using the Paragraph->GetChildObjects()->Insert(int index, intrusive_ptr<IDocumentObject> entity) method.
  • Save the result document using the Document->SaveToFile(LPCWSTR_S fileName, FileFormat fileFormat) method.
  • C++
#include "Spire.Doc.o.h"

using namespace Spire::Doc;

int main()
{
	//Initialize an instance of the Document class
	intrusive_ptr<Document> document = new Document();
	//Load a Word document
	document->LoadFromFile(L"Input.docx");

	//Find a specific text
	intrusive_ptr<TextSelection> find = document->FindString(L"Microsoft Office", false, true);


	//Create a comment and set its content, author and ID
	intrusive_ptr<Comment> comment = new Comment(document);
	comment->GetBody()->AddParagraph()->SetText(L"A software developed by Microsoft in 1988.");
	comment->GetFormat()->SetAuthor(L"George");
	comment->GetFormat()->SetCommentId(0);

	//Create a comment start mark
	intrusive_ptr<CommentMark> commentmarkStart = new CommentMark(document);
	commentmarkStart->SetType(CommentMarkType::CommentStart);
	//Set its ID to be the same as the ID of the comment
	commentmarkStart->SetCommentId(0);
	//Create a comment end mark
	intrusive_ptr<CommentMark> commentmarkEnd = new CommentMark(document);
	commentmarkEnd->SetType(CommentMarkType::CommentEnd);
	//Set its ID to be the same as the ID of the comment
	commentmarkEnd->SetCommentId(0);

	//Get the found text as a single text range
	intrusive_ptr<TextRange> range = find->GetAsOneRange();

	//Get the owner paragraph of the text range
	intrusive_ptr<Paragraph> para = range->GetOwnerParagraph();

	//Add the comment to the owner paragraph
	para->GetChildObjects()->Add(comment);

	//Get the index position of the text range in the owner paragraph
	int index = para->GetChildObjects()->IndexOf(range);

	//Insert the comment start mark before the index position of the text range
	para->GetChildObjects()->Insert(index, commentmarkStart);
	//Insert the comment end mark after the index position of the text range
	para->GetChildObjects()->Insert(index + 2, commentmarkEnd);

	//Save the result document
	document->SaveToFile(L"AddCommentForSpecificText.docx", FileFormat::Docx2019);
	document->Close();
}

C++: Add, Reply to, or Delete Comments in Word Documents

Add a Comment with a Picture in Word in C++

Sometimes, you may want to include a picture in your comment to supplement the textual content. The following steps explain how to add a comment with a picture to a Word document using Spire.Doc for C++:

  • Initialize an instance of the Document class.
  • Load a Word document using the Document->LoadFromFile(LPCWSTR_S fileName) method.
  • Get a specific section of the document using the Document->GetSections()->GetItemInSectionCollection(int index) method.
  • Get a specific paragraph of the section using the Section->GetParagraphs()->GetItemInParagraphCollection(int index) method.
  • Add a comment to the paragraph using the Paragraph->AppendComment(LPCWSTR_S text) method. Then set the author name and ID for the comment.
  • Initialize an instance of the DocPicture class.
  • Load a picture using the DocPicture->LoadImageSpire(LPCWSTR_S imgFile) method. Then set the width and height for the picture.
  • Add a paragraph to the comment. Then add the picture to the paragraph using the Paragraph->GetChildObjects()->Add() method.
  • Save the result document using the Document->SaveToFile(LPCWSTR_S fileName, FileFormat fileFormat) method.
  • C++
#include "Spire.Doc.o.h"

using namespace Spire::Doc;

int main()
{
	//Initialize an instance of the Document class
	intrusive_ptr<Document> document = new Document();
	//Load a Word document
	document->LoadFromFile(L"Input.docx");

	//Get the first section of the document
	intrusive_ptr<Section> section = document->GetSections()->GetItemInSectionCollection(0);

	//Get the first paragraph of the first section
	intrusive_ptr<Paragraph> paragraph = section->GetParagraphs()->GetItemInParagraphCollection(0);

	//Add a comment to the paragraph
	intrusive_ptr<Comment> comment = paragraph->AppendComment(L"Spire.Doc for Cpp");
	comment->GetFormat()->SetAuthor(L"E-iceblue");
	comment->GetFormat()->SetCommentId(0);

	//Load a picture
	intrusive_ptr<DocPicture> docPicture = new DocPicture(document);
	docPicture->LoadImageSpire(L"Doc-CPP.png");
	//Set picture width and height
	docPicture->SetWidth(100);
	docPicture->SetHeight(100);

	//Add the picture to the comment
	intrusive_ptr<Paragraph> picParagraph = comment->GetBody()->AddParagraph();
	picParagraph->GetChildObjects()->Add(docPicture);

	//Save the result document
	document->SaveToFile(L"AddCommentWithPicture.docx", FileFormat::Docx2019);
	document->Close();
}

C++: Add, Reply to, or Delete Comments in Word Documents

Reply to a Comment in Word in C++

When reviewing a comment added by others, you may want to reply to it to provide your insights. The following steps explain how to reply to an existing comment in a Word document using Spire.Doc for C++:

  • Initialize an instance of the Document class.
  • Load a Word document using the Document->LoadFromFile(LPCWSTR_S fileName) method.
  • Get a specific comment in the document using the Document-> GetComments()->GetItem(int index) method.
  • Initialize an instance of the Comment class to create a new comment. Then set the content, author, and ID for the comment.
  • Add the new comment as a reply to the specific comment using the Comment->ReplyToComment(intrusive_ptr<Comment> replyComment) method.
  • Save the result document using the Document->SaveToFile(LPCWSTR_S fileName, FileFormat fileFormat) method.
  • C++
#include "Spire.Doc.o.h"

using namespace Spire::Doc;

int main()
{
	//Initialize an instance of the Document class
	intrusive_ptr<Document> document = new Document();
	//Load a Word document
	document->LoadFromFile(L"AddCommentToParagraph.docx");
	
	//Get the first comment of the document
	intrusive_ptr<Comment> comment = document->GetComments()->GetItem(0);

	//Create a comment and set its author, ID and content
	intrusive_ptr<Comment> replyComment = new Comment(document);
	replyComment->GetFormat()->SetAuthor(L"Michael");
	replyComment->GetFormat()->SetCommentId(1);
	replyComment->GetBody()->AddParagraph()->AppendText(L"Spire.Doc is a wonderful Word library.");

	//Add the new comment as a reply to the first comment
	comment->ReplyToComment(replyComment);

	//Save the result document
	document->SaveToFile(L"ReplyToComment.docx", FileFormat::Docx2019);
	document->Close();
}

C++: Add, Reply to, or Delete Comments in Word Documents

Delete a Comment from Word in C++

After you have finished revising the file and want to publish a formal version, you may need to remove the comments from the file. The following steps show you how to delete a comment from a Word document using Spire.Doc for C++:

  • Initialize an instance of the Document class.
  • Load a Word document using the Document->LoadFromFile(LPCWSTR_S fileName) method.
  • Delete a specific comment from the document using the Document->GetComments()->RemoveAt(int index) method.
  • Save the result document using the Document->SaveToFile(LPCWSTR_S fileName, FileFormat fileFormat) method.
  • C++
#include "Spire.Doc.o.h"

using namespace Spire::Doc;

int main()
{
	//Initialize an instance of the Document class
	intrusive_ptr<Document> document = new Document();
	//Load a Word document
	document->LoadFromFile(L"AddCommentToParagraph.docx");

	//Delete the first comment from the document
	document->GetComments()->RemoveAt(0);

	//Save the result document
	document->SaveToFile(L"DeleteComment.docx", FileFormat::Docx2019);
	document->Close();
}

C++: Add, Reply to, or Delete Comments in Word Documents

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.

Published in Comment
Friday, 19 May 2023 01:01

C++: Create Lists in a Word Document

If you want to make your paragraphs easier to navigate and read, rearranging them into alphabetical, numerical, or even bullet order will allow your readers to quickly find what they are looking for and search through the list in an instant. In this article, you will learn how to create numbered lists, bulleted lists, and multilevel lists in a Word document in C++ 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

Create a Numbered List in Word in C++

Spire.Doc for C++ offers the ListStyle class that you can use to create a numbered list style or a bulleted style. Then, the list style can be applied to a paragraph using Paragraph->GetListFormat()->ApplyStyle() method. The steps to create a numbered list are as follows.

  • Create a Document object.
  • Add a section using Document->AddSection() method.
  • Create an instance of ListStyle class, specifying the list type to Numbered.
  • Get a specific level of the list using ListStyle->GetLevels()->GetItem(index) method, and set the numbering type using ListLevel->SetPatternType() method.
  • Add the list style to the document using Document->GetListStyles()->Add() method.
  • Add several paragraphs to the document using Section->AddParagraph() method.
  • Apply the list style to a specific paragraph using Paragraph->GetListFormat()->ApplyStyle() method.
  • Specify the list level using Paragraph->GetListFormat()->GetListLevelNumber() method.
  • Save the document to a Word file using Document->SaveToFile() method.
  • C++
#include "Spire.Doc.o.h";

using namespace Spire::Doc;
using namespace std;

int main() {

    //Create a Document object
    intrusive_ptr<Document> document = new Document();

    //Add a section
    intrusive_ptr<Section> section = document->AddSection();

    //Create a numbered list style
    intrusive_ptr<ListStyle> listStyle = new ListStyle(document, ListType::Numbered);
    listStyle->SetName(L"numberedList");
    listStyle->GetLevels()->GetItem(0)->SetPatternType(ListPatternType::DecimalEnclosedParen);
    listStyle->GetLevels()->GetItem(0)->SetTextPosition(20);
    document->GetListStyles()->Add(listStyle);

    //Add a paragraph
    intrusive_ptr<Paragraph> paragraph = section->AddParagraph();
    paragraph->AppendText(L"Required Web Development Skills:");
    paragraph->GetFormat()->SetAfterSpacing(5);

    //Add a paragraph and apply the numbered list style to it
    paragraph = section->AddParagraph();
    paragraph->AppendText(L"HTML");
    paragraph->GetListFormat()->ApplyStyle(L"numberedList");
    paragraph->GetListFormat()->SetListLevelNumber(0);

    //Add another four paragraphs and apply the numbered list style to them
    paragraph = section->AddParagraph();
    paragraph->AppendText(L"CSS");
    paragraph->GetListFormat()->ApplyStyle(L"numberedList");
    paragraph->GetListFormat()->SetListLevelNumber(0);

    paragraph = section->AddParagraph();
    paragraph->AppendText(L"C++Script");
    paragraph->GetListFormat()->ApplyStyle(L"numberedList");
    paragraph->GetListFormat()->SetListLevelNumber(0);

    paragraph = section->AddParagraph();
    paragraph->AppendText(L"Python");
    paragraph->GetListFormat()->ApplyStyle(L"numberedList");
    paragraph->GetListFormat()->SetListLevelNumber(0);

    paragraph = section->AddParagraph();
    paragraph->AppendText(L"MySQL");
    paragraph->GetListFormat()->ApplyStyle(L"numberedList");
    paragraph->GetListFormat()->SetListLevelNumber(0);

    //Save the document to file
document->SaveToFile(L"output/NumberedList.docx", FileFormat::Docx2019);
document->Dispose();
}

C++: Create Lists in a Word Document

Create a Bulleted List in Word in C++

The process of creating a bulleted list is similar to that of creating a numbered list. The difference is that when creating a list style, you must specify the list type as Bulleted and set a bullet symbol for it. The following are the detailed steps.

  • Create a Document object.
  • Add a section using Document->AddSection() method.
  • Create an instance of ListStyle class, specifying the list type to Bulleted.
  • Get a specific level of the list using ListStyle->GetLevels()->Get(index) method, and set the bullet symbol using ListLevel->SetBulletCharacter() method.
  • Add the list style to the document using Document->GetListStyles()->Add() method.
  • Add several paragraphs to the document using Section->AddParagraph() method.
  • Apply the list style to a specific paragraph using Paragraph->GetListFormat()->ApplyStyle() method.
  • Specify the list level using Paragraph->GetListFormat()->SetListLevelNumber() method.
  • Save the document to a Word file using Document->SaveToFile() method.
  • C++
#include "Spire.Doc.o.h";

using namespace Spire::Doc;
using namespace std;

int main() {

    //Create a Document object
    intrusive_ptr<Document> document = new Document();

    //Add a section
    intrusive_ptr<Section> section = document->AddSection();

    //Create a bulleted list style
    intrusive_ptr<ListStyle> listStyle = new ListStyle(document, ListType::Bulleted);
    listStyle->SetName(L"bulletedList");
    listStyle->GetLevels()->GetItem(0)->SetBulletCharacter(L"\u00B7");
    listStyle->GetLevels()->GetItem(0)->GetCharacterFormat()->SetFontName(L"Symbol");
    listStyle->GetLevels()->GetItem(0)->SetTextPosition(20);
    document->GetListStyles()->Add(listStyle);

    //Add a paragraph
    intrusive_ptr<Paragraph> paragraph = section->AddParagraph();
    paragraph->AppendText(L"Computer Science Subjects:");
    paragraph->GetFormat()->SetAfterSpacing(5);

    //Add a paragraph and apply the bulleted list style to it
    paragraph = section->AddParagraph();
    paragraph->AppendText(L"Data Structure");
    paragraph->GetListFormat()->ApplyStyle(L"bulletedList");
    paragraph->GetListFormat()->SetListLevelNumber(0);

    //Add another five paragraphs and apply the bulleted list style to them
    paragraph = section->AddParagraph();
    paragraph->AppendText(L"Algorithm");
    paragraph->GetListFormat()->ApplyStyle(L"bulletedList");
    paragraph->GetListFormat()->SetListLevelNumber(0);

    paragraph = section->AddParagraph();
    paragraph->AppendText(L"Computer Networks");
    paragraph->GetListFormat()->ApplyStyle(L"bulletedList");
    paragraph->GetListFormat()->SetListLevelNumber(0);

    paragraph = section->AddParagraph();
    paragraph->AppendText(L"Operating System");
    paragraph->GetListFormat()->ApplyStyle(L"bulletedList");
    paragraph->GetListFormat()->SetListLevelNumber(0);

    paragraph = section->AddParagraph();
    paragraph->AppendText(L"C Programming");
    paragraph->GetListFormat()->ApplyStyle(L"bulletedList");
    paragraph->GetListFormat()->SetListLevelNumber(0);

    paragraph = section->AddParagraph();
    paragraph->AppendText(L"Theory of Computations");
    paragraph->GetListFormat()->ApplyStyle(L"bulletedList");
    paragraph->GetListFormat()->SetListLevelNumber(0);

    //Save the document to file
document->SaveToFile(L"output/BulletedList.docx", FileFormat::Docx2019);
document->Dispose();
}

C++: Create Lists in a Word Document

Create a Multilevel Numbered List in Word in C++

A multilevel list consists of at least two different levels. Each level of a nested list can be accessed using ListStyle->GetLevels()->GetItem(index) method. Through ListLevel object, you can set the numbering type and prefix for a certain level. The following are the steps to create a multilevel numbered list in Word.

  • Create a Document object.
  • Add a section using Document->AddSection() method.
  • Create an instance of ListStyle class, specifying the list type to Numbered.
  • Get a specific level of the list using ListStyle->GetLevels()->GetItem(index) method, and set the numbering type and prefix.
  • Add the list style to the document using Document->GetListStyles()->Add() method.
  • Add several paragraphs to the document using Section->AddParagraph() method.
  • Apply the list style to a specific paragraph using Paragraph->GetListFormat()->ApplyStyle() method.
  • Specify the list level using Paragraph->GetListFormat()->SetListLevelNumber() method.
  • Save the document to a Word file using Document->SaveToFile() method.
  • C++
#include "Spire.Doc.o.h";

using namespace Spire::Doc;
using namespace std;

int main() {

    //Create a Document object
    intrusive_ptr<Document> document = new Document();

    //Add a section
    intrusive_ptr<Section> section = document->AddSection();

    //Create a numbered list style, specifying number prefix and pattern type of each level
    intrusive_ptr<ListStyle> listStyle = new ListStyle(document, ListType::Numbered);
    listStyle->SetName(L"nestedStyle");
    listStyle->GetLevels()->GetItem(0)->SetPatternType(ListPatternType::Arabic);
    listStyle->GetLevels()->GetItem(0)->SetTextPosition(20);
    listStyle->GetLevels()->GetItem(1)->SetNumberPrefix(L"%1.");
    listStyle->GetLevels()->GetItem(1)->SetPatternType(ListPatternType::Arabic);
    listStyle->GetLevels()->GetItem(2)->SetNumberPrefix(L"%1.%2.");
    listStyle->GetLevels()->GetItem(2)->SetPatternType(ListPatternType::Arabic);
    document->GetListStyles()->Add(listStyle);

    //Add a paragraph
    intrusive_ptr<Paragraph> paragraph = section->AddParagraph();
    paragraph->AppendText(L"Here's a Multi-Level Numbered List:");
    paragraph->GetFormat()->SetAfterSpacing(5);

    //Add a paragraph and apply the numbered list style to it
    paragraph = section->AddParagraph();
    paragraph->AppendText(L"The first item");
    paragraph->GetListFormat()->ApplyStyle(L"nestedStyle");
    paragraph->GetListFormat()->SetListLevelNumber(0);

    //Add another five paragraphs and apply the numbered list stype to them
    paragraph = section->AddParagraph();
    paragraph->AppendText(L"The second item");
    paragraph->GetListFormat()->ApplyStyle(L"nestedStyle");
    paragraph->GetListFormat()->SetListLevelNumber(0);

    paragraph = section->AddParagraph();
    paragraph->AppendText(L"The first sub-item");
    paragraph->GetListFormat()->ApplyStyle(L"nestedStyle");
    paragraph->GetListFormat()->SetListLevelNumber(1);

    paragraph = section->AddParagraph();
    paragraph->AppendText(L"The second sub-item");
    paragraph->GetListFormat()->ContinueListNumbering();
    paragraph->GetListFormat()->ApplyStyle(L"nestedStyle");

    paragraph = section->AddParagraph();
    paragraph->AppendText(L"A sub-sub-item");
    paragraph->GetListFormat()->ApplyStyle(L"nestedStyle");
    paragraph->GetListFormat()->SetListLevelNumber(2);

    paragraph = section->AddParagraph();
    paragraph->AppendText(L"The third item");
    paragraph->GetListFormat()->ApplyStyle(L"nestedStyle");
    paragraph->GetListFormat()->SetListLevelNumber(0);

    //Save the document to file
    document->SaveToFile(L"output/MultilevelNumberedList.docx", FileFormat::Docx2019);
    document->Dispose();
}

C++: Create Lists in a Word Document

Create a Multilevel Mixed-Type List in Word in C++

A multilevel list can be a combination of numbered lists and bulleted lists. To create a mixed-type list, you just need to create a numbered list style and a bulleted list style and apply them to different paragraphs. The detailed steps are as follows.

  • Create a Document object.
  • Add a section using Document->AddSection() method.
  • Create a numbered list style and a bulleted list style.
  • Add several paragraphs to the document using Section->AddParagraph() method.
  • Apply different list style to different paragraphs using Paragraph->GgetListFormat()->ApplyStyle() method.
  • Save the document to a Word file using Document->SaveToFile() method.
  • C++
#include "Spire.Doc.o.h";

using namespace Spire::Doc;
using namespace std;

int main() {

    //Create a Document object
    intrusive_ptr<Document> document = new Document();

    //Add a section
    intrusive_ptr<Section> section = document->AddSection();

    //Create a numbered list style
    intrusive_ptr<ListStyle> numberedListStyle = new ListStyle(document, ListType::Numbered);
    numberedListStyle->SetName(L"numberedStyle");
    numberedListStyle->GetLevels()->GetItem(0)->SetPatternType(ListPatternType::Arabic);
    numberedListStyle->GetLevels()->GetItem(0)->SetTextPosition(20);
    numberedListStyle->GetLevels()->GetItem(1)->SetPatternType(ListPatternType::LowLetter);
    document->GetListStyles()->Add(numberedListStyle);

    //Create a bulleted list style
    intrusive_ptr<ListStyle> bulletedListStyle = new ListStyle(document, ListType::Bulleted);
    bulletedListStyle->SetName(L"bulletedStyle");
    bulletedListStyle->GetLevels()->GetItem(2)->SetBulletCharacter(L"\u002A");
    bulletedListStyle->GetLevels()->GetItem(2)->GetCharacterFormat()->SetFontName(L"Symbol");
    document->GetListStyles()->Add(bulletedListStyle);

    //Add a paragraph
    intrusive_ptr<Paragraph> paragraph = section->AddParagraph();
    paragraph->AppendText(L"Here's a Multi-Level Mixed List:");
    paragraph->GetFormat()->SetAfterSpacing(5);

    //Add a paragraph and apply the numbered list style to it
    paragraph = section->AddParagraph();
    paragraph->AppendText(L"The first item");
    paragraph->GetListFormat()->ApplyStyle(L"numberedStyle");
    paragraph->GetListFormat()->SetListLevelNumber(0);

    //Add another five paragraphs and apply different list stype to them
    paragraph = section->AddParagraph();
    paragraph->AppendText(L"The first sub-item");
    paragraph->GetListFormat()->ApplyStyle(L"numberedStyle");
    paragraph->GetListFormat()->SetListLevelNumber(1);

    paragraph = section->AddParagraph();
    paragraph->AppendText(L"The second sub-item");
    paragraph->GetListFormat()->SetListLevelNumber(1);
    paragraph->GetListFormat()->ApplyStyle(L"numberedStyle");

    paragraph = section->AddParagraph();
    paragraph->AppendText(L"The first sub-sub-item");
    paragraph->GetListFormat()->ApplyStyle(L"bulletedStyle");
    paragraph->GetListFormat()->SetListLevelNumber(2);

    paragraph = section->AddParagraph();
    paragraph->AppendText(L"The second sub-sub-item");
    paragraph->GetListFormat()->ApplyStyle(L"bulletedStyle");
    paragraph->GetListFormat()->SetListLevelNumber(2);

    paragraph = section->AddParagraph();
    paragraph->AppendText(L"The second item");
    paragraph->GetListFormat()->ApplyStyle(L"numberedStyle");
    paragraph->GetListFormat()->SetListLevelNumber(0);

    //Save the document to file
document->SaveToFile(L"output/MultilevelMixedList.docx", FileFormat::Docx);
document->Dispose();
}

C++: Create Lists in a Word Document

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.

Published in List

Comparison of two versions of a document is the process of checking the new version against the previous one to identify changes made by different contributors. By comparing documents, legal staffs can easily review contracts to determine what changes have been made or still need to be made, and teachers can quickly compare student papers to determine whether or not necessary changes have been applied. In this article, you will learn how to compare two Word documents in C++ using Spire.Doc for C++.

The following is a screenshot of the two Word documents we’re going to compare.

C++: Compare Two Word Documents for Differences

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

Compare Two Word Documents in C++

Spire.Doc for C++ allows to compare two Word documents and save the result in a third document. When opening this document with MS Word, you can see all changes that have been made to the original document, including insertions, deletions as well as formatting modifications. The following are the detailed steps.

  • Load two Word documents separately while initialing two different Document objects.
  • Compare these two documents using Document->Compare() method.
  • Save the result in a third Word document using Document->SaveToFile() method.
  • C++
#include "Spire.Doc.o.h"

using namespace Spire::Doc;

int main() {

	//Load the first document
	Document* doc1 = new Document(L"C:\\Users\\Administrator\\Desktop\\Original.docx");

	//Load the second document
	Document* doc2 = new Document(L"C:\\Users\\Administrator\\Desktop\\Revised.docx");

	//Compare the second document on the basis of the first document 
	doc1->Compare(doc2, L"Patrick");

	//Save to a docx file
	doc1->SaveToFile(L"output/Result.docx", Spire::Doc::FileFormat::Docx2013);

	doc1->Close();
	doc2->Close();
	delete doc1;
	delete doc2;
}

C++: Compare Two Word Documents for Differences

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.

Published in Document Operation
Friday, 14 April 2023 01:15

C++: Change Font Color in Word

Changing the font color in a Word document can be an effective way to emphasize important points. For example, if you are creating a report that contains crucial data, changing the font color of the data text to a brighter color can make it stand out from other text and quickly grab your reader's attention. Another benefit of changing the font color is that it can enhance the visual appearance and readability of the document. For instance, when preparing marketing materials, changing the font color of headings and subheadings to a different font color than the rest of the text can help create a clear information hierarchy, making the materials more attractive and easier to read. In this article, we will demonstrate how to change the font color in a Word document in C++ 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

Change the Font Color of a Paragraph in Word in C++

To change the font color of a specific paragraph in a Word document, you can create a custom paragraph style with a specific font color, then add the style to the document and apply it to the paragraph you want to modify. The detailed steps are as follows:

  • Create an instance of the Document class.
  • Load a Word document using the Document->LoadFromFile() method.
  • Access a specific section in the document by its index using the Document->GetSections()->GetItem(int index) method.
  • Access the paragraph you want to modify by its index using the Section->GetParagraphs()->GetItem(int index) method.
  • Create an instance of the ParagraphStyle class to define a custom paragraph style.
  • Set the name and font color of the paragraph style using the ParagraphStyle->SetName() and ParagraphStyle->GetCharacterFormat()->SetTextColor() methods.
  • Add the custom paragraph style to the document using the Document->GetStyles()->Add() method.
  • Apply the custom paragraph style to the specific paragraph using the Paragraph->ApplyStyle() method.
  • Save the modified document using the Document->SaveToFile() method.
  • C++
#include "Spire.Doc.o.h";

using namespace Spire::Doc;
using namespace Spire::Common;

int main()
{
    //Create a Document instance
    Document* document = new Document();
    //Load a Word document
    document->LoadFromFile(L"Sample.docx");

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

    //Change the font color of the first Paragraph
    Paragraph* p1 = section->GetParagraphs()->GetItem(0);
    ParagraphStyle* style1 = new ParagraphStyle(document);
    style1->SetName(L"Color1");
    style1->GetCharacterFormat()->SetTextColor(Color::GetRosyBrown());
    document->GetStyles()->Add(style1);
    p1->ApplyStyle(style1);

    //Change the font color of the second Paragraph
    Paragraph* p2 = section->GetParagraphs()->GetItem(1);
    ParagraphStyle* style2 = new ParagraphStyle(document);
    style2->SetName(L"Color2");
    style2->GetCharacterFormat()->SetTextColor(Color::GetDarkGreen());
    document->GetStyles()->Add(style2);
    p2->ApplyStyle(style2);

    //Save the result document
    document->SaveToFile(L"ChangeFontColorForParagraph.docx", FileFormat::Docx2013);
    document->Close();
    delete document;
}

C++: Change Font Color in Word

Change the Font Color of a Specific Text in Word in C++

To change the font color of a specific text in a Word document, you need to search for the text in the document, then change the font color of its all occurrences. The detailed steps are as follows:

  • Create an instance of the Document class.
  • Load a Word document using the Document->LoadFromFile() method.
  • Find the text that you want to change the font color of using the Document->FindAllString() method.
  • Iterate through all occurrences of the text and change the font color of each occurrence using the TextSelection->GetAsOneRange()->GetCharacterFormat()->SetTextColor() method.
  • Save the result document using the Document->SaveToFile() method.
  • C++
#include "Spire.Doc.o.h";

using namespace Spire::Doc;
using namespace Spire::Common;
using namespace std;

int main()
{
	//Create a Document instance
	Document* document = new Document();
	//Load a Word document
	document->LoadFromFile(L"Sample.docx");

	//Find the text that you want to change the font color of
	vector<TextSelection*> selection = document->FindAllString(L"Spire.Doc for C++", false, true);

	//Change the font color of all occurrences of the text
	for (auto text : selection)
	{
		text->GetAsOneRange()->GetCharacterFormat()->SetTextColor(Color::GetRed());
	}

	//Save the result document
	document->SaveToFile(L"ChangeFontColorForCertainText.docx", FileFormat::Docx2013);
    	document->Close();
	delete document;
}

C++: Change Font Color 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.

Published in Font

A bookmark in a Word document enables you to navigate to any specific place within the document without having to scroll through large blocks of text. It functions just like an internal link between sections of your document. Bookmarks are particularly useful for navigating through lengthy documents. In this article, you will learn how to add or remove bookmarks in Word documents in C++ 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

Add a Bookmark to a Paragraph in C++

Bookmarks are usually created based on whole paragraphs, especially when the paragraphs themselves are headings. The following are the steps to add a bookmark to a paragraph using Spire.Doc for C++.

  • Create a Document object.
  • Load a Word file using Document->LoadFromFile() method.
  • Get a specific section using Document->GetSections()->GetItem() method.
  • Get a specific paragraph from the section using Section->GetParagraphs()->GetItem() method.
  • Create a BookmarkStart object, and insert it at the beginning of the paragraph using Paragraph->GetChildObjects()->Insert(int index, Spire::Doc::IDocumentObject *entity) method.
  • Append a BookmarkEnd object at the end of the paragraph using Paragraph->AppendBookmarkEnd(LPCWSTR_S name) method.
  • Save the document to another Word file using Document->SaveToFile() method.
  • C++
#include "Spire.Doc.o.h";

using namespace Spire::Doc;

int main() {

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

	//Load a Word file
	document->LoadFromFile(L"C:\\Users\\Administrator\\Desktop\\sample2.docx");

	//Get a specific paragraph
	Paragraph* paragraph = document->GetSections()->GetItem(0)->GetParagraphs()->GetItem(1);

	//Create a bookmark start
	BookmarkStart* start = new BookmarkStart(document, L"myBookmark");

	//Insert the bookmark start before the selected text
	paragraph->GetChildObjects()->Insert(0, start);

	//Append a bookmark end at the end of the paragraph
	paragraph->AppendBookmarkEnd(L"myBookmark");

	//Save the document to another Word file
	document->SaveToFile(L"Output/AddBookmarkToParagraph.docx", FileFormat::Docx2013);
	document->Close();
	delete document;
}

C++: Add or Remove Bookmarks in Word Documents

Add a Bookmark to Selected Text in C++

A bookmark can also be inserted to a specific position within a paragraph. The following are the steps to add a bookmark to selected text using Spire.Doc for C++.

  • Create a Document object.
  • Load a Word file using Document->LoadFromFile() method.
  • Find the selected text from the document using Document->FindAllString() method.
  • Find the owner paragraph of the text using TextSelection-> ->GetAsOneRange()->GetOwnerParagraph() method.
  • Get the index of the text in the paragraph using Paragraph->GetChildObjects()->IndexOf() method.
  • Create a BookmarkStart object, and insert it before the selected text using Paragraph->GetChildObjects()->Insert(int index, Spire::Doc::IDocumentObject *entity) method.
  • Create a BookmarkEnd object, and insert it after the selected text using Paragraph->GetChildObjects()->Insert(int index, Spire::Doc::IDocumentObject *entity) method.
  • Save the document to another Word file using Document->SaveToFile() method.
  • C++
#include "Spire.Doc.o.h";

using namespace Spire::Doc;
using namespace std;

int main() {

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

	//Load a Word file
	document->LoadFromFile(L"C:\\Users\\Administrator\\Desktop\\sample2.docx");

    //Specify text to find
    wstring stringToFind = L"business-to-consumer";

    //Find the text from the document
    vector<TextSelection*>  finds = document->FindAllString(stringToFind.c_str(), false, true);
    TextSelection* specificText = finds[0];

    //Find the paragraph where the text is located
    Paragraph* para = specificText->GetAsOneRange()->GetOwnerParagraph();

    //Get the index of the text in the paragraph
    int index = para->GetChildObjects()->IndexOf(specificText->GetAsOneRange());

    //Create a bookmark start
    BookmarkStart* start = new BookmarkStart(document, L"myBookmark");

    //Insert the bookmark start before the selected text
    para->GetChildObjects()->Insert(index, start);

    //Create a bookmark end
    BookmarkEnd* end = new BookmarkEnd(document, L"myBookmark");

    //Insert the bookmark end after the selected text
    para->GetChildObjects()->Insert(index + 2, end);

	//Save the document to another Word file
	document->SaveToFile(L"Output/AddBookmarkToText.docx", FileFormat::Docx2013);
	document->Close();
	delete document;
}

C++: Add or Remove Bookmarks in Word Documents

Remove Bookmarks from a Word Document in C++

Using Spire.Doc for C++, you can easily get and remove all bookmarks or a particular bookmark from a Word document. The following are the detailed steps.

  • Create a Document object.
  • Load a Word file containing bookmarks using Document->LoadFromFile() method.
  • Get a specific bookmark using Document->GetBookmarks()->GetItem() method.
  • Remove the bookmark using Document->GetBookmarks()->Remove() method. To remove all bookmarks at once, use Document->GetBookmarks()->Clear() method.
  • Save the document to another Word file using Document->SaveToFile() method.
  • C++
#include "Spire.Doc.o.h";

using namespace Spire::Doc;

int main() {

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

	//Load a Word file
	document->LoadFromFile(L"C:\\Users\\Administrator\\Desktop\\Bookmarks.docx");

	//Get a specific bookmark by its index
	Bookmark* bookmark = document->GetBookmarks()->GetItem(0);

	//Remove the bookmark
	document->GetBookmarks()->Remove(bookmark);

	//Remove all bookmarks
	//document->GetBookmarks()->Clear();

	//Save the document to another Word file
	document->SaveToFile(L"Output/RemoveBookmarks.docx", FileFormat::Docx2013);
	document->Close();
	delete document;
}

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.

Published in Bookmark
Monday, 03 April 2023 01:15

C++: Protect or Unprotect Word Documents

Document security is particularly important when critical or private data is involved in a Word document. There are several security options in Word that you can use to protect your documents, including password protection, read-only mode, editing restrictions, and partial protection. On the contrary, when the protection is no longer required, you may need to unprotect a Word document to improve work efficiency.

In this article, you will learn how to protect or unprotect Word documents in C++ 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

Password Protect a Word Document in C++

Encrypting a document with a password makes sure that only you and certain people can read or edit it. The following are the steps to password protect a Word document using Spire.Doc for C++.

  • Create a Document object.
  • Load a Word document using Document->LoadFromFile() method.
  • Encrypt the document with a password using Document->Eencrypt(LPCWSTR_S password) method.
  • Save the document to another Word file using Document->SaveToFile() method.
  • C++
#include "Spire.Doc.o.h";

using namespace Spire::Doc;

int main() {

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

    //Load a Word file
    document->LoadFromFile(L"C:\\Users\\Administrator\\Desktop\\sample.docx");

    //Encrypt the document with a password
    document->Encrypt(L"open-psd");

    //Save the document to another Word file
    document->SaveToFile(L"Output/Encryption.docx", FileFormat::Docx2013);
    document->Close();
    delete document;
}

C++: Protect or Unprotect Word Documents

Restrict Editing of a Word Document in C++

If you want to grant people permission to read your document but restrict the types of modifications that someone can make, you can protect your document with a specific protection type and a permission password.

Protection Type Description
AllowOnlyComments Modification of comments in the document is allowed.
AllowOnlyFormFields The user can only enter data in the form fields in the document.
AllowOnlyReading The document is read-only.
AllowOnlyRevisions The user can only add revision marks to the document.
NoProtection The document is not protected.

The following are the steps to restrict editing of a Word document using Spire.Doc for C++.

  • Create a Document object.
  • Load a Word document using Document->LoadFromFile() method.
  • Protect the document by specifying the protection type and the permission password using Document->Protect(Spire::Doc::ProtectionType type, LPCWSTR_S password) method.
  • Save the document to another Word file using Document->SaveToFile() method.
  • C++
#include "Spire.Doc.o.h";

using namespace Spire::Doc;

int main() {

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

    //Load a Word file
    document->LoadFromFile(L"C:\\Users\\Administrator\\Desktop\\sample.docx");

    //Protect the document by specifying the protection type and the password
    document->Protect(ProtectionType::AllowOnlyReading, L"permission-psd");

    //Save the document to another Word file
    document->SaveToFile(L"Output/RestrictEditing.docx", FileFormat::Docx2013);
    document->Close();
    delete document;
}

C++: Protect or Unprotect Word Documents

Protect Sections of a Word Document in C++

Word allows you to lock some sections of your Word document and leave the rest available for editing. The following are the steps to protect selected sections of a Word document using Spire.Doc for C++.

  • Create a Document object.
  • Load a Word document using Document->LoadFromFile() method.
  • Set the protection type to AllowOnlyFormFields using Document->Protect() method.
  • Unprotect a particular section by passing false as an argument to the Section->SetProtectForm() method. Other sections will continue to be protected.
  • Save the document to another Word file using Document->SaveToFile() method.
  • C++
#include "Spire.Doc.o.h";

using namespace Spire::Doc;

int main() {

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

    //Load a Word file
    document->LoadFromFile(L"C:\\Users\\Administrator\\Desktop\\sample.docx");

    //Set the protection type as "AllowOnlyFormFields"
    document->Protect(ProtectionType::AllowOnlyFormFields, L"permission-psd");

    //Unprotect section 2
    document->GetSections()->GetItem(1)->SetProtectForm(false);

    //Save the document to another Word file
    document->SaveToFile(L"Output/ProtectSections.docx", FileFormat::Docx2013);
    document->Close();
    delete document;
}

C++: Protect or Unprotect Word Documents

Create Editable Regions in a Word Document in C++

Aside from making certain sections editable, you can create editable regions based on text ranges in order to narrow down the scope of changes that users are able to make. The following are the steps to create editable regions in a read-only Word document using Spire.Doc for C++.

  • Create a Document object.
  • Load a Word file using Document->LoadFromFile() method.
  • Set the protection type to AllowOnlyReading using Document->Protect() method.
  • Create a PermissionStart object and a PermissionEnd object.
  • Insert the PermissionStart object at the beginning of a paragraph using DocumentObjectCollection->Insert(int index, Spire::Doc::lDocumentObject *entity) method, which indicates the start of an editable region.
  • Add the PermissionEnd object at the end of a paragraph using DocumentObjectCollection->Add(Spire::Doc::lDocumentObject *entity) method, which indicates the end of an editable region.
  • Save the document to another Word file using Document->SaveToFile() method.
  • C++
#include "Spire.Doc.o.h";

using namespace Spire::Doc;

int main() {

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

    //Load a Word file
    document->LoadFromFile(L"C:\\Users\\Administrator\\Desktop\\sample2.docx");

    //Set the protect type to AllowOnlyReading
    document->Protect(ProtectionType::AllowOnlyReading, L"permission-psd");

    //Create tags for permission start and end
    PermissionStart* start = new PermissionStart(document, L"regionOne");
    PermissionEnd* end = new PermissionEnd(document, L"regionOne");

    //Add the start and end tags to allow the selected paragraphs to be editable
    document->GetSections()->GetItem(0)->GetParagraphs()->GetItem(0)->GetChildObjects()->Insert(0, start);
    document->GetSections()->GetItem(0)->GetParagraphs()->GetItem(1)->GetChildObjects()->Add(end);

    //Save the document to another Word file
    document->SaveToFile(L"Output/SetEditableRegions.docx", FileFormat::Docx2013);
    document->Close();
    delete document;
}

C++: Protect or Unprotect Word Documents

Remove Editable Regions from a Word Document in C++

In order to remove the editable regions, you need to find the "PermissionStart" and "PermissionEnd" tags in the document and remove them. The following are the detailed steps.

  • Create a Document object.
  • Load a Word document containing editable regions using Document->LoadFromFile() method.
  • Traverse through all the child objects in the document, and determine if a certain child object is an instance of PermissionStart class or PermissionEnd class. If yes, remove the child object from the paragraph using Paragraph->GetChildObjects()->Remove(Spire::Doc::IDocumentObject *entity) method.
  • Save the document to another Word file using Document->SaveToFile() method.
  • C++
#include "Spire.Doc.o.h";

using namespace Spire::Doc;

int main() {

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

    //Load a Word file
    document->LoadFromFile(L"C:\\Users\\Administrator\\Desktop\\SetEditableRegions.docx");

	//Find "PermissionStart" and "PermissionEnd" tags and remove them
	for (int i = 0; i < document->GetSections()->GetCount(); i++)
	{
		Section* section = document->GetSections()->GetItem(i);
		for (int j = 0; j < section->GetBody()->GetParagraphs()->GetCount(); j++)
		{
			Paragraph* para = section->GetBody()->GetParagraphs()->GetItem(j);
			for (int k = 0; k < para->GetChildObjects()->GetCount(); k++)
			{
				DocumentObject* obj = para->GetChildObjects()->GetItem(k);
				if (dynamic_cast<PermissionStart*>(obj) != nullptr || dynamic_cast<PermissionEnd*>(obj) != nullptr)
				{
					para->GetChildObjects()->Remove(obj);
				}
				else
				{
					k++;
				}
			}
		}
	}

    //Save the document to another Word file
    document->SaveToFile(L"Output/RemoveEditableRegions.docx", FileFormat::Docx2013);
    document->Close();
    delete document;
}

Remove Restrictions from a Word Document in C++

Spire.Doc for C++ allows you to remove editing restrictions without knowing the permission password. The following are the detailed steps.

  • Create a Document object.
  • Load a Word document containing editing restrictions using Document->LoadFromFile() method.
  • Set the protection type to NoProtection using Document->Protect() method.
  • Save the document to another Word file using Document->SaveToFile() method.
  • C++
#include "Spire.Doc.o.h";

using namespace Spire::Doc;

int main() {

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

    //Load a Word file
    document->LoadFromFile(L"C:\\Users\\Administrator\\Desktop\\RestrictEditing.docx");

    //Set the protect type to NoProtection
    document->Protect(ProtectionType::NoProtection);

    //Save the document to another Word file
    document->SaveToFile(L"Output/RemoveRestrictions.docx", FileFormat::Docx2013);
    document->Close();
    delete document;
}

Remove Password from a Password-Protected Word Document in C++

The password of an encrypted Word document can be removed if it is no longer needed. The following are the detailed steps.

  • Create a Document object.
  • Load a password-protected Word document using Document->LoadFromFile((LPCWSTR_S fileName, Spire::Doc::FileFormat fileFormat, LPCWSTR_S password) method.
  • Remove the password using Document->RemoveEncryption() method.
  • Save the document to another Word file using Document->SaveToFile() method.
  • C++
#include "Spire.Doc.o.h";

using namespace Spire::Doc;

int main() {

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

    //Load an encrypted Word file
    document->LoadFromFile(L"C:\\Users\\Administrator\\Desktop\\Encryption.docx", FileFormat::Docx, L"open-psd");

    //Remove the open password
    document->RemoveEncryption();

    //Save the document to another Word file
    document->SaveToFile(L"Output/RemovePassword.docx", FileFormat::Docx2013);
    document->Close();
    delete document;
}

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.

Published in Security

In Word, a watermark is a semi-transparent text or image that you can place in the background. Typically, watermarks are used to emphasize something important about a document. For example, you can use it to remind the user that the content is confidential or a draft. Or other times, you may want to add a washout mark to include the company logo in the document. In this article, you will learn how to insert text or image watermarks to Word documents in C++ 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

Insert a Text Watermark to Word in C++

Spire.Doc for C++ offers the TextWatermark class to represent a text watermark. The content and appearance of the watermark can be set using the methods under it. Once a text watermark is created, you can apply it to the whole document by using Document->SetWatermark() method. The following are the detailed steps.

  • Create a Document object.
  • Load a Word file using Document->LoadFromFile() method.
  • Create a TextWatermark object.
  • Se the content and appearance of the watermark using the methods under the TextWatermark object.
  • Apply the text watermark to the document using Document->SetWatermrak() method.
  • Save the document using Document->SaveToFile() method.
  • C++
#include "Spire.Doc.o.h";

using namespace Spire::Doc;
using namespace Spire::Common;
using namespace std;

int main() {

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

    //Load a Word file
    document->LoadFromFile(L"C:\\Users\\Administrator\\Desktop\\sample.docx");

    //Create a TextWatermark object
    TextWatermark* txtWatermark = new TextWatermark();

    //Set the content and format of the text watermark
    txtWatermark->SetText(L"CONFIDENTIAL");
    txtWatermark->SetFontSize(40);
    txtWatermark->SetSemitransparent(90);
    txtWatermark->SetFontName(L"Arial Black");
    txtWatermark->SetColor(Color::GetBlue());
    txtWatermark->SetLayout(WatermarkLayout::Diagonal);

    //Apply the text watermark to the document 
    document->SetWatermark(txtWatermark);

    //Save the document 
    document->SaveToFile(L"Output/TextWatermark.docx", FileFormat::Docx2013);
    document->Close();
    delete document;
}

C++: Insert Text or Image Watermarks to Word

Insert an Image Watermark to Word in C++

Likewise, an image watermark can be created using PictureWatermark class. Once created, you can apply it to a Word document using Document->SetWatermark() method. Here are the detailed steps.

  • Create a Document object.
  • Load a Word file using Document->LoadFromFile() method.
  • Create a PictureWatermark object.
  • Set the image and appearance of the watermark using the methods under the PictureWatermark object.
  • Apply the image watermark to the document using Document->SetWatermrak() method.
  • Save the document using Document->SaveToFile() method.
  • C++
#include "Spire.Doc.o.h";

using namespace Spire::Doc;
using namespace Spire::Common;
using namespace std;

int main() {

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

    //Load a Word file
    document->LoadFromFile(L"C:\\Users\\Administrator\\Desktop\\sample.docx");

    //Create a PictureWatermark object
    PictureWatermark* pictureWatermark = new PictureWatermark();

    //Specify image for the watermark
    pictureWatermark->SetPicture(Image::FromFile(L"C:\\Users\\Administrator\\Desktop\\company-png.png"));
    pictureWatermark->SetScaling(100);
    pictureWatermark->SetIsWashout(false);

    //Apply the image watermark to the document
    document->SetWatermark(pictureWatermark);

    //Save the document 
    document->SaveToFile(L"Output/ImageWatermark.docx", FileFormat::Docx2013);
    document->Close();
    delete document;
}

C++: Insert Text or Image Watermarks to 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.

Published in Watermark
Tuesday, 28 March 2023 01:06

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.

Published in Paragraph
Monday, 27 March 2023 01:27

C++: Create a Fillable Form in Word

Word allows you to create forms that other people can use to input information. Fillable forms are particularly useful when you need to gather data or feedback from many individuals and want to make sure that the formatting is consistent. The tools that you may need for creating a form include:

  • Content Controls: The areas where users input information in a form.
  • Tables: Tables are used in forms to align text and form fields, and to create borders and boxes.
  • Protection: Allows users to populate fields but not to make changes to the rest of the document.

Content controls in Word are containers for content that let users build structured documents. A structured document controls where content appears within the document. There are basically ten types of content controls available in Word 2013. This article focuses on how to create a fillable form in Word consisting of the following seven common content controls using Spire.Doc for C++.

Content Control Description
Plain Text A text field limited to plain text, so no formatting can be included.
Rich Text A text field that can contain formatted text or other items, such as tables, pictures, or other content controls.
Picture Accepts a single picture.
Drop-Down List A drop-down list displays a predefined list of items for the user to choose from.
Combo Box A combo box enables users to select a predefined value in a list or type their own value in the text box of the control.
Check Box A check box provides a graphical widget that allows the user to make a binary choice: yes (checked) or no (not checked).
Date Picker Contains a calendar control from which the user can select a date.

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

Create a Fillable Form in Word in C++

The StructureDocumentTagInline class provided by Spire.Doc for C++ is used to create structured document tags for inline-level structures (DrawingML object, fields, etc.) in a paragraph. The SDTProperties property and the SDTContent property under this class shall be used to specify the properties and content of the current structured document tag. The following are the detailed steps to create a fillable form with content controls in Word.

  • Create a Document object.
  • Add a section using Document->AddSection() method.
  • Add a table using Section->AddTable() method.
  • Add a paragraph to a specific table cell using TableCell->AddParagraph() method.
  • Create an instance of StructureDocumentTagInline class, and add it to the paragraph as a child object using Paragraph->GetChildObjects()->Add() method.
  • Specify the properties and content of the structured document tag using the methods under the SDTProperties property and the SDTContent property of the StructureDocumentTagInline object. The type of the structured document tag is set through SDTProperties->SetSDTType() method.
  • Prevent users from editing content outside form fields using Document->Protect() method.
  • Save the document using Document->SaveToFile() method.
  • C++
#include "Spire.Doc.o.h";

using namespace Spire::Doc;
using namespace std;

int main() {

    //Create a Document object
    Document* doc = new Document();

    //Add a section
    Section* section = doc->AddSection();

    //add a table
    Table* table = section->AddTable(true);
    table-> ResetCells(7, 2);

    //Add text to the cells of the first column
    Paragraph* paragraph = table->GetRows()->GetItem(0)->GetCells()->GetItem(0)->AddParagraph();
    paragraph->AppendText(L"Plain Text Content Control");
    paragraph = table->GetRows()->GetItem(1)->GetCells()->GetItem(0)->AddParagraph();
    paragraph->AppendText(L"Rich Text Content Control");
    paragraph = table->GetRows()->GetItem(2)->GetCells()->GetItem(0)->AddParagraph();
    paragraph->AppendText(L"Picture Content Control");
    paragraph = table->GetRows()->GetItem(3)->GetCells()->GetItem(0)->AddParagraph();
    paragraph->AppendText(L"Drop-Down List Content Control");
    paragraph = table->GetRows()->GetItem(4)->GetCells()->GetItem(0)->AddParagraph();
    paragraph->AppendText(L"Check Box Content Control");
    paragraph = table->GetRows()->GetItem(5)->GetCells()->GetItem(0)->AddParagraph();
    paragraph->AppendText(L"Combo box Content Control");
    paragraph = table->GetRows()->GetItem(6)->GetCells()->GetItem(0)->AddParagraph();
    paragraph->AppendText(L"Date Picker Content Control");

    //Add a plain text content control to the cell (0,1)
    paragraph = table->GetRows()->GetItem(0)->GetCells()->GetItem(1)->AddParagraph();
    StructureDocumentTagInline* sdt = new StructureDocumentTagInline(doc);
    paragraph->GetChildObjects()->Add(sdt);
    sdt->GetSDTProperties()->SetSDTType(SdtType::Text);
    sdt->GetSDTProperties()->SetAlias(L"Plain Text");
    sdt->GetSDTProperties()->SetTag(L"Plain Text");
    sdt->GetSDTProperties()->SetIsShowingPlaceHolder(true);
    SdtText* text = new SdtText(true);
    text->SetIsMultiline(false);
    sdt->GetSDTProperties()->SetControlProperties(text);
    TextRange* tr = new TextRange(doc);
    tr->SetText(L"Click or tap here to enter text.");
    sdt->GetSDTContent()->GetChildObjects()->Add(tr);

    //Add a rich text content control to the cell (1,1)
    paragraph = table->GetRows()->GetItem(1)->GetCells()->GetItem(1)->AddParagraph();
    sdt = new StructureDocumentTagInline(doc);
    paragraph->GetChildObjects()->Add(sdt);
    sdt->GetSDTProperties()->SetSDTType(SdtType::RichText);
    sdt->GetSDTProperties()->SetAlias(L"Rich Text");
    sdt->GetSDTProperties()->SetTag(L"Rich Text");
    sdt->GetSDTProperties()->SetIsShowingPlaceHolder(true);
    text = new SdtText(true);
    text->SetIsMultiline(false);
    sdt->GetSDTProperties()->SetControlProperties(text);
    tr = new TextRange(doc);
    tr->SetText(L"Click or tap here to enter text.");
    sdt->GetSDTContent()->GetChildObjects()->Add(tr);

    //Add a picture content control to the cell (2,1)
    paragraph = table->GetRows()->GetItem(2)->GetCells()->GetItem(1)->AddParagraph();
    sdt = new StructureDocumentTagInline(doc);
    paragraph->GetChildObjects()->Add(sdt);
    sdt->GetSDTProperties()->SetSDTType(SdtType::Picture);
    sdt->GetSDTProperties()->SetAlias(L"Picture");
    sdt->GetSDTProperties()->SetTag(L"Picture");
    SdtPicture* sdtPicture = new SdtPicture();
    sdt->GetSDTProperties()->SetControlProperties(sdtPicture);
    DocPicture* pic = new DocPicture(doc);
    pic->LoadImageSpire(L"C:\\Users\\Administrator\\Desktop\\ChooseImage.png");
    sdt->GetSDTContent()->GetChildObjects()->Add(pic);

    //Add a dropdown list content control to the cell(3,1)
    paragraph = table->GetRows()->GetItem(3)->GetCells()->GetItem(1)->AddParagraph();
    sdt = new StructureDocumentTagInline(doc);
    sdt->GetSDTProperties()->SetSDTType(SdtType::DropDownList);
    sdt->GetSDTProperties()->SetAlias(L"Dropdown List");
    sdt->GetSDTProperties()->SetTag(L"Dropdown List");
    paragraph->GetChildObjects()->Add(sdt);
    SdtDropDownList* sddl = new SdtDropDownList();
    sddl->GetListItems()->Add(new SdtListItem(L"Choose an item.", L"1"));
    sddl->GetListItems()->Add(new SdtListItem(L"Item 2", L"2"));
    sddl->GetListItems()->Add(new SdtListItem(L"Item 3", L"3"));
    sddl->GetListItems()->Add(new SdtListItem(L"Item 4", L"4"));
    sdt->GetSDTProperties()->SetControlProperties(sddl);
    tr = new TextRange(doc);
    tr->SetText(sddl->GetListItems()->GetItem(0)->GetDisplayText());
    sdt->GetSDTContent()->GetChildObjects()->Add(tr);

    //Add two check box content controls to the cell (4,1)
    paragraph = table->GetRows()->GetItem(4)->GetCells()->GetItem(1)->AddParagraph();
    sdt = new StructureDocumentTagInline(doc);
    paragraph->GetChildObjects()->Add(sdt);
    sdt->GetSDTProperties()->SetSDTType(SdtType::CheckBox);
    SdtCheckBox* scb = new SdtCheckBox();
    sdt->GetSDTProperties()->SetControlProperties(scb);
    tr = new TextRange(doc);
    sdt->GetChildObjects()->Add(tr);
    scb->SetChecked(false);
    paragraph->AppendText(L" Option 1");

    paragraph = table->GetRows()->GetItem(4)->GetCells()->GetItem(1)->AddParagraph();
    sdt = new StructureDocumentTagInline(doc);
    paragraph->GetChildObjects()->Add(sdt);
    sdt->GetSDTProperties()->SetSDTType(SdtType::CheckBox);
    scb = new SdtCheckBox();
    sdt->GetSDTProperties()->SetControlProperties(scb);
    tr = new TextRange(doc);
    sdt->GetChildObjects()->Add(tr);
    scb->SetChecked(false);
    paragraph->AppendText(L" Option 2");

    //Add a combo box content control to the cell (5,1)
    paragraph = table->GetRows()->GetItem(5)->GetCells()->GetItem(1)->AddParagraph();
    sdt = new StructureDocumentTagInline(doc);
    paragraph->GetChildObjects()->Add(sdt);
    sdt->GetSDTProperties()->SetSDTType(SdtType::ComboBox);
    sdt->GetSDTProperties()->SetAlias(L"Combo Box");
    sdt->GetSDTProperties()->SetTag(L"Combo Box");
    SdtComboBox* cb = new SdtComboBox();
    cb->GetListItems()->Add(new SdtListItem(L"Choose an item."));
    cb->GetListItems()->Add(new SdtListItem(L"Item 2"));
    cb->GetListItems()->Add(new SdtListItem(L"Item 3"));
    sdt->GetSDTProperties()->SetControlProperties(cb);
    tr = new TextRange(doc);
    tr->SetText(cb->GetListItems()->GetItem(0)->GetDisplayText());
    sdt->GetSDTContent()->GetChildObjects()->Add(tr);

    //Add a date picker content control to the cell (6,1)
    paragraph = table->GetRows()->GetItem(6)->GetCells()->GetItem(1)->AddParagraph();
    sdt = new StructureDocumentTagInline(doc);
    paragraph->GetChildObjects()->Add(sdt);
    sdt->GetSDTProperties()->SetSDTType(SdtType::DatePicker);
    sdt->GetSDTProperties()->SetAlias(L"Date Picker");
    sdt->GetSDTProperties()->SetTag(L"Date Picker");
    SdtDate* date = new SdtDate();
    date->SetCalendarType(CalendarType::Default);
    date->SetDateFormatSpire(L"yyyy.MM.dd");
    date->SetFullDate(DateTime::GetNow());
    sdt->GetSDTProperties()->SetControlProperties(date);
    tr = new TextRange(doc);
    tr->SetText(L"Click or tap to enter a date.");
    sdt->GetSDTContent()->GetChildObjects()->Add(tr);

    //Allow users to edit the form fields only
    doc->Protect(ProtectionType::AllowOnlyFormFields,L"permission-psd");

    //Save to file
    doc->SaveToFile(L"Output/WordForm.docx",FileFormat::Docx2013);
	doc->Close();
	delete doc;
}

C++: Create a Fillable Form 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.

Published in Form Field
Page 1 of 2