officefileapi-405432-word-processing-document-api-examples-export-how-to-generate-fillable-form.md
The Word Processing Document API allows you to generate a document with fillable fields. Fields remain interactive when you export the result to a PDF file.
Add content controls to your document to generate a fillable form. The following types can be converted to interactive forms:
Refer to the following help topic for more information on how to manage content controls: Content Controls in Word Processing Documents
The following code snippet inserts content controls to different table cells. Use the TableCell.ContentRange and TableCell.Range properties to obtain cell ranges.
using DevExpress.XtraPrinting;
using DevExpress.XtraRichEdit;
//...
using (var wordProcessor = new RichEditDocumentServer()) {
wordProcessor.LoadDocument(@"C:\Documents\appointment-form-template.docx");
var table = wordProcessor.Document.Tables[0];
var contentControls = wordProcessor.Document.ContentControls;
// Insert a date picker
contentControls.InsertDatePickerControl(table[2, 1].Range.Start);
var textBox = contentControls.InsertPlainTextControl(table[8, 1].Range.Start);
// Insert a plain text box
var nameTextPosition = wordProcessor.Document.CreatePosition(textBox.Range.Start.ToInt() + 1);
wordProcessor.Document.InsertText(nameTextPosition, "Click to enter a name");
// Insert a drop-down list
var dropDownList = contentControls.InsertDropDownListControl(table[9, 1].Range.Start);
dropDownList.AddItem("Annual physical examination", "Annual physical examination");
dropDownList.AddItem("Follow-up visit", "Follow-up visit");
dropDownList.AddItem("Specific concerns", "Specific concerns");
dropDownList.SelectedItemIndex = 0;
// Insert multiple check boxes
var cellParagraphs = wordProcessor.Document.Paragraphs.Get(table[11, 1].ContentRange);
contentControls.InsertCheckboxControl(cellParagraphs[0].Range.Start);
contentControls.InsertCheckboxControl(cellParagraphs[1].Range.Start);
contentControls.InsertCheckboxControl(cellParagraphs[2].Range.Start);
//...
}
Imports DevExpress.XtraPrinting
Imports DevExpress.XtraRichEdit
'...
Using wordProcessor = New RichEditDocumentServer()
wordProcessor.LoadDocument("C:\Documents\appointment-form-template.docx")
Dim table = wordProcessor.Document.Tables(0)
Dim contentControls = wordProcessor.Document.ContentControls
' Insert a date picker
contentControls.InsertDatePickerControl(table(2, 1).Range.Start)
Dim textBox = contentControls.InsertPlainTextControl(table(8, 1).Range.Start)
' Insert a plain text box
Dim nameTextPosition = wordProcessor.Document.CreatePosition(textBox.Range.Start.ToInt() + 1)
wordProcessor.Document.InsertText(nameTextPosition, "Click to enter a name")
' Insert a drop-down list
Dim dropDownList = contentControls.InsertDropDownListControl(table(9, 1).Range.Start)
dropDownList.AddItem("Annual physical examination", "Annual physical examination")
dropDownList.AddItem("Follow-up visit", "Follow-up visit")
dropDownList.AddItem("Specific concerns", "Specific concerns")
dropDownList.SelectedItemIndex = 0
' Insert multiple check boxes
Dim cellParagraphs = wordProcessor.Document.Paragraphs.Get(table(11, 1).ContentRange)
contentControls.InsertCheckboxControl(cellParagraphs(0).Range.Start)
contentControls.InsertCheckboxControl(cellParagraphs(1).Range.Start)
contentControls.InsertCheckboxControl(cellParagraphs(2).Range.Start)
'...
End Using
Create a new PdfExportOptions object and set its ExportEditingFieldsToAcroForms to true to convert editable fields to interactive fields during PDF export.
Call the RichEditDocumentServer.ExportToPdf method and pass the created PdfExportOptions instance as the parameter to export the document to PDF.
PdfExportOptions options = new PdfExportOptions();
options.ExportEditingFieldsToAcroForms = true;
wordProcessor.ExportToPdf("appointment-form.pdf", options);
Dim options As New PdfExportOptions()
options.ExportEditingFieldsToAcroForms = True
wordProcessor.ExportToPdf("appointment-form.pdf", options)