From c0cece62af8412b1eade8f9f1c5a3304720a0188 Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Wed, 19 Jun 2024 01:55:04 +0200 Subject: [PATCH] =?UTF-8?q?Entwicklung=20von=20Methoden=20zur=20Datums-=20?= =?UTF-8?q?und=20Ortserkennung.=20F=C3=BCgen=20Sie=20"annotations"=20hinzu?= =?UTF-8?q?,=20damit=20der=20Benutzer=20das=20Datum=20und=20die=20Ortsanga?= =?UTF-8?q?ben=20bei=20Bedarf=20aktualisieren=20kann.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wwwroot/js/annotation.js | 46 +++++++++++++++---- EnvelopeGenerator.Web/wwwroot/js/app.js | 4 +- EnvelopeGenerator.Web/wwwroot/js/util.js | 33 ++++++++++++- 3 files changed, 70 insertions(+), 13 deletions(-) diff --git a/EnvelopeGenerator.Web/wwwroot/js/annotation.js b/EnvelopeGenerator.Web/wwwroot/js/annotation.js index ce5b61ba..4841ac21 100644 --- a/EnvelopeGenerator.Web/wwwroot/js/annotation.js +++ b/EnvelopeGenerator.Web/wwwroot/js/annotation.js @@ -1,25 +1,27 @@ class Annotation { - static createAnnotations(document) { + static async createAnnotations(document) { const signatures = [] - document.elements.forEach((element) => { - const [annotation, formField] = Annotation.createSignature(element) + + for(var element of document.elements){ + const [annotation, formField, annotation2, formFieldDate] = await Annotation.createSignature(element) signatures.push(annotation) signatures.push(formField) - }) - - return { - signatures: signatures + signatures.push(annotation2) + signatures.push(formFieldDate) } + + return [...signatures ] } - static createSignature(element) { + static async createSignature(element) { const id = PSPDFKit.generateInstantId() const width = Annotation.inchToPoint(element.width) const height = Annotation.inchToPoint(element.height) const top = Annotation.inchToPoint(element.top) - height / 2 const left = Annotation.inchToPoint(element.left) - width / 2 const page = element.page - 1 - console.log(id) + + //signatures const annotation = new PSPDFKit.Annotations.WidgetAnnotation({ id: id, pageIndex: page, @@ -39,7 +41,31 @@ annotationIds: PSPDFKit.Immutable.List([annotation.id]), }) - return [annotation, formField] + //date + var city = await getCity(); + const id_date = PSPDFKit.generateInstantId() + const annotation_date = new PSPDFKit.Annotations.WidgetAnnotation({ + id: id_date, + pageIndex: page, + formFieldName: id_date, + backgroundColor: PSPDFKit.Color.DarkBlue, + blendMode: 'multiply', + boundingBox: new PSPDFKit.Geometry.Rect({ + width: width * 1.5, + height: height / 2, + top: top + height + 10, + left: left - width * .25, + }), + fontSize:8 + }) + + const formFieldDate = new PSPDFKit.FormFields.TextFormField({ + name: id_date, + annotationIds: PSPDFKit.Immutable.List([annotation_date.id]), + value: getLocaleDateString() + ", " + city + }) + + return [annotation, formField, annotation_date, formFieldDate] } static createTextBox(element) { diff --git a/EnvelopeGenerator.Web/wwwroot/js/app.js b/EnvelopeGenerator.Web/wwwroot/js/app.js index 86fca45e..32ddc1e1 100644 --- a/EnvelopeGenerator.Web/wwwroot/js/app.js +++ b/EnvelopeGenerator.Web/wwwroot/js/app.js @@ -70,8 +70,8 @@ class App { // Load annotations into PSPDFKit try { this.signatureCount = this.currentDocument.elements.length - const annotations = Annotation.createAnnotations(this.currentDocument) - await this.Instance.create(annotations.signatures) + const annotations = await Annotation.createAnnotations(this.currentDocument) + await this.Instance.create(annotations) const openResponse = await this.Network.openDocument(this.envelopeKey) diff --git a/EnvelopeGenerator.Web/wwwroot/js/util.js b/EnvelopeGenerator.Web/wwwroot/js/util.js index bba9fce3..b54353ec 100644 --- a/EnvelopeGenerator.Web/wwwroot/js/util.js +++ b/EnvelopeGenerator.Web/wwwroot/js/util.js @@ -1 +1,32 @@ -const B64ToBuff = (base64String) => new Uint8Array(Array.from(atob(base64String), char => char.charCodeAt(0))).buffer; \ No newline at end of file +const B64ToBuff = (base64String) => new Uint8Array(Array.from(atob(base64String), char => char.charCodeAt(0))).buffer; + +function getCoordinates() { + return new Promise((resolve, reject) => { + if (navigator.geolocation) { + navigator.geolocation.getCurrentPosition( + position => resolve(position.coords), + error => reject(error) + ); + } else { + reject(new Error("Geolocation is not supported by this browser.")); + } + }); +} + +async function getCity() { + try { + const coords = await getCoordinates(); + const response = await fetch(`https://nominatim.openstreetmap.org/reverse?format=json&lat=${coords.latitude}&lon=${coords.longitude}`); + const data = await response.json(); + + if (data && data.address) { + const city = data.address.city || data.address.town || data.address.village || data.address.hamlet; + const postalCode = data.address.postcode; + return postalCode + ' ' + city || ''; + } + } catch { + return ''; + } +} + +const getLocaleDateString = _ => new Date().toLocaleDateString('de-DE') \ No newline at end of file