diff --git a/EnvelopeGenerator.Web/Models/Annotation/AnnotationParams.cs b/EnvelopeGenerator.Web/Models/Annotation/AnnotationParams.cs index 785d7046..2bf4ff09 100644 --- a/EnvelopeGenerator.Web/Models/Annotation/AnnotationParams.cs +++ b/EnvelopeGenerator.Web/Models/Annotation/AnnotationParams.cs @@ -64,7 +64,10 @@ public class AnnotationParams var dict = _annots.ToDictionary(a => a.Name.ToLower(), a => a as IAnnotation); if (Background is not null) + { + Background.Locate(_annots); dict.Add(Background.Name.ToLower(), Background); + } return dict; } diff --git a/EnvelopeGenerator.Web/Models/Annotation/Background.cs b/EnvelopeGenerator.Web/Models/Annotation/Background.cs index fc0b76a4..7d05bfe1 100644 --- a/EnvelopeGenerator.Web/Models/Annotation/Background.cs +++ b/EnvelopeGenerator.Web/Models/Annotation/Background.cs @@ -20,4 +20,29 @@ public record Background : IAnnotation public double Left { get; set; } public double Top { get; set; } + + public void Locate(IEnumerable annotations) + { + // set Top + if (annotations.MinBy(a => a.Top)?.Top is double minTop) + Top = minTop; + + // set Left + if (annotations.MinBy(a => a.Left)?.Left is double minLeft) + Left = minLeft; + + // set Width + if(annotations.MaxBy(a => a.GetRight())?.GetRight() is double maxRight) + Width = maxRight - Left; + + // set Height + if (annotations.MaxBy(a => a.GetBottom())?.GetBottom() is double maxBottom) + Height = maxBottom - Top; + + // add margins + Top -= Margin; + Left -= Margin; + Width += Margin * 2; + Height += Margin * 2; + } } diff --git a/EnvelopeGenerator.Web/Models/Annotation/Extensions.cs b/EnvelopeGenerator.Web/Models/Annotation/Extensions.cs new file mode 100644 index 00000000..92fe3fa4 --- /dev/null +++ b/EnvelopeGenerator.Web/Models/Annotation/Extensions.cs @@ -0,0 +1,8 @@ +namespace EnvelopeGenerator.Web.Models.Annotation; + +public static class Extensions +{ + public static double GetRight(this IAnnotation annotation) => annotation.Left + annotation?.Width ?? 0; + + public static double GetBottom(this IAnnotation annotation) => annotation.Top + annotation?.Height ?? 0; +} diff --git a/EnvelopeGenerator.Web/wwwroot/js/annotation.js b/EnvelopeGenerator.Web/wwwroot/js/annotation.js index 05d85a31..2345cf59 100644 --- a/EnvelopeGenerator.Web/wwwroot/js/annotation.js +++ b/EnvelopeGenerator.Web/wwwroot/js/annotation.js @@ -7,23 +7,28 @@ async function createAnnotations(document, instance) { const page = element.page - 1 //background - const id_background = PSPDFKit.generateInstantId() - const annotation_background = new PSPDFKit.Annotations.WidgetAnnotation({ - id: id_background, - pageIndex: page, - formFieldName: id_background, - backgroundColor: PSPDFKit.Color.WHITE, - blendMode: 'normal', - //boundingBox: new PSPDFKit.Geometry.Rect({width: 152, height: 180, left: 186.09992, top: 648.8533600000001}), - fontSize: 8, - }) + if(annotParams.background){ + const id_background = PSPDFKit.generateInstantId(); + const annotation_background = new PSPDFKit.Annotations.WidgetAnnotation({ + id: id_background, + pageIndex: page, + formFieldName: id_background, + backgroundColor: PSPDFKit.Color.WHITE, + blendMode: 'normal', + boundingBox: new PSPDFKit.Geometry.Rect(annotParams.background), + fontSize: 8, + }); + + const formFieldBackground = new PSPDFKit.FormFields.ButtonFormField({ + name: id_background, + annotationIds: PSPDFKit.Immutable.List([annotation_background.id]), + value: "", + readOnly: false + }); - const formFieldBackground = new PSPDFKit.FormFields.ButtonFormField({ - name: id_background, - annotationIds: PSPDFKit.Immutable.List([annotation_background.id]), - value: "", - readOnly: false - }) + signatures.push(annotation_background) + signatures.push(formFieldBackground) + } //signatures const id = PSPDFKit.generateInstantId() @@ -175,9 +180,6 @@ async function createAnnotations(document, instance) { readOnly: true }) - signatures.push(annotation_background) - signatures.push(formFieldBackground) - signatures.push(annotation) signatures.push(formField) signatures.push(annotation_date)