Back to Devexpress

How to: Use RichEditControl for WPF to Create a Fillable PDF Form in Code

wpf-405449-controls-and-libraries-rich-text-editor-examples-export-how-to-export-content-controls-to-pdf-acro-form.md

latest5.0 KB
Original Source

How to: Use RichEditControl for WPF to Create a Fillable PDF Form in Code

  • May 12, 2025
  • 2 minutes to read

The RichEditControl for WPF 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.

csharp
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);
//...
vb
Imports DevExpress.XtraPrinting
Imports DevExpress.XtraRichEdit

'...
richEditControl.LoadDocument("C:\Documents\appointment-form-template.docx")
Dim table = richEditControl.Document.Tables(0)

Dim contentControls = richEditControl.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 = richEditControl.Document.CreatePosition(textBox.Range.Start.ToInt() + 1)
richEditControl.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 = 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.

csharp
PdfExportOptions options = new PdfExportOptions();
options.ExportEditingFieldsToAcroForms = true;
richEditControl.ExportToPdf("appointment-form.pdf", options);
vb
Dim options As New PdfExportOptions()
options.ExportEditingFieldsToAcroForms = True
richEditControl.ExportToPdf("appointment-form.pdf", options)