From 2b7cd4a2093b39b74aae89e4a7681d5a705e4f7e Mon Sep 17 00:00:00 2001 From: Jonathan Jenne Date: Fri, 17 Nov 2023 13:46:38 +0100 Subject: [PATCH] clean up pages, dont send the document to server --- EnvelopeGenerator.Web/Classes/PDFBurner.cs | 192 ++++++++++++++++++ .../EnvelopeGenerator.Web.csproj | 4 + .../Views/Home/EnvelopeSigned.cshtml | 6 +- EnvelopeGenerator.Web/Views/Home/Index.cshtml | 13 -- .../Views/Home/ShowEnvelope.cshtml | 2 +- .../wwwroot/js/annotation.js | 3 +- EnvelopeGenerator.Web/wwwroot/js/app.js | 13 -- 7 files changed, 201 insertions(+), 32 deletions(-) create mode 100644 EnvelopeGenerator.Web/Classes/PDFBurner.cs diff --git a/EnvelopeGenerator.Web/Classes/PDFBurner.cs b/EnvelopeGenerator.Web/Classes/PDFBurner.cs new file mode 100644 index 00000000..b4d28b33 --- /dev/null +++ b/EnvelopeGenerator.Web/Classes/PDFBurner.cs @@ -0,0 +1,192 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using DigitalData.Modules.Base; +using DigitalData.Modules.Logging; +using GdPicture14; +using Newtonsoft.Json; + +namespace EnvelopeGenerator.Web.Classes +{ +public partial class PDFBurner : BaseClass +{ + + private readonly string LicenseKey; + private readonly AnnotationManager Manager; + private readonly LicenseManager LicenseManager; + + private const string ANNOTATION_TYPE_IMAGE = "pspdfkit/image"; + private const string ANNOTATION_TYPE_INK = "pspdfkit/ink"; + + public PDFBurner(LogConfig pLogConfig, string pGDPictureLicenseKey) : base(pLogConfig) + { + + LicenseKey = pGDPictureLicenseKey; + LicenseManager = new LicenseManager(); + LicenseManager.RegisterKEY(pGDPictureLicenseKey); + + Manager = new AnnotationManager(); + } + + public bool BurnInstantJSONAnnotationsToPDF(string pSourcePath, List pInstantJSONList, string pDestinationPath) + { + if (Manager.InitFromFile(pSourcePath) != GdPictureStatus.OK) + { + Logger.Warn("Could not open file [{0}] for burning.", pSourcePath); + return false; + } + + foreach (var oJSON in pInstantJSONList) + { + if (AddInstantJSONAnnotationToPDF(oJSON) == false) + { + Logger.Warn("Adding Annotation failed. Exiting"); + return false; + } + } + + try + { + Manager.BurnAnnotationsToPage(RemoveInitialAnnots: true, VectorMode: true); + Manager.SaveDocumentToPDF(pDestinationPath); + Manager.Close(); + + return true; + } + catch (Exception ex) + { + Logger.Warn("Could not burn and save annotations to file [{0}]!", pDestinationPath); + Logger.Error(ex); + + return false; + } + } + + private bool AddInstantJSONAnnotationToPDF(string pInstantJSON) + { + try + { + var oAnnotationData = JsonConvert.DeserializeObject(pInstantJSON); + + foreach (Annotation oAnnotation in oAnnotationData.annotations) + { + switch (oAnnotation.type) + { + case ANNOTATION_TYPE_IMAGE: + { + AddImageAnnotation(oAnnotation, oAnnotationData.attachments); + break; + } + case ANNOTATION_TYPE_INK: + { + AddInkAnnotation(oAnnotation); + break; + } + } + } + + return true; + } + catch (Exception ex) + { + Logger.Warn("Could not create annotation from InstantJSON"); + Logger.Error(ex); + return false; + } + } + + private bool AddImageAnnotation(Annotation pAnnotation, Dictionary pAttachments) + { + try + { + var oAttachment = pAttachments.Where(a => (a.Key ?? "") == (pAnnotation.imageAttachmentId ?? "")).SingleOrDefault(); + + // Convert pixels to Inches + var oBounds = pAnnotation.bbox.Select(ToInches).ToList(); + + float oX = oBounds[0]; + float oY = oBounds[1]; + float oWidth = oBounds[2]; + float oHeight = oBounds[3]; + + Manager.AddEmbeddedImageAnnotFromBase64(oAttachment.Value.binary, oX, oY, oWidth, oHeight); + + return true; + } + catch (Exception ex) + { + Logger.Warn("Could not add image annotation!"); + Logger.Error(ex); + + return false; + } + } + + private bool AddInkAnnotation(Annotation pAnnotation) + { + try + { + var oSegments = pAnnotation.lines.points; + var oColor = ColorTranslator.FromHtml(pAnnotation.strokeColor); + Manager.SelectPage(pAnnotation.pageIndex); + + foreach (List> oSegment in oSegments) + { + var oPoints = oSegment.Select(ToPointF).ToArray(); + Manager.AddFreeHandAnnot(oColor, oPoints); + } + + return true; + } + catch (Exception ex) + { + Logger.Warn("Could not add image annotation!"); + Logger.Error(ex); + + return false; + } + + } + + private PointF ToPointF(List pPoints) + { + var oPoints = pPoints.Select(ToInches).ToList(); + return new PointF(oPoints[0], oPoints[1]); + } + + private float ToInches(float pValue) + { + return pValue / 72f; + } + + internal partial class AnnotationData + { + public List annotations { get; set; } + public Dictionary attachments { get; set; } + } + + internal partial class Annotation + { + public string id { get; set; } + public List bbox { get; set; } + public string type { get; set; } + public bool isSignature { get; set; } + public string imageAttachmentId { get; set; } + public Lines lines { get; set; } + public int pageIndex { get; set; } + public string strokeColor { get; set; } + } + + internal partial class Lines + { + public List>> points { get; set; } + } + + internal partial class Attachment + { + public string binary { get; set; } + public string contentType { get; set; } + } +} +} diff --git a/EnvelopeGenerator.Web/EnvelopeGenerator.Web.csproj b/EnvelopeGenerator.Web/EnvelopeGenerator.Web.csproj index db9b9670..2f938db2 100644 --- a/EnvelopeGenerator.Web/EnvelopeGenerator.Web.csproj +++ b/EnvelopeGenerator.Web/EnvelopeGenerator.Web.csproj @@ -7,6 +7,7 @@ + @@ -32,6 +33,9 @@ ..\..\DDModules\Logging\bin\Debug\DigitalData.Modules.Logging.dll + + D:\ProgramFiles\GdPicture.NET 14\Redist\GdPicture.NET (.NET Framework 4.5)\GdPicture.NET.14.dll + diff --git a/EnvelopeGenerator.Web/Views/Home/EnvelopeSigned.cshtml b/EnvelopeGenerator.Web/Views/Home/EnvelopeSigned.cshtml index a08172c7..b7b2d68e 100644 --- a/EnvelopeGenerator.Web/Views/Home/EnvelopeSigned.cshtml +++ b/EnvelopeGenerator.Web/Views/Home/EnvelopeSigned.cshtml @@ -1,5 +1,5 @@ @{ - ViewData["Title"] = "Privacy Policy"; + ViewData["Title"] = "Dokument unterschrieben"; }
@@ -10,11 +10,11 @@
-

Umschlag erfolgreich signiert!

+

Dokument erfolgreich signiert!

-

Sie haben das Dokument erfolgreich signiert. Im Anschluss erhalten Sie eine schriftliche Bestätigung.

+

Sie haben das Dokument signiert. Im Anschluss erhalten Sie eine schriftliche Bestätigung.

diff --git a/EnvelopeGenerator.Web/Views/Home/Index.cshtml b/EnvelopeGenerator.Web/Views/Home/Index.cshtml index e191b918..da55e976 100644 --- a/EnvelopeGenerator.Web/Views/Home/Index.cshtml +++ b/EnvelopeGenerator.Web/Views/Home/Index.cshtml @@ -20,18 +20,5 @@ } - -
- Show image - -
- - - diff --git a/EnvelopeGenerator.Web/Views/Home/ShowEnvelope.cshtml b/EnvelopeGenerator.Web/Views/Home/ShowEnvelope.cshtml index 6109a421..bd90ba66 100644 --- a/EnvelopeGenerator.Web/Views/Home/ShowEnvelope.cshtml +++ b/EnvelopeGenerator.Web/Views/Home/ShowEnvelope.cshtml @@ -1,5 +1,5 @@ @{ - ViewData["Title"] = "Privacy Policy"; + ViewData["Title"] = "Dokument unterschreiben"; }