Skip to content

Commit ae01789

Browse files
author
Andrey Safonov
authored
Merge pull request #7 from PDFTron/add-date-picker
Added ability to add date field to the document
2 parents c8cd562 + 3c623c9 commit ae01789

File tree

2 files changed

+46
-16
lines changed

2 files changed

+46
-16
lines changed

src/components/PrepareDocument/PrepareDocument.js

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -88,23 +88,13 @@ const PrepareDocument = () => {
8888
let field;
8989

9090
if (typeof annot.custom !== 'undefined') {
91-
// set flags
92-
const flags = new Annotations.WidgetFlags();
93-
if (annot.custom.flag.readOnly) {
94-
flags.set('ReadOnly', true);
95-
}
96-
if (annot.custom.flag.multiline) {
97-
flags.set('Multiline', true);
98-
}
99-
10091
// create a form field based on the type of annotation
10192
if (annot.custom.type === 'TEXT') {
10293
field = new Annotations.Forms.Field(
10394
annot.getContents() + Date.now() + index,
10495
{
10596
type: 'Tx',
10697
value: annot.custom.value,
107-
flags,
10898
},
10999
);
110100
inputAnnot = new Annotations.TextWidgetAnnotation(field);
@@ -113,7 +103,6 @@ const PrepareDocument = () => {
113103
annot.getContents() + Date.now() + index,
114104
{
115105
type: 'Sig',
116-
flags,
117106
},
118107
);
119108
inputAnnot = new Annotations.SignatureWidgetAnnotation(field, {
@@ -131,6 +120,35 @@ const PrepareDocument = () => {
131120
},
132121
},
133122
});
123+
} else if (annot.custom.type === 'DATE') {
124+
field = new Annotations.Forms.Field(
125+
annot.getContents() + Date.now() + index,
126+
{
127+
type: 'Tx',
128+
value: 'm-d-yyyy',
129+
// Actions need to be added for DatePickerWidgetAnnotation to recognize this field.
130+
actions: {
131+
F: [
132+
{
133+
name: 'JavaScript',
134+
// You can customize the date format here between the two double-quotation marks
135+
// or leave this blank to use the default format
136+
javascript: 'AFDate_FormatEx("mmm d, yyyy");',
137+
},
138+
],
139+
K: [
140+
{
141+
name: 'JavaScript',
142+
// You can customize the date format here between the two double-quotation marks
143+
// or leave this blank to use the default format
144+
javascript: 'AFDate_FormatEx("mmm d, yyyy");',
145+
},
146+
],
147+
},
148+
},
149+
);
150+
151+
inputAnnot = new Annotations.DatePickerWidgetAnnotation(field);
134152
} else {
135153
// exit early for other annotations
136154
annotManager.deleteAnnotation(annot, false, true); // prevent duplicates when importing xfdf
@@ -141,7 +159,7 @@ const PrepareDocument = () => {
141159
return;
142160
}
143161

144-
// set flag and position
162+
// set position
145163
inputAnnot.PageNumber = annot.getPageNumber();
146164
inputAnnot.X = annot.getX();
147165
inputAnnot.Y = annot.getY();
@@ -178,7 +196,7 @@ const PrepareDocument = () => {
178196
annotManager.deleteAnnotations(annotsToDelete, null, true);
179197

180198
// refresh viewer
181-
annotManager.drawAnnotationsFromList(annotsToDraw);
199+
await annotManager.drawAnnotationsFromList(annotsToDraw);
182200
await uploadForSigning();
183201
};
184202

@@ -243,7 +261,7 @@ const PrepareDocument = () => {
243261
const docRef = storageRef.child(referenceString);
244262
const { docViewer, annotManager } = instance;
245263
const doc = docViewer.getDocument();
246-
const xfdfString = await annotManager.exportAnnotations();
264+
const xfdfString = await annotManager.exportAnnotations({ widgets: true, fields: true });
247265
const data = await doc.getFileData({ xfdfString });
248266
const arr = new Uint8Array(data);
249267
const blob = new Blob([arr], { type: 'application/pdf' });
@@ -365,6 +383,20 @@ const PrepareDocument = () => {
365383
/>
366384
</div>
367385
</Box>
386+
<Box padding={2}>
387+
<div
388+
draggable
389+
onDragStart={e => dragStart(e)}
390+
onDragEnd={e => dragEnd(e, 'DATE')}
391+
>
392+
<Button
393+
onClick={() => addField('DATE')}
394+
accessibilityLabel="add date field"
395+
text="Add date"
396+
iconEnd="calendar"
397+
/>
398+
</div>
399+
</Box>
368400
</Stack>
369401
</Row>
370402
<Row gap={1}>

src/components/SignDocument/SignDocument.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,13 @@ const SignDocument = () => {
4949
// load document
5050
const storageRef = storage.ref();
5151
const URL = await storageRef.child(docRef).getDownloadURL();
52-
console.log(URL);
5352
docViewer.loadDocument(URL);
5453

5554
const normalStyles = (widget) => {
5655
if (widget instanceof Annotations.TextWidgetAnnotation) {
5756
return {
5857
'background-color': '#a5c7ff',
5958
color: 'white',
60-
'font-size': '20px',
6159
};
6260
} else if (widget instanceof Annotations.SignatureWidgetAnnotation) {
6361
return {

0 commit comments

Comments
 (0)