HelpBot Assistant

How can I help you?

Add Custom Data to PDF Form Fields in MVC PDF Viewer

10 Feb 20269 minutes to read

The Syncfusion MVC PDF Viewer allows you to attach custom application-specific data to form fields by using the customData property. This enables you to associate business identifiers, tags, validation hints, or workflow metadata with form fields.

The custom data remains linked to the form field throughout the viewer session and can be accessed or updated whenever the field is queried or modified.

This page explains how to:

Key Points

  • customData is a free form object; you control its structure.
  • Use only serializable values such as objects, arrays, strings, numbers, and booleans.
  • Custom data does not affect the field appearance or behavior unless consumed by your application logic.

Add Custom Data While Creating PDF Form Fields

You can attach custom data at the time of field creation by passing a customData object in the settings parameter of addFormField().

<div style="width:100%;height:600px">
        @Html.EJS().PdfViewer("pdfviewer").DocumentPath("https://cdn.syncfusion.com/content/pdf/form-filling-document.pdf").Render()
    </div>

    <script>
        document.addEventListener("DOMContentLoaded", function () {
            var pdfviewer = document.getElementById('pdfviewer').ej2_instances[0];
            pdfviewer.documentLoad = function () {
                var meta = { businessId: 'C-1024', tags: ['profile','kiosk'], requiredRole: 'admin' };
                pdfviewer.formDesignerModule.addFormField('Textbox', {
                    name: 'Email',
                    bounds: { X: 146, Y: 229, Width: 200, Height: 24 },
                    customData: meta
                });
            };
        });
    </script>

Set Default Custom Data for PDF Form Fields Added Using UI

When users add form fields using the Form Designer toolbar, you can define default customData so that newly created fields automatically inherit it. Default custom data can be configured using per-field settings objects such as:

<div style="width:100%;height:600px">
        @Html.EJS().PdfViewer("pdfviewer").DocumentPath("https://cdn.syncfusion.com/content/pdf/form-filling-document.pdf").Render()
    </div>

    <script>
        document.addEventListener("DOMContentLoaded", function () {
            var pdfviewer = document.getElementById('pdfviewer').ej2_instances[0];
            // Example for textbox defaults
            pdfviewer.textFieldSettings = {
                name: 'Textbox',
                customData: { group: 'contact', createdBy: 'designer', requiredRole: 'user' }
            };

            // Example for checkbox defaults
            pdfviewer.checkBoxFieldSettings = {
                name: 'Checkbox',
                customData: { consentType: 'marketing', defaultChecked: false }
            };
        });
    </script>

Update or Replace PDF Form Field Custom Data

You can modify the customData of an existing form field by using the updateFormField() method. The field can be identified using either its object reference or field ID.

<div style="width:100%;height:600px">
        @Html.EJS().PdfViewer("pdfviewer").DocumentPath("https://cdn.syncfusion.com/content/pdf/form-filling-document.pdf").Render()
    </div>

    <script>
        document.addEventListener("DOMContentLoaded", function () {
            var pdfviewer = document.getElementById('pdfviewer').ej2_instances[0];
            // Retrieve existing fields
            var fields = pdfviewer.retrieveFormFields();
            var target = fields.find(function (f) { return f.name === 'Email'; });

            if (target) {
                // Merge with existing customData to avoid overwriting
                var merged = Object.assign({}, target.customData || {}, { updatedAt: Date.now(), verified: true });
                pdfviewer.formDesignerModule.updateFormField(target, { customData: merged });
            }
        });
    </script>

Tip:
Merge new values with the existing customData object before calling updateFormField() to avoid overwriting previously stored data.

Read Custom Data from PDF Form Fields

You can access the customData property from any form field at any point in your application flow, such as:

  • After the document is loaded
  • During save or submit operations
  • While performing validation or conditional routing
<div style="width:100%;height:600px">
        @Html.EJS().PdfViewer("pdfviewer").DocumentPath("https://cdn.syncfusion.com/content/pdf/form-filling-document.pdf").Render()
    </div>

    <script>
        document.addEventListener("DOMContentLoaded", function () {
            var pdfviewer = document.getElementById('pdfviewer').ej2_instances[0];
            pdfviewer.documentLoad = function () {
                var fields = pdfviewer.retrieveFormFields();
                fields.forEach(function (f) {
                    console.log('Field:', f.name, 'customData:', f.customData);
                    if (f.customData && f.customData.requiredRole === 'admin') {
                        // mark field for special handling
                    }
                });
            };
        });
    </script>

Best Practices

  • Treat customData as application metadata, not display data.
  • Use it to drive business rules, validation logic, and workflow decisions.
  • Keep the data minimal and structured for easy processing.
  • When cloning or copying form fields, ensure customData is copied or updated as required.

View Sample on GitHub

See Also