format js
This commit is contained in:
@@ -1,164 +1,156 @@
|
||||
class Annotation {
|
||||
createAnnotations(document) {
|
||||
const annotations = []
|
||||
createAnnotations(document) {
|
||||
const annotations = []
|
||||
|
||||
document.elements.forEach((element) => {
|
||||
console.log('Creating annotation for element', element.id)
|
||||
document.elements.forEach((element) => {
|
||||
console.log('Creating annotation for element', element.id)
|
||||
|
||||
const [annotation, formField] =
|
||||
this.createAnnotationFromElement(element)
|
||||
annotations.push(annotation)
|
||||
annotations.push(formField)
|
||||
})
|
||||
const [annotation, formField] = this.createAnnotationFromElement(element)
|
||||
annotations.push(annotation)
|
||||
annotations.push(formField)
|
||||
})
|
||||
|
||||
return annotations
|
||||
}
|
||||
return annotations
|
||||
}
|
||||
|
||||
async deleteAnnotations(instance) {
|
||||
let pageAnnotations = (
|
||||
await Promise.all(
|
||||
Array.from({ length: instance.totalPageCount }).map(
|
||||
(_, pageIndex) => instance.getAnnotations(pageIndex)
|
||||
)
|
||||
)
|
||||
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 ||
|
||||
annotation.description == 'FRAME'
|
||||
)
|
||||
//deleting all Annotations
|
||||
return await instance.delete(pageAnnotations)
|
||||
}
|
||||
)
|
||||
)
|
||||
.flatMap((annotations) =>
|
||||
annotations.reduce((acc, annotation) => acc.concat(annotation), [])
|
||||
)
|
||||
.filter(
|
||||
(annotation) =>
|
||||
!!annotation.isSignature || annotation.description == 'FRAME'
|
||||
)
|
||||
//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)
|
||||
)
|
||||
)
|
||||
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
|
||||
)
|
||||
|
||||
const formField = new PSPDFKit.FormFields.SignatureFormField({
|
||||
name: id,
|
||||
annotationIds: PSPDFKit.Immutable.List([annotation.id]),
|
||||
})
|
||||
|
||||
return [annotation, formField]
|
||||
}
|
||||
|
||||
createSignatureAnnotation(id, width, height, top, left, pageIndex) {
|
||||
const annotation = new PSPDFKit.Annotations.WidgetAnnotation({
|
||||
id: id,
|
||||
pageIndex: pageIndex,
|
||||
formFieldName: id,
|
||||
backgroundColor: PSPDFKit.Color.YELLOW,
|
||||
blendMode: "multiply",
|
||||
boundingBox: new PSPDFKit.Geometry.Rect({
|
||||
width,
|
||||
height,
|
||||
top,
|
||||
left,
|
||||
}),
|
||||
})
|
||||
|
||||
)
|
||||
)
|
||||
.flatMap((annotations) =>
|
||||
annotations.reduce((acc, annotation) => acc.concat(annotation), [])
|
||||
)
|
||||
.map((annotation) => {
|
||||
console.log(annotation.toJS())
|
||||
return annotation
|
||||
}
|
||||
})
|
||||
|
||||
createImageAnnotation(boundingBox, pageIndex, imageAttachmentId) {
|
||||
const frameAnnotation = new PSPDFKit.Annotations.ImageAnnotation({
|
||||
pageIndex: pageIndex,
|
||||
isSignature: false,
|
||||
readOnly: true,
|
||||
locked: true,
|
||||
lockedContents: true,
|
||||
contentType: 'image/png',
|
||||
imageAttachmentId,
|
||||
description: 'FRAME',
|
||||
boundingBox: boundingBox,
|
||||
});
|
||||
return frameAnnotation
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
async createAnnotationFrameBlob(receiverName, width, height) {
|
||||
const canvas = document.createElement('canvas')
|
||||
canvas.width = width
|
||||
canvas.height = height
|
||||
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
|
||||
)
|
||||
|
||||
const ctx = canvas.getContext('2d')
|
||||
const formField = new PSPDFKit.FormFields.SignatureFormField({
|
||||
name: id,
|
||||
annotationIds: PSPDFKit.Immutable.List([annotation.id]),
|
||||
})
|
||||
|
||||
const date = new Date()
|
||||
const dateString = date.toLocaleDateString('de-DE')
|
||||
return [annotation, formField]
|
||||
}
|
||||
|
||||
const signatureLength = 100
|
||||
createSignatureAnnotation(id, width, height, top, left, pageIndex) {
|
||||
const annotation = new PSPDFKit.Annotations.WidgetAnnotation({
|
||||
id: id,
|
||||
pageIndex: pageIndex,
|
||||
formFieldName: id,
|
||||
backgroundColor: PSPDFKit.Color.YELLOW,
|
||||
blendMode: 'multiply',
|
||||
boundingBox: new PSPDFKit.Geometry.Rect({
|
||||
width,
|
||||
height,
|
||||
top,
|
||||
left,
|
||||
}),
|
||||
})
|
||||
|
||||
ctx.beginPath()
|
||||
return annotation
|
||||
}
|
||||
|
||||
ctx.moveTo(30, 10)
|
||||
ctx.lineTo(signatureLength, 10)
|
||||
createImageAnnotation(boundingBox, pageIndex, imageAttachmentId) {
|
||||
const frameAnnotation = new PSPDFKit.Annotations.ImageAnnotation({
|
||||
pageIndex: pageIndex,
|
||||
isSignature: false,
|
||||
readOnly: true,
|
||||
locked: true,
|
||||
lockedContents: true,
|
||||
contentType: 'image/png',
|
||||
imageAttachmentId,
|
||||
description: 'FRAME',
|
||||
boundingBox: boundingBox,
|
||||
})
|
||||
return frameAnnotation
|
||||
}
|
||||
|
||||
ctx.moveTo(30, 10)
|
||||
ctx.arcTo(10, 10, 10, 30, 20)
|
||||
async createAnnotationFrameBlob(receiverName, width, height) {
|
||||
const canvas = document.createElement('canvas')
|
||||
canvas.width = width
|
||||
canvas.height = height
|
||||
|
||||
ctx.moveTo(10, 30)
|
||||
ctx.arcTo(10, 50, 30, 50, 20)
|
||||
const ctx = canvas.getContext('2d')
|
||||
|
||||
ctx.moveTo(30, 50)
|
||||
ctx.lineTo(signatureLength, 50)
|
||||
const date = new Date()
|
||||
const dateString = date.toLocaleDateString('de-DE')
|
||||
|
||||
ctx.strokeStyle = 'darkblue'
|
||||
ctx.stroke()
|
||||
const signatureLength = 100
|
||||
|
||||
ctx.fillStyle = 'black'
|
||||
ctx.font = '10px serif'
|
||||
ctx.fillText('Signed by', 30, 10)
|
||||
ctx.fillText(receiverName + ', ' + dateString, 15, 60)
|
||||
ctx.beginPath()
|
||||
|
||||
return new Promise((resolve) => {
|
||||
canvas.toBlob((blob) => {
|
||||
const url = URL.createObjectURL(blob)
|
||||
resolve(url)
|
||||
})
|
||||
})
|
||||
}
|
||||
ctx.moveTo(30, 10)
|
||||
ctx.lineTo(signatureLength, 10)
|
||||
|
||||
inchToPoint(inch) {
|
||||
return inch * 72
|
||||
}
|
||||
ctx.moveTo(30, 10)
|
||||
ctx.arcTo(10, 10, 10, 30, 20)
|
||||
|
||||
ctx.moveTo(10, 30)
|
||||
ctx.arcTo(10, 50, 30, 50, 20)
|
||||
|
||||
ctx.moveTo(30, 50)
|
||||
ctx.lineTo(signatureLength, 50)
|
||||
|
||||
ctx.strokeStyle = 'darkblue'
|
||||
ctx.stroke()
|
||||
|
||||
ctx.fillStyle = 'black'
|
||||
ctx.font = '10px serif'
|
||||
ctx.fillText('Signed by', 30, 10)
|
||||
ctx.fillText(receiverName + ', ' + dateString, 15, 60)
|
||||
|
||||
return new Promise((resolve) => {
|
||||
canvas.toBlob((blob) => {
|
||||
const url = URL.createObjectURL(blob)
|
||||
resolve(url)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
inchToPoint(inch) {
|
||||
return inch * 72
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user