|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: Remove form fields in the React PDF Viewer component | Syncfusion |
| 4 | +description: Learn how to remove PDF form fields using the UI and programmatically in the Syncfusion React PDF Viewer component. |
| 5 | +platform: document-processing |
| 6 | +control: PDF Viewer |
| 7 | +documentation: ug |
| 8 | +--- |
| 9 | + |
| 10 | +# Remove form fields in React PDF Viewer control |
| 11 | + |
| 12 | +The PDF Viewer component allows users to remove PDF form fields using the Form Designer UI and programmatically. |
| 13 | + |
| 14 | +## Remove form fields using the UI |
| 15 | + |
| 16 | +You can remove designed form fields directly from the Form Designer toolbar. |
| 17 | + |
| 18 | +Steps: |
| 19 | + |
| 20 | +1) Select the target form field on the page. |
| 21 | +2) Click the Delete Form Field icon on the Form Designer toolbar. |
| 22 | +3) Alternatively, press the `Delete key` after selecting one or more fields. |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | +## Remove form fields programmatically |
| 27 | + |
| 28 | +Use the `deleteFormField` method to remove form fields programmatically. Retrieve the target field from the `formFieldCollections` property (by object or ID) and pass it to `deleteFormField`. |
| 29 | + |
| 30 | +The following example adds three fields on document load (Textbox, Password, and Signature) and shows two ways to remove them using buttons. |
| 31 | + |
| 32 | +{% tabs %} |
| 33 | +{% highlight js tabtitle="index.js" %} |
| 34 | +import * as ReactDOM from 'react-dom/client'; |
| 35 | +import React, { useRef, useCallback } from 'react'; |
| 36 | +import './index.css'; |
| 37 | +import { PdfViewerComponent, Toolbar, Magnification, Navigation, LinkAnnotation, BookmarkView, ThumbnailView, Print, TextSelection, Annotation, TextSearch, FormDesigner, FormFields, Inject } from '@syncfusion/ej2-react-pdfviewer'; |
| 38 | + |
| 39 | +export function App() { |
| 40 | + const viewerRef = useRef(null); |
| 41 | + |
| 42 | + const onDocumentLoad = useCallback(() => { |
| 43 | + const viewer = viewerRef.current; |
| 44 | + if (!viewer) return; |
| 45 | + |
| 46 | + viewer.formDesignerModule.addFormField('Textbox', { |
| 47 | + name: 'First Name', |
| 48 | + bounds: { X: 146, Y: 229, Width: 150, Height: 24 } |
| 49 | + }); |
| 50 | + |
| 51 | + viewer.formDesignerModule.addFormField('Password', { |
| 52 | + name: 'Password', |
| 53 | + bounds: { X: 338, Y: 229, Width: 150, Height: 24 } |
| 54 | + }); |
| 55 | + |
| 56 | + viewer.formDesignerModule.addFormField('SignatureField', { |
| 57 | + name: 'Sign Here', |
| 58 | + bounds: { X: 146, Y: 280, Width: 200, Height: 43 } |
| 59 | + }); |
| 60 | + }, []); |
| 61 | + |
| 62 | + const deleteAll = useCallback(() => { |
| 63 | + const viewer = viewerRef.current; |
| 64 | + if (!viewer || !viewer.formFieldCollection) return; |
| 65 | + const fields = [...viewer.formFieldCollection]; // clone to avoid mutation issues |
| 66 | + fields.forEach(field => viewer.formDesignerModule.deleteFormField(field)); |
| 67 | + }, []); |
| 68 | + |
| 69 | + const deleteById = useCallback(() => { |
| 70 | + const viewer = viewerRef.current; |
| 71 | + const list = viewer?.formFieldCollection || []; |
| 72 | + if (list.length > 0) { |
| 73 | + const id = list[0].id; |
| 74 | + if (id) viewer.formDesignerModule.deleteFormField(id); |
| 75 | + } |
| 76 | + }, []); |
| 77 | + |
| 78 | + return ( |
| 79 | + <div> |
| 80 | + <div className='control-section'> |
| 81 | + <div style={{ marginBottom: 8 }}> |
| 82 | + <button onClick={deleteAll}>Delete Form Fields</button> |
| 83 | + <button onClick={deleteById} style={{ marginLeft: 8 }}>Delete First Field By ID</button> |
| 84 | + </div> |
| 85 | + <PdfViewerComponent |
| 86 | + ref={viewerRef} |
| 87 | + id="container" |
| 88 | + documentPath="https://cdn.syncfusion.com/content/pdf/form-designer.pdf" |
| 89 | + resourceUrl="https://cdn.syncfusion.com/ej2/31.2.2/dist/ej2-pdfviewer-lib" |
| 90 | + // serviceUrl="https://document.syncfusion.com/web-services/pdf-viewer/api/pdfviewer/" // Optional server-backed |
| 91 | + documentLoad={onDocumentLoad} |
| 92 | + style={{ height: '680px' }} |
| 93 | + > |
| 94 | + <Inject services={[Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, BookmarkView, ThumbnailView, Print, TextSelection, TextSearch, FormDesigner, FormFields]} /> |
| 95 | + </PdfViewerComponent> |
| 96 | + </div> |
| 97 | + </div> |
| 98 | + ); |
| 99 | +} |
| 100 | + |
| 101 | +const root = ReactDOM.createRoot(document.getElementById('sample')); |
| 102 | +root.render(<App />); |
| 103 | +{% endhighlight %} |
| 104 | +{% endtabs %} |
| 105 | + |
| 106 | +N> To configure the server-backed PDF Viewer, add the following `serviceUrl` as a prop in the `index.js` file: |
| 107 | +`serviceUrl="https://document.syncfusion.com/web-services/pdf-viewer/api/pdfviewer/"` |
| 108 | + |
| 109 | +[View Sample on GitHub](https://github.com/SyncfusionExamples/react-pdf-viewer-examples) |
| 110 | + |
| 111 | +## See also |
| 112 | + |
| 113 | +- [Form Designer overview](../overview) |
| 114 | +- [Form Designer Toolbar](../../toolbar-customization/form-designer-toolbar) |
| 115 | +- [Create form fields](./create-formfields) |
| 116 | +- [Edit form fields](./edit-formfields) |
| 117 | +- [Style form fields](./style-formfields) |
| 118 | +- [Group form fields](../group-formfields) |
| 119 | +- [Form validation](../form-validation) |
| 120 | +- [Add custom data to form fields](../custom-data) |
| 121 | +- [Form fields API](../formfields-api) |
0 commit comments