refactor: standardize annotation types and clean up PDF burn handler
- Introduced `AnnotationType.PSPDFKit` constants for consistent annotation type references. - Updated `BurnPdfCommandHandler` to use new PSPDFKit annotation types. - Refactored method signatures and local variables for clarity (e.g., using 'var', null-coalescing for annotation properties). - Streamlined `using` statements and memory management in PDF burning process. - Added placeholder methods for form fields, image, and ink annotations to centralize future implementations.
This commit is contained in:
parent
6a5c9a3489
commit
b7d146ddb5
@ -1,6 +1,7 @@
|
|||||||
using DigitalData.Core.Abstraction.Application.Repository;
|
using DigitalData.Core.Abstraction.Application.Repository;
|
||||||
using EnvelopeGenerator.Application.Common.Configurations;
|
using EnvelopeGenerator.Application.Common.Configurations;
|
||||||
using EnvelopeGenerator.Application.Exceptions;
|
using EnvelopeGenerator.Application.Exceptions;
|
||||||
|
using EnvelopeGenerator.Domain.Constants;
|
||||||
using EnvelopeGenerator.Domain.Entities;
|
using EnvelopeGenerator.Domain.Entities;
|
||||||
using EnvelopeGenerator.PdfEditor;
|
using EnvelopeGenerator.PdfEditor;
|
||||||
using GdPicture14;
|
using GdPicture14;
|
||||||
@ -72,93 +73,86 @@ public class BurnPdfCommandHandler : IRequestHandler<BurnPdfCommand>
|
|||||||
public byte[] BurnElementAnnotsToPDF(byte[] pSourceBuffer, List<Signature> elements)
|
public byte[] BurnElementAnnotsToPDF(byte[] pSourceBuffer, List<Signature> elements)
|
||||||
{
|
{
|
||||||
// Add background
|
// Add background
|
||||||
using (var doc = PdfEditor.Pdf.FromMemory(pSourceBuffer))
|
using var doc = PdfEditor.Pdf.FromMemory(pSourceBuffer);
|
||||||
|
// TODO: take the length from the largest y
|
||||||
|
pSourceBuffer = doc.Background(elements, 1.9500000000000002 * 0.93, 2.52 * 0.67)
|
||||||
|
.ExportStream()
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
|
GdPictureStatus oResult;
|
||||||
|
using var oSourceStream = new MemoryStream(pSourceBuffer);
|
||||||
|
// Open PDF
|
||||||
|
oResult = _manager.InitFromStream(oSourceStream);
|
||||||
|
if (oResult != GdPictureStatus.OK)
|
||||||
|
throw new BurnAnnotationException($"Could not open document for burning: [{oResult}]");
|
||||||
|
|
||||||
|
// Imported from background (add to configuration)
|
||||||
|
var margin = 0.2;
|
||||||
|
var inchFactor = 72;
|
||||||
|
|
||||||
|
// Y offset of form fields
|
||||||
|
var keys = new[] { "position", "city", "date" }; // add to configuration
|
||||||
|
var unitYOffsets = 0.2;
|
||||||
|
|
||||||
|
var yOffsetsOfFF = keys
|
||||||
|
.Select((k, i) => new { Key = k, Value = unitYOffsets * i + 1 })
|
||||||
|
.ToDictionary(x => x.Key, x => x.Value);
|
||||||
|
|
||||||
|
// Add annotations
|
||||||
|
foreach (var element in elements)
|
||||||
{
|
{
|
||||||
// TODO: take the length from the largest y
|
var frameX = element.Left - 0.7 - margin;
|
||||||
pSourceBuffer = doc.Background(elements, 1.9500000000000002 * 0.93, 2.52 * 0.67)
|
|
||||||
.ExportStream()
|
|
||||||
.ToArray();
|
|
||||||
|
|
||||||
GdPictureStatus oResult;
|
var frame = element.Annotations?.FirstOrDefault(a => a.Name == "frame");
|
||||||
using (var oSourceStream = new MemoryStream(pSourceBuffer))
|
var frameY = element.Top - 0.5 - margin;
|
||||||
|
var frameYShift = (frame!.Y ?? default) - frameY * inchFactor;
|
||||||
|
var frameXShift = (frame.X ?? default) - frameX * inchFactor;
|
||||||
|
|
||||||
|
foreach (var annot in element.Annotations!)
|
||||||
{
|
{
|
||||||
// Open PDF
|
if (!yOffsetsOfFF.TryGetValue(annot.Name, out var yOffsetofFF))
|
||||||
oResult = _manager.InitFromStream(oSourceStream);
|
yOffsetofFF = 0;
|
||||||
if (oResult != GdPictureStatus.OK)
|
|
||||||
throw new BurnAnnotationException($"Could not open document for burning: [{oResult}]");
|
|
||||||
|
|
||||||
// Imported from background (add to configuration)
|
var y = frameY + yOffsetofFF;
|
||||||
double margin = 0.2;
|
|
||||||
double inchFactor = 72;
|
|
||||||
|
|
||||||
// Y offset of form fields
|
if (annot.Type == AnnotationType.PSPDFKit.FormField)
|
||||||
var keys = new[] { "position", "city", "date" }; // add to configuration
|
|
||||||
double unitYOffsets = 0.2;
|
|
||||||
|
|
||||||
var yOffsetsOfFF = keys
|
|
||||||
.Select((k, i) => new { Key = k, Value = unitYOffsets * i + 1 })
|
|
||||||
.ToDictionary(x => x.Key, x => x.Value);
|
|
||||||
|
|
||||||
// Add annotations
|
|
||||||
foreach (var element in elements)
|
|
||||||
{
|
{
|
||||||
double frameX = element.Left - 0.7 - margin;
|
AddFormFieldValue(
|
||||||
|
(annot.X ?? default) / inchFactor,
|
||||||
var frame = element.Annotations?.FirstOrDefault(a => a.Name == "frame");
|
y,
|
||||||
double frameY = element.Top - 0.5 - margin;
|
(annot.Width ?? default) / inchFactor,
|
||||||
double frameYShift = (frame!.Y ?? default) - frameY * inchFactor;
|
(annot.Height ?? default) / inchFactor,
|
||||||
double frameXShift = (frame.X ?? default) - frameX * inchFactor;
|
element.Page,
|
||||||
|
annot.Value
|
||||||
foreach (var annot in element.Annotations!)
|
);
|
||||||
{
|
|
||||||
double yOffsetofFF;
|
|
||||||
if (!yOffsetsOfFF.TryGetValue(annot.Name, out yOffsetofFF))
|
|
||||||
yOffsetofFF = 0;
|
|
||||||
|
|
||||||
double y = frameY + yOffsetofFF;
|
|
||||||
|
|
||||||
if (annot.Type == AnnotationType.FormField)
|
|
||||||
{
|
|
||||||
AddFormFieldValue(
|
|
||||||
annot.X / inchFactor,
|
|
||||||
y,
|
|
||||||
annot.Width / inchFactor,
|
|
||||||
annot.Height / inchFactor,
|
|
||||||
element.Page,
|
|
||||||
annot.Value
|
|
||||||
);
|
|
||||||
}
|
|
||||||
else if (annot.Type == AnnotationType.Image)
|
|
||||||
{
|
|
||||||
AddImageAnnotation(
|
|
||||||
annot.X / inchFactor,
|
|
||||||
annot.Name == "signature" ? (annot.Y - frameYShift) / inchFactor : y,
|
|
||||||
annot.Width / inchFactor,
|
|
||||||
annot.Height / inchFactor,
|
|
||||||
element.Page,
|
|
||||||
annot.Value
|
|
||||||
);
|
|
||||||
}
|
|
||||||
else if (annot.Type == AnnotationType.Ink)
|
|
||||||
{
|
|
||||||
AddInkAnnotation(element.Page, annot.Value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
else if (annot.Type == AnnotationType.PSPDFKit.Image)
|
||||||
// Save PDF
|
|
||||||
using (var oNewStream = new MemoryStream())
|
|
||||||
{
|
{
|
||||||
oResult = _manager.SaveDocumentToPDF(oNewStream);
|
AddImageAnnotation(
|
||||||
if (oResult != GdPictureStatus.OK)
|
(annot.X ?? default) / inchFactor,
|
||||||
throw new BurnAnnotationException($"Could not save document to stream: [{oResult}]");
|
annot.Name == "signature" ? ((annot.Y ?? default) - frameYShift) / inchFactor : y,
|
||||||
|
(annot.Width ?? default) / inchFactor,
|
||||||
_manager.Close();
|
(annot.Height ?? default) / inchFactor,
|
||||||
|
element.Page,
|
||||||
return oNewStream.ToArray();
|
annot.Value
|
||||||
|
);
|
||||||
|
}
|
||||||
|
else if (annot.Type == AnnotationType.PSPDFKit.Ink)
|
||||||
|
{
|
||||||
|
AddInkAnnotation(element.Page, annot.Value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Save PDF
|
||||||
|
using var oNewStream = new MemoryStream();
|
||||||
|
oResult = _manager.SaveDocumentToPDF(oNewStream);
|
||||||
|
if (oResult != GdPictureStatus.OK)
|
||||||
|
throw new BurnAnnotationException($"Could not save document to stream: [{oResult}]");
|
||||||
|
|
||||||
|
_manager.Close();
|
||||||
|
|
||||||
|
return oNewStream.ToArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -169,7 +163,38 @@ public class BurnPdfCommandHandler : IRequestHandler<BurnPdfCommand>
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public byte[] BurnInstantJSONAnnotsToPDF(byte[] pSourceBuffer, List<string> pInstantJSONList)
|
public byte[] BurnInstantJSONAnnotsToPDF(byte[] pSourceBuffer, List<string> pInstantJSONList)
|
||||||
{
|
{
|
||||||
return Enumerable.Empty<byte>().ToArray();
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void AddFormFieldValue(double x, double y, double width, double height, int page, string value)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void AddFormFieldValue(Annotation pAnnotation, FormFieldValue formFieldValue)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void AddImageAnnotation(double x, double y, double width, double height, int page, string base64)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void AddImageAnnotation(Annotation pAnnotation, Dictionary<string, Attachment> pAttachments)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void AddInkAnnotation(int page, string value)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void AddInkAnnotation(Annotation pAnnotation)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
13
EnvelopeGenerator.Domain/Constants/AnnotationType.cs
Normal file
13
EnvelopeGenerator.Domain/Constants/AnnotationType.cs
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
namespace EnvelopeGenerator.Domain.Constants
|
||||||
|
{
|
||||||
|
public static class AnnotationType
|
||||||
|
{
|
||||||
|
public static class PSPDFKit
|
||||||
|
{
|
||||||
|
public const string Image = "pspdfkit/image";
|
||||||
|
public const string Ink = "pspdfkit/ink";
|
||||||
|
public const string Widget = "pspdfkit/widget";
|
||||||
|
public const string FormField = "pspdfkit/form-field-value";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user