78 lines
2.5 KiB
JavaScript
78 lines
2.5 KiB
JavaScript
class Annotation {
|
|
createAnnotations(document) {
|
|
const annotations = [];
|
|
|
|
document.elements.forEach((element) => {
|
|
console.log("Creating annotation for element", element.id)
|
|
|
|
const [annotation, formField] = this.createAnnotationFromElement(element)
|
|
annotations.push(annotation);
|
|
annotations.push(formField);
|
|
})
|
|
|
|
return annotations;
|
|
}
|
|
|
|
async deleteAnnotations(instance) {
|
|
let pageAnnotations = (
|
|
await Promise.all(Array.from({ length: instance.totalPageCount }).map((_, pageIndex) =>
|
|
instance.getAnnotations(pageIndex)
|
|
))
|
|
).flatMap((annotations) =>
|
|
annotations.reduce((acc, annotation) => acc.concat(annotation), [])
|
|
).filter((annotation) => !!annotation.isSignature);
|
|
//deleting all Annotations
|
|
return await instance.delete(pageAnnotations);
|
|
}
|
|
|
|
async validateAnnotations(instance) {
|
|
let pageAnnotations = (
|
|
await Promise.all(Array.from({ length: instance.totalPageCount }).map((_, pageIndex) =>
|
|
instance.getAnnotations(pageIndex)
|
|
))
|
|
).flatMap((annotations) =>
|
|
annotations.reduce((acc, annotation) => acc.concat(annotation), [])
|
|
).map((annotation) => {
|
|
console.log(annotation.toJS());
|
|
return annotation;
|
|
});
|
|
|
|
return true;
|
|
}
|
|
|
|
createAnnotationFromElement(element) {
|
|
const id = PSPDFKit.generateInstantId()
|
|
const width = this.inchToPoint(element.width)
|
|
const height = this.inchToPoint(element.height)
|
|
const top = this.inchToPoint(element.top) - (height / 2)
|
|
const left = this.inchToPoint(element.left) - (width / 2)
|
|
const page = element.page - 1
|
|
const annotation = this.createSignatureAnnotation(id, width, height, top, left, page)
|
|
console.log(annotation)
|
|
|
|
const formField = new SignatureFormField({
|
|
name: id,
|
|
annotationIds: List([annotation.id])
|
|
})
|
|
console.log(formField)
|
|
|
|
return [annotation, formField]
|
|
}
|
|
|
|
createSignatureAnnotation(id, width, height, top, left, pageIndex) {
|
|
const annotation = new PSPDFKit.Annotations.WidgetAnnotation({
|
|
id: id,
|
|
pageIndex: pageIndex,
|
|
formFieldName: id,
|
|
boundingBox: new Rect({ width, height, top, left })
|
|
})
|
|
|
|
return annotation
|
|
}
|
|
|
|
inchToPoint(inch) {
|
|
return inch * 72;
|
|
}
|
|
}
|
|
|