refactor(BurnPdfCommandHandler): use IOptions<PDFBurnerParams> and improve form field handling

- Updated BurnPdfCommandHandler constructor to accept IOptions<PDFBurnerParams> instead of raw PDFBurnerParams.
- Updated AddFormFieldValue to calculate coordinates based on PDFBurnerParams offsets and margins.
- Added helper methods for converting pixels to inches (ToInches, ToPointF).
- Removed unused NotImplemented methods in AddFormFieldValue overload.
- Improved code readability and maintainability for annotation burning logic.
This commit is contained in:
tekh 2025-11-07 12:59:13 +01:00
parent 02c5f286ec
commit a386ad72bb
2 changed files with 39 additions and 12 deletions

View File

@ -1,5 +1,4 @@
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.CodeAnalysis;
using System.Drawing;
namespace EnvelopeGenerator.Application.Common.Configurations;

View File

@ -8,7 +8,9 @@ using GdPicture14;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using System.Drawing;
namespace EnvelopeGenerator.Application.Pdf;
@ -42,9 +44,9 @@ public class BurnPdfCommandHandler : IRequestHandler<BurnPdfCommand>
/// <param name="manager"></param>
/// <param name="signRepo"></param>
/// <param name="logger"></param>
public BurnPdfCommandHandler(PDFBurnerParams pdfBurnerParams, AnnotationManager manager, IRepository<Signature> signRepo, ILogger<BurnPdfCommandHandler> logger)
public BurnPdfCommandHandler(IOptions<PDFBurnerParams> pdfBurnerParams, AnnotationManager manager, IRepository<Signature> signRepo, ILogger<BurnPdfCommandHandler> logger)
{
_pdfBurnerParams = pdfBurnerParams;
_pdfBurnerParams = pdfBurnerParams.Value;
_manager = manager;
_signRepo = signRepo;
_logger = logger;
@ -252,12 +254,25 @@ public class BurnPdfCommandHandler : IRequestHandler<BurnPdfCommand>
}
}
private void AddFormFieldValue(double x, double y, double width, double height, int page, string value)
private void AddFormFieldValue(Annotation pAnnotation, FormFieldValue formFieldValue)
{
_manager.SelectPage(page);
var ffIndex = _pdfBurnerParams.IndexOfAnnot[pAnnotation.EgName];
// Convert pixels to Inches
var oBounds = pAnnotation.Bbox?.Select(ToInches).ToList();
if (oBounds is null || oBounds.Count < 4)
return;
double oX = oBounds[0];
double oY = oBounds[1] + _pdfBurnerParams.YOffset * ffIndex + _pdfBurnerParams.TopMargin;
double oWidth = oBounds[2];
double oHeight = oBounds[3];
_manager.SelectPage(pAnnotation.PageIndex + 1);
// Add the text annotation
var ant = _manager.AddTextAnnot((float)x, (float)y, (float)width, (float)height, value);
var ant = _manager.AddTextAnnot((float)oX, (float)oY, (float)oWidth, (float)oHeight, formFieldValue.Value);
// Set the font properties
ant.FontName = _pdfBurnerParams.FontName;
@ -267,11 +282,6 @@ public class BurnPdfCommandHandler : IRequestHandler<BurnPdfCommand>
_manager.SaveAnnotationsToPage();
}
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();
@ -292,6 +302,24 @@ public class BurnPdfCommandHandler : IRequestHandler<BurnPdfCommand>
throw new NotImplementedException();
}
#region Helpers
private PointF ToPointF(List<float> pPoints)
{
var oPoints = pPoints.Select(ToInches).ToList();
return new PointF(oPoints[0], oPoints[1]);
}
private double ToInches(double pValue)
{
return pValue / 72.0;
}
private float ToInches(float pValue)
{
return pValue / 72f;
}
#endregion
/// <summary>
///
/// </summary>