- Refactored BurnPdfCommandHandler to use new extension methods for cleaner annotation handling. - Introduced ITextStyle interface to generalize font styling for text annotations. - Updated PDFBurnerParams to implement ITextStyle for consistent font settings reuse. - Added MathematExtensions for coordinate and unit conversion (ToInches, ToPointF). - Added GdPictureExtensions to encapsulate annotation-related logic (form fields, images, ink). - Improved readability and maintainability by removing redundant helper methods.
177 lines
5.7 KiB
C#
177 lines
5.7 KiB
C#
using EnvelopeGenerator.Application.Common.Dto.PSPDFKitInstant;
|
|
using EnvelopeGenerator.Domain.Constants;
|
|
using GdPicture14;
|
|
using Microsoft.Extensions.Logging;
|
|
using Newtonsoft.Json;
|
|
using SixLabors.ImageSharp;
|
|
using System.Drawing;
|
|
using EnvelopeGenerator.Application.Common.Extensions;
|
|
using EnvelopeGenerator.Application.Common.Interfaces.Model;
|
|
using EnvelopeGenerator.Application.Common.Configurations;
|
|
|
|
namespace EnvelopeGenerator.Application.Common.Extensions;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public static class GdPictureExtensions
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="manager"></param>
|
|
/// <param name="x"></param>
|
|
/// <param name="y"></param>
|
|
/// <param name="width"></param>
|
|
/// <param name="height"></param>
|
|
/// <param name="page"></param>
|
|
/// <param name="value"></param>
|
|
/// <param name="textStyle"></param>
|
|
public static void AddFormFieldValue(this AnnotationManager manager, double x, double y, double width, double height, int page, string value, ITextStyle textStyle)
|
|
{
|
|
manager.SelectPage(page);
|
|
|
|
// Add the text annotation
|
|
var ant = manager.AddTextAnnot((float)x, (float)y, (float)width, (float)height, value);
|
|
|
|
// Set the font properties
|
|
ant.FontName = textStyle.FontName;
|
|
ant.FontSize = textStyle.FontSize;
|
|
ant.FontStyle = textStyle.FontStyle;
|
|
|
|
manager.SaveAnnotationsToPage();
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="manager"></param>
|
|
/// <param name="pAnnotation"></param>
|
|
/// <param name="formFieldValue"></param>
|
|
/// <param name="options"></param>
|
|
public static void AddFormFieldValue(this AnnotationManager manager, Annotation pAnnotation, FormFieldValue formFieldValue, PDFBurnerParams options)
|
|
{
|
|
var ffIndex = options.GetAnnotationIndex(pAnnotation.EgName);
|
|
|
|
// Convert pixels to Inches
|
|
var oBounds = pAnnotation.Bbox?.Select(points => points.ToInches()).ToList();
|
|
|
|
if (oBounds is null || oBounds.Count < 4)
|
|
return;
|
|
|
|
double oX = oBounds[0];
|
|
double oY = oBounds[1] + options.YOffset * ffIndex + options.TopMargin;
|
|
double oWidth = oBounds[2];
|
|
double oHeight = oBounds[3];
|
|
|
|
manager.SelectPage(pAnnotation.PageIndex + 1);
|
|
|
|
// Add the text annotation
|
|
var ant = manager.AddTextAnnot((float)oX, (float)oY, (float)oWidth, (float)oHeight, formFieldValue.Value);
|
|
|
|
// Set the font properties
|
|
ant.FontName = options.FontName;
|
|
ant.FontSize = options.FontSize;
|
|
ant.FontStyle = options.FontStyle;
|
|
|
|
manager.SaveAnnotationsToPage();
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="manager"></param>
|
|
/// <param name="x"></param>
|
|
/// <param name="y"></param>
|
|
/// <param name="width"></param>
|
|
/// <param name="height"></param>
|
|
/// <param name="page"></param>
|
|
/// <param name="base64"></param>
|
|
public static void AddImageAnnotation(this AnnotationManager manager, double x, double y, double width, double height, int page, string base64)
|
|
{
|
|
manager.SelectPage(page);
|
|
manager.AddEmbeddedImageAnnotFromBase64(base64, (float)x, (float)y, (float)width, (float)height);
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="manager"></param>
|
|
/// <param name="pAnnotation"></param>
|
|
/// <param name="pAttachments"></param>
|
|
public static void AddImageAnnotation(this AnnotationManager manager, Annotation pAnnotation, Dictionary<string, Attachment> pAttachments)
|
|
{
|
|
var oAttachment = pAttachments
|
|
.Where(a => a.Key == pAnnotation.ImageAttachmentId)
|
|
.SingleOrDefault();
|
|
|
|
if (oAttachment.Value == null)
|
|
return;
|
|
|
|
// Convert pixels to Inches
|
|
var oBounds = pAnnotation.Bbox?.Select(post => post.ToInches()).ToList();
|
|
|
|
if (oBounds is null || oBounds.Count < 4)
|
|
return;
|
|
|
|
var oX = oBounds[0];
|
|
var oY = oBounds[1];
|
|
var oWidth = oBounds[2];
|
|
var oHeight = oBounds[3];
|
|
|
|
manager.SelectPage(pAnnotation.PageIndex + 1);
|
|
manager.AddEmbeddedImageAnnotFromBase64(oAttachment.Value.Binary, (float)oX, (float)oY, (float)oWidth, (float)oHeight);
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="manager"></param>
|
|
/// <param name="page"></param>
|
|
/// <param name="value"></param>
|
|
public static void AddInkAnnotation(this AnnotationManager manager, int page, string value)
|
|
{
|
|
var ink = JsonConvert.DeserializeObject<Ink>(value);
|
|
|
|
var oSegments = ink?.Lines.Points;
|
|
var oColor = ColorTranslator.FromHtml(ink?.StrokeColor ?? "#000000");
|
|
manager.SelectPage(page);
|
|
|
|
if (oSegments is null)
|
|
return;
|
|
|
|
foreach (var oSegment in oSegments)
|
|
{
|
|
var oPoints = oSegment
|
|
.Select(points => points.ToPointF())
|
|
.ToArray();
|
|
|
|
manager.AddFreeHandAnnot(oColor, oPoints);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="manager"></param>
|
|
/// <param name="pAnnotation"></param>
|
|
public static void AddInkAnnotation(this AnnotationManager manager, Annotation pAnnotation)
|
|
{
|
|
var oSegments = pAnnotation.Lines?.Points;
|
|
var oColor = ColorTranslator.FromHtml(pAnnotation.StrokeColor ?? "#000000");
|
|
manager.SelectPage(pAnnotation.PageIndex + 1);
|
|
|
|
if (oSegments is null)
|
|
return;
|
|
|
|
foreach (var oSegment in oSegments)
|
|
{
|
|
var oPoints = oSegment
|
|
.Select(points => points.ToPointF())
|
|
.ToArray();
|
|
|
|
manager.AddFreeHandAnnot(oColor, oPoints);
|
|
}
|
|
}
|
|
}
|