How to: Use RichEditControl API to Create a Fillable PDF Form
- 2 minutes to read
The RichEditControl allows you to generate a document with fillable fields. Fields remain interactive when you export the result to a PDF file.
Generate a Fillable Form
Add content controls to your document to generate a fillable form. The following types can be converted to interactive forms:
- Check Box
- Combo Box
- Date Picker
- Drop-Down List
- Plain Text
- Picture
- Rich Text
Refer to the following help topic for more information on how to manage content controls: Content Controls in Rich Text 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;
//...
richEditControl.LoadDocument(@"C:\Documents\appointment-form-template.docx");
var table = richEditControl.Document.Tables[0];
var contentControls = richEditControl.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 = richEditControl.Document.CreatePosition(textBox.Range.Start.ToInt() + 1);
richEditControl.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 = richEditControl.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);
//...
Export the Document to PDF with Interactive Forms
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.