Content Controls - Fill Form

This demo uses the DevExpress Word Processing Document API to generate a Word document with content controls.

Specify form field values in the panel below. If you generate a PDF file, content controls are exported as an interactive form.

Use the Populate the Form and Save as… dropdown button to select an output format, generate a document, and download the result.

Sample Document
ContentControls_AppointmentForm.docx



using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;

async Task<Stream> ProcessContentControlsDocument(Stream inputStream, AppointmentForm appointmentForm,
    bool isPdfOutput, bool exportAsAcroForm, DocumentFormat targetFormat) {
    using var wordProcessor = new RichEditDocumentServer();
    wordProcessor.LoadDocument(inputStream);
    var document = wordProcessor.Document;

    PopulateContentControls(document, appointmentForm);

    var outputStream = new MemoryStream();

    if (isPdfOutput) {
        DevExpress.XtraPrinting.PdfExportOptions options = new DevExpress.XtraPrinting.PdfExportOptions
        {
            ExportEditingFieldsToAcroForms = exportAsAcroForm
        };
        wordProcessor.ExportToPdf(outputStream, options);
    }
    else
        wordProcessor.SaveDocument(outputStream, targetFormat);

    outputStream.Position = 0;
    return outputStream;
}

void PopulateContentControls(Document document, AppointmentForm appointmentForm) {
    foreach (var control in document.ContentControls) {
        switch (control.Tag) {
            case "PatientName":
                UpdateControlText(document, (ContentControlPlainText)control, appointmentForm.PatientName);
                break;
            case "DateOfBirth":
                ((ContentControlDate)control).Date = appointmentForm.DateOfBirth;
                break;
            case "PhoneNumber":
                UpdateControlText(document, (ContentControlPlainText)control, appointmentForm.PhoneNumber);
                break;
            case "EmailAddress":
                UpdateControlText(document, (ContentControlPlainText)control, appointmentForm.EmailAddress);
                break;
            case "AppointmentDate":
                ((ContentControlDate)control).Date = appointmentForm.AppointmentDate;
                break;
            case "AppointmentTime":
                UpdateControlText(document, (ContentControlPlainText)control, appointmentForm.AppointmentTime);
                break;
            case "DoctorName":
                UpdateControlText(document, (ContentControlPlainText)control, appointmentForm.DoctorName);
                break;
            case "VisitReason":
                ((ContentControlComboBox)control).SelectedItemIndex = (int)appointmentForm.VisitReason;
                break;
            case "AdditionalNotes":
                UpdateControlText(document, (ContentControlRichText)control, appointmentForm.AdditionalNotes ?? string.Empty);
                break;
            case "WheelchairAccess":
                ((ContentControlCheckbox)control).Checked = appointmentForm.WheelchairAccess;
                break;
            case "LanguageInterpreter":
                ((ContentControlCheckbox)control).Checked = appointmentForm.LanguageInterpreter;
                break;
            case "OtherRequirements":
                ((ContentControlCheckbox)control).Checked = appointmentForm.OtherRequirements;
                break;
            case "OtherRequirement":
                UpdateControlText(document, (ContentControlPlainText)control, appointmentForm.OtherRequirement ?? string.Empty);
                break;
        }
    }
}

void UpdateControlText(Document document, ContentControlBase control, string text) {
    document.Delete(document.CreateRange(control.Range.Start.ToInt() + 1, control.Range.Length - 2));
    document.InsertText(document.CreatePosition(control.Range.Start.ToInt() + 1), text);
}