Skip to content

Commit cac9429

Browse files
Merge pull request #79 from SyncfusionExamples/985220-formfilling
985220 - How to Work with Form Fields in a PDF Document Using the React PDF Viewer
2 parents 4a9ee07 + ed3ccca commit cac9429

File tree

12 files changed

+285
-0
lines changed

12 files changed

+285
-0
lines changed

Videos/Form Fields/.gitignore

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
pnpm-debug.log*
8+
lerna-debug.log*
9+
10+
node_modules
11+
dist
12+
dist-ssr
13+
*.local
14+
15+
# Editor directories and files
16+
.vscode/*
17+
!.vscode/extensions.json
18+
.idea
19+
.DS_Store
20+
*.suo
21+
*.ntvs*
22+
*.njsproj
23+
*.sln
24+
*.sw?

Videos/Form Fields/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Form Fields in PDF Viewer
2+
3+
This section demonstrates how to work with form fields in a PDF document using the Syncfusion React PDF Viewer.
4+
5+
Documentation: https://help.syncfusion.com/document-processing/pdf/pdf-viewer/react/form-designer/create-programmatically
6+
7+
8+
## Running the React Sample
9+
To run the React sample
10+
11+
Step 1: Open a terminal or command prompt.
12+
13+
Step 2: Navigate to the root directory of the React sample.
14+
15+
Step 3: Run the command npm install to install the necessary dependencies specified in the package.json file.
16+
```
17+
npm install
18+
```
19+
Step 4 Once the installation is complete, run npm start to start the React development server and open the PDF Viewer component in your default browser.
20+
```
21+
npm start
22+
```
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import js from '@eslint/js'
2+
import globals from 'globals'
3+
import reactHooks from 'eslint-plugin-react-hooks'
4+
import reactRefresh from 'eslint-plugin-react-refresh'
5+
import { defineConfig, globalIgnores } from 'eslint/config'
6+
7+
export default defineConfig([
8+
globalIgnores(['dist']),
9+
{
10+
files: ['**/*.{js,jsx}'],
11+
extends: [
12+
js.configs.recommended,
13+
reactHooks.configs.flat.recommended,
14+
reactRefresh.configs.vite,
15+
],
16+
languageOptions: {
17+
ecmaVersion: 2020,
18+
globals: globals.browser,
19+
parserOptions: {
20+
ecmaVersion: 'latest',
21+
ecmaFeatures: { jsx: true },
22+
sourceType: 'module',
23+
},
24+
},
25+
rules: {
26+
'no-unused-vars': ['error', { varsIgnorePattern: '^[A-Z_]' }],
27+
},
28+
},
29+
])

Videos/Form Fields/index.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>pdfviewer</title>
8+
</head>
9+
<body>
10+
<div id="root"></div>
11+
<script type="module" src="/src/main.jsx"></script>
12+
</body>
13+
</html>

Videos/Form Fields/package.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "pdfviewer",
3+
"private": true,
4+
"version": "0.0.0",
5+
"type": "module",
6+
"scripts": {
7+
"dev": "vite",
8+
"build": "vite build",
9+
"lint": "eslint .",
10+
"preview": "vite preview"
11+
},
12+
"dependencies": {
13+
"@syncfusion/ej2-react-pdfviewer": "*",
14+
"react": "^19.2.0",
15+
"react-dom": "^19.2.0"
16+
},
17+
"devDependencies": {
18+
"@eslint/js": "^9.39.1",
19+
"@types/react": "^19.2.5",
20+
"@types/react-dom": "^19.2.3",
21+
"@vitejs/plugin-react": "^5.1.1",
22+
"eslint": "^9.39.1",
23+
"eslint-plugin-react-hooks": "^7.0.1",
24+
"eslint-plugin-react-refresh": "^0.4.24",
25+
"globals": "^16.5.0",
26+
"vite": "^7.2.4"
27+
}
28+
}

Videos/Form Fields/public/vite.svg

Lines changed: 1 addition & 0 deletions
Loading

