Programmatically work with form field in React Pdfviewer component
27 Nov 202322 minutes to read
The PDF Viewer control provides the option to add, edit and delete the Form Fields. The Form Fields type supported by the PDF Viewer Control are:
* Textbox
* Password
* CheckBox
* RadioButton
* ListBox
* DropDown
* SignatureField
* InitialField
Add a form field to PDF document programmatically
Using addFormField method, the form fields can be added to the PDF document programmatically. We need to pass two parameters in this method. They are Form Field Type and Properties of Form Field Type. To add form field programmatically, Use the following code.
import * as ReactDOM from 'react-dom';
import * as React from 'react';
import { PdfViewerComponent, Toolbar, Magnification, Navigation, LinkAnnotation, BookmarkView, ThumbnailView,
Print, TextSelection, Annotation, TextSearch, Inject, FormDesigner, FormFields, PageOrganizer} from '@syncfusion/ej2-react-pdfviewer';
export function App() {
function documentLoaded(){
var viewer = document.getElementById('container').ej2_instances[0];
viewer.formDesignerModule.addFormField("Textbox", { name: "Textbox", bounds: { X: 146, Y: 229, Width: 150, Height: 24 } });
}
return (<div>
<div className='control-section'>
{/* Render the PDF Viewer */}
<PdfViewerComponent
id="container"
documentPath="https://cdn.syncfusion.com/content/pdf/form-designer.pdf"
serviceUrl="https://services.syncfusion.com/react/production/api/pdfviewer"
documentLoad={documentLoaded}
height= '640px'>
<Inject services={[ Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, BookmarkView, ThumbnailView,
Print, TextSelection, TextSearch, FormDesigner, FormFields, PageOrganizer ]} />
</PdfViewerComponent>
</div>
</div>);
}
const root = ReactDOM.createRoot(document.getElementById('sample'));
root.render(<App />);
import * as ReactDOM from 'react-dom';
import * as React from 'react';
import { PdfViewerComponent, Toolbar, Magnification, Navigation, LinkAnnotation, BookmarkView, ThumbnailView,
Print, TextSelection, Annotation, TextSearch, Inject, FormDesigner, FormFields, PageOrganizer} from '@syncfusion/ej2-react-pdfviewer';
export function App() {
function documentLoaded(){
var viewer = document.getElementById('container').ej2_instances[0];
viewer.formDesignerModule.addFormField("Textbox", { name: "Textbox", bounds: { X: 146, Y: 229, Width: 150, Height: 24 } });
}
return (<div>
<div className='control-section'>
{/* Render the PDF Viewer */}
<PdfViewerComponent
id="container"
documentPath="https://cdn.syncfusion.com/content/pdf/form-designer.pdf"
serviceUrl="https://services.syncfusion.com/react/production/api/pdfviewer"
documentLoad={documentLoaded}
height= '640px'>
<Inject services={[ Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, BookmarkView, ThumbnailView,
Print, TextSelection, TextSearch, FormDesigner, FormFields, PageOrganizer ]} />
</PdfViewerComponent>
</div>
</div>);
}
const root = ReactDOM.createRoot(document.getElementById('sample'));
root.render(<App />);
<button class="preview-sample-button" id="PreviewSampleButton-z663t945kf4tdcg1tb1wtawhcwvc4tem" onclick="LoadPreviewSample('https://ej2.syncfusion.com/react/documentation/code-snippet/pdfviewer/addformfield-cs2',this.id);">Preview Sample</button><button class="stackblitz-button" id="StackBlitzButton-z663t945kf4tdcg1tb1wtawhcwvc4tem" onclick="OpenSampleInStackBlitz('https://ej2.syncfusion.com/react/documentation/code-snippet/pdfviewer/addformfield-cs2');"><img class="stackblitz-icon" src="https://cdn.syncfusion.com/documentation/images/StackBlitz-icon.png"/><span class="stackblitz-text">Open in Stackblitz</span></button>
<div id="PreviewSampleHolder-z663t945kf4tdcg1tb1wtawhcwvc4tem"></div>
<button class="preview-sample-button" id="PreviewSampleButton-0tlvjwk31u502w6rzjxvo2ji55xrkdss" onclick="LoadPreviewSample('https://ej2.syncfusion.com/react/documentation/code-snippet/pdfviewer/addformfield-cs2',this.id);">Preview Sample</button><button class="stackblitz-button" id="StackBlitzButton-0tlvjwk31u502w6rzjxvo2ji55xrkdss" onclick="OpenSampleInStackBlitz('https://ej2.syncfusion.com/react/documentation/code-snippet/pdfviewer/addformfield-cs2');"><img class="stackblitz-icon" src="https://cdn.syncfusion.com/documentation/images/StackBlitz-icon.png"/><span class="stackblitz-text">Open in Stackblitz</span></button>
<div id="PreviewSampleHolder-0tlvjwk31u502w6rzjxvo2ji55xrkdss"></div>
Edit/Update form field programmatically
Using updateFormField method, Form Field can be updated programmatically. We should get the Form Field object/Id from FormFieldCollections property that you would like to edit and pass it as a parameter to updateFormField method. The second parameter should be the properties that you would like to update for Form Field programmatically. We have updated the value and background Color properties of Textbox Form Field.
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { PdfViewerComponent, Toolbar, Magnification, Navigation, LinkAnnotation, BookmarkView,ThumbnailView,
Print, TextSelection, Annotation, TextSearch, FormDesigner, FormFields, Inject } from '@syncfusion/ej2-react-pdfviewer';
export function App() {
function documentLoaded() {
var viewer = document.getElementById('container').ej2_instances[0];
viewer.formDesignerModule.addFormField("Textbox", { name: "Textbox", bounds: { X: 146, Y: 229, Width: 150, Height: 24 }});
viewer.formDesignerModule.addFormField("Textbox", { name: "Textfield", bounds: { X: 300, Y: 229, Width: 150, Height: 24 }}
);
viewer.formDesignerModule.updateFormField(viewer.formFieldCollections[0], { backgroundColor: 'red' });
}
return (<div>
<div className='control-section'>
{/* Render the PDF Viewer */}
<PdfViewerComponent
id="container"
documentPath="https://cdn.syncfusion.com/content/pdf/form-designer.pdf"
serviceUrl="https://services.syncfusion.com/react/production/api/pdfviewer"
documentLoad={documentLoaded}
height= '640px'>
<Inject services={[ Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, BookmarkView, ThumbnailView,
Print, TextSelection, TextSearch, FormDesigner, FormFields]} />
</PdfViewerComponent>
</div>
</div>);
}
const root = ReactDOM.createRoot(document.getElementById('sample'));
root.render(<App />);
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { PdfViewerComponent, Toolbar, Magnification, Navigation, LinkAnnotation, BookmarkView,ThumbnailView,
Print, TextSelection, Annotation, TextSearch, FormDesigner, FormFields, Inject } from '@syncfusion/ej2-react-pdfviewer';
export function App() {
function documentLoaded() {
var viewer = document.getElementById('container').ej2_instances[0];
viewer.formDesignerModule.addFormField("Textbox", { name: "Textbox", bounds: { X: 146, Y: 229, Width: 150, Height: 24 }});
viewer.formDesignerModule.addFormField("Textbox", { name: "Textfield", bounds: { X: 300, Y: 229, Width: 150, Height: 24 }}
);
viewer.formDesignerModule.updateFormField(viewer.formFieldCollections[0], { backgroundColor: 'red' });
}
return (<div>
<div className='control-section'>
{/* Render the PDF Viewer */}
<PdfViewerComponent
id="container"
documentPath="https://cdn.syncfusion.com/content/pdf/form-designer.pdf"
serviceUrl="https://services.syncfusion.com/react/production/api/pdfviewer"
documentLoad={documentLoaded}
height= '640px'>
<Inject services={[ Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, BookmarkView, ThumbnailView,
Print, TextSelection, TextSearch, FormDesigner, FormFields]} />
</PdfViewerComponent>
</div>
</div>);
}
const root = ReactDOM.createRoot(document.getElementById('sample'));
root.render(<App />);
<button class="preview-sample-button" id="PreviewSampleButton-979gw3dhdfoidv7cf8ufm3v414l0et8z" onclick="LoadPreviewSample('https://ej2.syncfusion.com/react/documentation/code-snippet/pdfviewer/updateformfield-cs2',this.id);">Preview Sample</button><button class="stackblitz-button" id="StackBlitzButton-979gw3dhdfoidv7cf8ufm3v414l0et8z" onclick="OpenSampleInStackBlitz('https://ej2.syncfusion.com/react/documentation/code-snippet/pdfviewer/updateformfield-cs2');"><img class="stackblitz-icon" src="https://cdn.syncfusion.com/documentation/images/StackBlitz-icon.png"/><span class="stackblitz-text">Open in Stackblitz</span></button>
<div id="PreviewSampleHolder-979gw3dhdfoidv7cf8ufm3v414l0et8z"></div>
<button class="preview-sample-button" id="PreviewSampleButton-lnnq0b1aqdyfun1xvutohounsi3gvq44" onclick="LoadPreviewSample('https://ej2.syncfusion.com/react/documentation/code-snippet/pdfviewer/updateformfield-cs2',this.id);">Preview Sample</button><button class="stackblitz-button" id="StackBlitzButton-lnnq0b1aqdyfun1xvutohounsi3gvq44" onclick="OpenSampleInStackBlitz('https://ej2.syncfusion.com/react/documentation/code-snippet/pdfviewer/updateformfield-cs2');"><img class="stackblitz-icon" src="https://cdn.syncfusion.com/documentation/images/StackBlitz-icon.png"/><span class="stackblitz-text">Open in Stackblitz</span></button>
<div id="PreviewSampleHolder-lnnq0b1aqdyfun1xvutohounsi3gvq44"></div>
Delete form field programmatically
Using deleteFormField method, the form field can be deleted programmatically. We should retrieve the Form Field object/Id from FormFieldCollections property that you would like to delete and pass it as a parameter to deleteFormField method. To delete a Form Field programmatically, use the following code.
import * as ReactDOM from 'react-dom';
import * as React from 'react';
import { PdfViewerComponent, Toolbar, Magnification, Navigation, LinkAnnotation, BookmarkView,ThumbnailView,
Print, TextSelection, Annotation, TextSearch, Inject, FormDesigner, FormFields } from '@syncfusion/ej2-react-pdfviewer';
export function App() {
function documentLoaded(){
var viewer = document.getElementById('container').ej2_instances[0];
viewer.formDesignerModule.addFormField("Textbox", { name: "Textbox", bounds: { X: 146, Y: 229, Width: 150, Height: 24 }});
viewer.formDesignerModule.addFormField("Textbox", { name: "Textfield", bounds: { X: 300, Y: 229, Width: 150, Height: 24 }}
);
viewer.formDesignerModule.deleteFormField(viewer.formFieldCollections[0]);
}
return (<div>
<div className='control-section'>
{/* Render the PDF Viewer */}
<PdfViewerComponent
id="container"
documentPath="https://cdn.syncfusion.com/content/pdf/form-designer.pdf"
serviceUrl="https://services.syncfusion.com/react/production/api/pdfviewer"
documentLoad={documentLoaded}
height= '640px'>
<Inject services={[ Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, BookmarkView,
ThumbnailView, Print, TextSelection, TextSearch, FormDesigner, FormFields]} />
</PdfViewerComponent>
</div>
</div>);
}
const root = ReactDOM.createRoot(document.getElementById('sample'));
root.render(<App />);
import * as ReactDOM from 'react-dom';
import * as React from 'react';
import { PdfViewerComponent, Toolbar, Magnification, Navigation, LinkAnnotation, BookmarkView,ThumbnailView,
Print, TextSelection, Annotation, TextSearch, Inject, FormDesigner, FormFields } from '@syncfusion/ej2-react-pdfviewer';
export function App() {
function documentLoaded(){
var viewer = document.getElementById('container').ej2_instances[0];
viewer.formDesignerModule.addFormField("Textbox", { name: "Textbox", bounds: { X: 146, Y: 229, Width: 150, Height: 24 }});
viewer.formDesignerModule.addFormField("Textbox", { name: "Textfield", bounds: { X: 300, Y: 229, Width: 150, Height: 24 }}
);
viewer.formDesignerModule.deleteFormField(viewer.formFieldCollections[0]);
}
return (<div>
<div className='control-section'>
{/* Render the PDF Viewer */}
<PdfViewerComponent
id="container"
documentPath="https://cdn.syncfusion.com/content/pdf/form-designer.pdf"
serviceUrl="https://services.syncfusion.com/react/production/api/pdfviewer"
documentLoad={documentLoaded}
height= '640px'>
<Inject services={[ Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, BookmarkView,
ThumbnailView, Print, TextSelection, TextSearch, FormDesigner, FormFields]} />
</PdfViewerComponent>
</div>
</div>);
}
const root = ReactDOM.createRoot(document.getElementById('sample'));
root.render(<App />);
<button class="preview-sample-button" id="PreviewSampleButton-8qz4ltv2lihr1ktm5jlvvok9slivw7zx" onclick="LoadPreviewSample('https://ej2.syncfusion.com/react/documentation/code-snippet/pdfviewer/deleteformfield-cs2-standalone',this.id);">Preview Sample</button><button class="stackblitz-button" id="StackBlitzButton-8qz4ltv2lihr1ktm5jlvvok9slivw7zx" onclick="OpenSampleInStackBlitz('https://ej2.syncfusion.com/react/documentation/code-snippet/pdfviewer/deleteformfield-cs2-standalone');"><img class="stackblitz-icon" src="https://cdn.syncfusion.com/documentation/images/StackBlitz-icon.png"/><span class="stackblitz-text">Open in Stackblitz</span></button>
<div id="PreviewSampleHolder-8qz4ltv2lihr1ktm5jlvvok9slivw7zx"></div>
<button class="preview-sample-button" id="PreviewSampleButton-1ze2bvp88osay2aie1mya27bfneytass" onclick="LoadPreviewSample('https://ej2.syncfusion.com/react/documentation/code-snippet/pdfviewer/deleteformfield-cs2',this.id);">Preview Sample</button><button class="stackblitz-button" id="StackBlitzButton-1ze2bvp88osay2aie1mya27bfneytass" onclick="OpenSampleInStackBlitz('https://ej2.syncfusion.com/react/documentation/code-snippet/pdfviewer/deleteformfield-cs2');"><img class="stackblitz-icon" src="https://cdn.syncfusion.com/documentation/images/StackBlitz-icon.png"/><span class="stackblitz-text">Open in Stackblitz</span></button>
<div id="PreviewSampleHolder-1ze2bvp88osay2aie1mya27bfneytass"></div>
Saving the form fields
When the download icon is selected on the toolbar, the Form Fields will be saved in the PDF document and this action will not affect the original document. Refer the below GIF for further reference.
You can invoke download action using following code snippet.,
import * as ReactDOM from 'react-dom';
import * as React from 'react';
import { PdfViewerComponent, Toolbar, Magnification, Navigation, LinkAnnotation, BookmarkView,ThumbnailView,
Print, TextSelection, Annotation, TextSearch, FormDesigner, FormFields, Inject } from '@syncfusion/ej2-react-pdfviewer';
let pdfviewer;
function App() {
function downloadClicked() {
var viewer = document.getElementById('container').ej2_instances[0];
viewer.download();
}
return (<div>
<div className='control-section'>
{/* Render the PDF Viewer */}
<button onClick={downloadClicked}>Download</button>
<PdfViewerComponent
ref={(scope) => { pdfviewer = scope; }}
id="container"
documentPath="PDF_Succinctly.pdf"
serviceUrl="https://services.syncfusion.com/react/production/api/pdfviewer"
style={{ 'height': '640px' }}>
<Inject services={[ Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, BookmarkView,
ThumbnailView, Print, TextSelection, TextSearch, FormDesigner, FormFields ]} />
</PdfViewerComponent>
</div>
</div>);
}
const root = ReactDOM.createRoot(document.getElementById('sample'));
root.render(<App />);
Printing the form fields
When the print icon is selected on the toolbar, the PDF document will be printed along with the Form Fields added to the pages and this action will not affect the original document. Refer the below GIF for further reference.
import * as ReactDOM from 'react-dom';
import * as React from 'react';
import { PdfViewerComponent, Toolbar, Magnification, Navigation, LinkAnnotation, BookmarkView,ThumbnailView,
Print, TextSelection, Annotation, TextSearch, FormDesigner, FormFields, Inject } from '@syncfusion/ej2-react-pdfviewer';
export function App() {
return (<div>
<div className='control-section'>
{/* Render the PDF Viewer */}
<PdfViewerComponent
id="container"
documentPath="PDF_Succinctly.pdf"
enablePrint={true}
serviceUrl="https://services.syncfusion.com/react/production/api/pdfviewer"
style={{ 'height': '640px' }}>
<Inject services={[ Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, BookmarkView,
ThumbnailView, Print, TextSelection, TextSearch, FormDesigner, FormFields ]} />
</PdfViewerComponent>
</div>
</div>);
}
const root = ReactDOM.createRoot(document.getElementById('sample'));
root.render(<App />);
Open the existing PDF document
We can open the already saved PDF document contains Form Fields in it by clicking the open icon in the toolbar. Refer the below GIF for further reference.