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.