Videos/Form Fields/src/App.css

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#root {
2+
max-width: 1280px;
3+
margin: 0 auto;
4+
padding: 2rem;
5+
text-align: center;
6+
}
7+
8+
.logo {
9+
height: 6em;
10+
padding: 1.5em;
11+
will-change: filter;
12+
transition: filter 300ms;
13+
}
14+
.logo:hover {
15+
filter: drop-shadow(0 0 2em #646cffaa);
16+
}
17+
.logo.react:hover {
18+
filter: drop-shadow(0 0 2em #61dafbaa);
19+
}
20+
21+
@keyframes logo-spin {
22+
from {
23+
transform: rotate(0deg);
24+
}
25+
to {
26+
transform: rotate(360deg);
27+
}
28+
}
29+
30+
@media (prefers-reduced-motion: no-preference) {
31+
a:nth-of-type(2) .logo {
32+
animation: logo-spin infinite 20s linear;
33+
}
34+
}
35+
36+
.card {
37+
padding: 2em;
38+
}
39+
40+
.read-the-docs {
41+
color: #888;
42+
}

Videos/Form Fields/src/App.jsx

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import * as React from 'react';
2+
import './index.css';
3+
import {
4+
PdfViewerComponent, Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, BookmarkView,
5+
ThumbnailView, Print, TextSelection, TextSearch, FormFields, FormDesigner, Inject
6+
} from '@syncfusion/ej2-react-pdfviewer';
7+
8+
function App() {
9+
10+
const addField = () => {
11+
var viewer = document.getElementById('container').ej2_instances[0];
12+
viewer.formDesignerModule.addFormField("Textbox", { name: "First Name", bounds: { X: 146, Y: 229, Width: 150, Height: 24 } });
13+
viewer.formDesignerModule.addFormField("Textbox", { name: "Middle Name", bounds: { X: 338, Y: 229, Width: 150, Height: 24 } });
14+
viewer.formDesignerModule.addFormField('Textbox', { name: 'Last Name', bounds: { X: 530, Y: 229, Width: 150, Height: 24 }, });
15+
viewer.formDesignerModule.addFormField('RadioButton', { bounds: { X: 148, Y: 289, Width: 18, Height: 18 }, name: 'Gender', isSelected: false, });
16+
viewer.formDesignerModule.addFormField('RadioButton', { bounds: { X: 292, Y: 289, Width: 18, Height: 18 }, name: 'Gender', isSelected: false, });
17+
viewer.formDesignerModule.addFormField('Textbox', { name: 'DOB Month', bounds: { X: 146, Y: 320, Width: 35, Height: 24 }, });
18+
viewer.formDesignerModule.addFormField('Textbox', { name: 'DOB Date', bounds: { X: 193, Y: 320, Width: 35, Height: 24 }, });
19+
viewer.formDesignerModule.addFormField('Textbox', { name: 'DOB Year', bounds: { X: 242, Y: 320, Width: 35, Height: 24 }, });
20+
viewer.formDesignerModule.addFormField('InitialField', { name: 'Agree', bounds: { X: 148, Y: 408, Width: 200, Height: 43 }, });
21+
viewer.formDesignerModule.addFormField('InitialField', { name: 'Do Not Agree', bounds: { X: 148, Y: 466, Width: 200, Height: 43 }, });
22+
viewer.formDesignerModule.addFormField('CheckBox', { name: 'Text Message', bounds: { X: 56, Y: 664, Width: 20, Height: 20 }, isChecked: false, });
23+
viewer.formDesignerModule.addFormField('CheckBox', { name: 'Email', bounds: { X: 242, Y: 664, Width: 20, Height: 20 }, isChecked: false, });
24+
viewer.formDesignerModule.addFormField('CheckBox', { name: 'Appointment Reminder', bounds: { X: 56, Y: 740, Width: 20, Height: 20 }, isChecked: false, });
25+
viewer.formDesignerModule.addFormField('CheckBox', { name: 'Request for Customerservice', bounds: { X: 56, Y: 778, Width: 20, Height: 20 }, isChecked: false, });
26+
viewer.formDesignerModule.addFormField('CheckBox', { name: 'Information Billing', bounds: { X: 290, Y: 740, Width: 20, Height: 20 }, isChecked: false, });
27+
viewer.formDesignerModule.addFormField('Textbox', { name: 'My Email', bounds: { X: 146, Y: 850, Width: 200, Height: 24 }, });
28+
viewer.formDesignerModule.addFormField('Textbox', { name: 'My Phone', bounds: { X: 482, Y: 850, Width: 200, Height: 24 }, });
29+
viewer.formDesignerModule.addFormField('SignatureField', { name: 'Sign', bounds: { X: 57, Y: 923, Width: 200, Height: 43 }, });
30+
viewer.formDesignerModule.addFormField('Textbox', { name: 'DOS Month', bounds: { X: 386, Y: 923, Width: 35, Height: 24 }, });
31+
viewer.formDesignerModule.addFormField('Textbox', { name: 'DOS Date', bounds: { X: 434, Y: 923, Width: 35, Height: 24 }, });
32+
viewer.formDesignerModule.addFormField('Textbox', { name: 'DOS Year', bounds: { X: 482, Y: 923, Width: 35, Height: 24 }, });
33+
};
34+
35+
const editField = () => {
36+
var viewer = document.getElementById('container').ej2_instances[0];
37+
const field = viewer.formFieldCollections.find(f => f.name === "First Name");
38+
if (field) {
39+
viewer.formDesignerModule.updateFormField(field, {
40+
value: 'Peter',
41+
backgroundColor: '#e0f7fa',
42+
borderColor: '#00796b',
43+
fontColor: '#004d40'
44+
});
45+
}
46+
};
47+
48+
const deleteField = () => {
49+
var viewer = document.getElementById('container').ej2_instances[0];
50+
viewer.formDesignerModule.deleteFormField(viewer.formFieldCollections[2]);
51+
};
52+
53+
function downloadClicked() {
54+
var viewer = document.getElementById('container').ej2_instances[0];
55+
viewer.download();
56+
}
57+
58+
return (
59+
<div>
60+
{/* Header Section */}
61+
<div style={{
62+
display: 'flex',
63+
justifyContent: 'space-between',
64+
alignItems: 'center',
65+
padding: '10px 20px',
66+
backgroundColor: '#f5f5f5',
67+
borderBottom: '1px solid #ddd'
68+
}}>
69+
<h2 style={{ margin: 0 }}>Syncfusion PDF Viewer </h2>
70+
<div>
71+
<button onClick={addField} style={{ marginRight: '10px' }}>Add Field</button>
72+
<button onClick={editField} style={{ marginRight: '10px' }}>Edit Field</button>
73+
<button onClick={deleteField} style={{ marginRight: '10px' }}>Delete Field</button>
74+
<button onClick={downloadClicked}>Download</button>
75+
</div>
76+
</div>
77+
{/* PDF Viewer Section */}
78+
<div className='control-section'>
79+
<PdfViewerComponent
80+
id="container"
81+
documentPath="https://cdn.syncfusion.com/content/pdf/form-designer.pdf"
82+
resourceUrl="https://cdn.syncfusion.com/ej2/31.2.2/dist/ej2-pdfviewer-lib"
83+
style={{ height: '700px' }}
84+
>
85+
<Inject services={[
86+
Toolbar, Magnification, Navigation, Annotation, LinkAnnotation,
87+
BookmarkView, ThumbnailView, Print, TextSelection, TextSearch,
88+
FormFields, FormDesigner
89+
]} />
90+
</PdfViewerComponent>
91+
</div>
92+
</div>
93+
);
94+
}
95+
96+
export default App;
Lines changed: 1 addition & 0 deletions
Loading

Videos/Form Fields/src/index.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@import '../node_modules/@syncfusion/ej2-base/styles/material.css';
2+
@import '../node_modules/@syncfusion/ej2-buttons/styles/material.css';
3+
@import '../node_modules/@syncfusion/ej2-dropdowns/styles/material.css';
4+
@import '../node_modules/@syncfusion/ej2-inputs/styles/material.css';
5+
@import '../node_modules/@syncfusion/ej2-navigations/styles/material.css';
6+
@import '../node_modules/@syncfusion/ej2-popups/styles/material.css';
7+
@import '../node_modules/@syncfusion/ej2-splitbuttons/styles/material.css';
8+
@import "../node_modules/@syncfusion/ej2-pdfviewer/styles/material.css";

0 commit comments

Comments
 (0)