Refactor Jobs namespace and improve PDF handling

Refactored all EnvelopeGenerator.Jobs files to use the EnvelopeGenerator.Jobs namespace instead of EnvelopeGenerator.CommonServices.Jobs. Updated the .csproj to remove custom content and compile includes for the Jobs folder. Switched FinalizeDocumentJob to use dependency injection for PDFBurner, PDFMerger, and ReportCreator. Improved image annotation logic in PDFBurner for better placement and scaling, and refactored form field value rendering for conditional font styling. Aliased Document as LayoutDocument in ReportCreator to avoid ambiguity. Removed the obsolete Class1.cs file and made minor type safety improvements. These changes modernize the codebase and enhance maintainability.
This commit is contained in:
2026-01-22 09:51:16 +01:00
parent 786a3e128d
commit f078bafdde
12 changed files with 65 additions and 77 deletions

View File

@@ -1,7 +0,0 @@
namespace EnvelopeGenerator.Jobs
{
public class Class1
{
}
}

View File

@@ -19,23 +19,4 @@
<ProjectReference Include="..\EnvelopeGenerator.PdfEditor\EnvelopeGenerator.PdfEditor.csproj" />
</ItemGroup>
<ItemGroup>
<Content Include="Jobs\**\*.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<Compile Remove="Jobs\APIBackendJobs\APIEnvelopeJob.cs" />
<Compile Remove="Jobs\DataRowExtensions.cs" />
<Compile Remove="Jobs\FinalizeDocument\FinalizeDocumentExceptions.cs" />
<Compile Remove="Jobs\FinalizeDocument\FinalizeDocumentJob.cs" />
<Compile Remove="Jobs\FinalizeDocument\PDFBurner.cs" />
<Compile Remove="Jobs\FinalizeDocument\PDFBurnerParams.cs" />
<Compile Remove="Jobs\FinalizeDocument\PDFMerger.cs" />
<Compile Remove="Jobs\FinalizeDocument\ReportCreator.cs" />
<Compile Remove="Jobs\FinalizeDocument\ReportItem.cs" />
<Compile Remove="Jobs\FinalizeDocument\ReportSource.cs" />
</ItemGroup>
</Project>

View File

@@ -7,7 +7,7 @@ using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Quartz;
namespace EnvelopeGenerator.CommonServices.Jobs.APIBackendJobs;
namespace EnvelopeGenerator.Jobs.APIBackendJobs;
public class APIEnvelopeJob : IJob
{

View File

@@ -1,7 +1,7 @@
using System;
using System.Data;
namespace EnvelopeGenerator.CommonServices.Jobs;
namespace EnvelopeGenerator.Jobs;
public static class DataRowExtensions
{

View File

@@ -1,4 +1,4 @@
namespace EnvelopeGenerator.CommonServices.Jobs.FinalizeDocument;
namespace EnvelopeGenerator.Jobs.FinalizeDocument;
public static class FinalizeDocumentExceptions
{

View File

@@ -4,15 +4,14 @@ using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using EnvelopeGenerator.CommonServices.Jobs.FinalizeDocument.FinalizeDocumentExceptions;
using EnvelopeGenerator.Domain.Constants;
using EnvelopeGenerator.Domain.Entities;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Quartz;
using static EnvelopeGenerator.Jobs.FinalizeDocument.FinalizeDocumentExceptions;
namespace EnvelopeGenerator.CommonServices.Jobs.FinalizeDocument;
namespace EnvelopeGenerator.Jobs.FinalizeDocument;
public class FinalizeDocumentJob : IJob
{
@@ -23,17 +22,16 @@ public class FinalizeDocumentJob : IJob
private record ConfigSettings(string DocumentPath, string DocumentPathOrigin, string ExportPath);
public FinalizeDocumentJob()
: this(NullLogger<FinalizeDocumentJob>.Instance)
{
}
public FinalizeDocumentJob(ILogger<FinalizeDocumentJob> logger)
public FinalizeDocumentJob(
ILogger<FinalizeDocumentJob> logger,
PDFBurner pdfBurner,
PDFMerger pdfMerger,
ReportCreator reportCreator)
{
_logger = logger;
_pdfBurner = new PDFBurner();
_pdfMerger = new PDFMerger();
_reportCreator = new ReportCreator();
_pdfBurner = pdfBurner;
_pdfMerger = pdfMerger;
_reportCreator = reportCreator;
}
public async Task Execute(IJobExecutionContext context)
@@ -77,20 +75,22 @@ public class FinalizeDocumentJob : IJob
continue;
}
var data = envelopeData.Value;
var envelope = new Envelope
{
Id = envelopeId,
Uuid = envelopeData.EnvelopeUuid ?? string.Empty,
Title = envelopeData.Title ?? string.Empty,
FinalEmailToCreator = FinalEmailType.No,
FinalEmailToReceivers = FinalEmailType.No
Uuid = data.EnvelopeUuid ?? string.Empty,
Title = data.Title ?? string.Empty,
FinalEmailToCreator = (int)FinalEmailType.No,
FinalEmailToReceivers = (int)FinalEmailType.No
};
var burned = _pdfBurner.BurnAnnotsToPDF(envelopeData.DocumentBytes, envelopeData.AnnotationData, envelopeId);
var burned = _pdfBurner.BurnAnnotsToPDF(data.DocumentBytes, data.AnnotationData, envelopeId);
var report = _reportCreator.CreateReport(connection, envelope);
var merged = _pdfMerger.MergeDocuments(burned, report);
var outputDirectory = Path.Combine(config.ExportPath, envelopeData.ParentFolderUid);
var outputDirectory = Path.Combine(config.ExportPath, data.ParentFolderUid);
Directory.CreateDirectory(outputDirectory);
var outputPath = Path.Combine(outputDirectory, $"{envelope.Uuid}.pdf");
await File.WriteAllBytesAsync(outputPath, merged, context.CancellationToken);

View File

@@ -1,7 +1,6 @@
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using EnvelopeGenerator.CommonServices.Jobs.FinalizeDocument.FinalizeDocumentExceptions;
using iText.IO.Image;
using iText.Kernel.Colors;
using iText.Kernel.Pdf;
@@ -11,8 +10,10 @@ using iText.Layout.Element;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Newtonsoft.Json;
using static EnvelopeGenerator.Jobs.FinalizeDocument.FinalizeDocumentExceptions;
using LayoutImage = iText.Layout.Element.Image;
namespace EnvelopeGenerator.CommonServices.Jobs.FinalizeDocument;
namespace EnvelopeGenerator.Jobs.FinalizeDocument;
public class PDFBurner
{
@@ -110,17 +111,20 @@ public class PDFBurner
}
var page = pdf.GetPage(annotation.pageIndex + 1);
var canvas = new PdfCanvas(page);
var bounds = annotation.bbox.Select(ToInches).ToList();
var x = bounds[0];
var y = bounds[1];
var width = bounds[2];
var height = bounds[3];
var x = (float)bounds[0];
var y = (float)bounds[1];
var width = (float)bounds[2];
var height = (float)bounds[3];
var imageBytes = Convert.FromBase64String(attachment.binary);
var imageData = ImageDataFactory.Create(imageBytes);
canvas.AddImageAt(imageData, x, y, false)
.ScaleToFit(width, height);
var image = new LayoutImage(imageData)
.ScaleAbsolute(width, height)
.SetFixedPosition(annotation.pageIndex + 1, x, y);
using var canvas = new Canvas(new PdfCanvas(page), page.GetPageSize());
canvas.Add(image);
}
private void AddInkAnnotation(PdfDocument pdf, Annotation annotation)
@@ -159,21 +163,31 @@ public class PDFBurner
private void AddFormFieldValue(PdfDocument pdf, Annotation annotation, string value)
{
var bounds = annotation.bbox.Select(ToInches).ToList();
var x = bounds[0];
var y = bounds[1];
var width = bounds[2];
var height = bounds[3];
var x = (float)bounds[0];
var y = (float)bounds[1];
var width = (float)bounds[2];
var height = (float)bounds[3];
var page = pdf.GetPage(annotation.pageIndex + 1);
var canvas = new Canvas(new PdfCanvas(page), page.GetPageSize());
canvas.ShowTextAligned(new Paragraph(value)
.SetFontSize(_pdfBurnerParams.FontSize)
.SetFontColor(ColorConstants.BLACK)
.SetFontFamily(_pdfBurnerParams.FontName)
.SetItalic(_pdfBurnerParams.FontStyle.HasFlag(FontStyle.Italic))
.SetBold(_pdfBurnerParams.FontStyle.HasFlag(FontStyle.Bold)),
x + _pdfBurnerParams.TopMargin,
y + _pdfBurnerParams.YOffset,
var paragraph = new Paragraph(value)
.SetFontSize(_pdfBurnerParams.FontSize)
.SetFontColor(ColorConstants.BLACK)
.SetFontFamily(_pdfBurnerParams.FontName);
if (_pdfBurnerParams.FontStyle.HasFlag(FontStyle.Italic))
{
paragraph.SetItalic();
}
if (_pdfBurnerParams.FontStyle.HasFlag(FontStyle.Bold))
{
paragraph.SetBold();
}
canvas.ShowTextAligned(paragraph,
x + (float)_pdfBurnerParams.TopMargin,
y + (float)_pdfBurnerParams.YOffset,
annotation.pageIndex + 1,
iText.Layout.Properties.TextAlignment.LEFT,
iText.Layout.Properties.VerticalAlignment.TOP,

View File

@@ -1,7 +1,7 @@
using System.Collections.Generic;
using System.Drawing;
namespace EnvelopeGenerator.CommonServices.Jobs.FinalizeDocument;
namespace EnvelopeGenerator.Jobs.FinalizeDocument;
public class PDFBurnerParams
{

View File

@@ -1,11 +1,11 @@
using System.IO;
using EnvelopeGenerator.CommonServices.Jobs.FinalizeDocument.FinalizeDocumentExceptions;
using iText.Kernel.Pdf;
using iText.Kernel.Utils;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using static EnvelopeGenerator.Jobs.FinalizeDocument.FinalizeDocumentExceptions;
namespace EnvelopeGenerator.CommonServices.Jobs.FinalizeDocument;
namespace EnvelopeGenerator.Jobs.FinalizeDocument;
public class PDFMerger
{

View File

@@ -1,15 +1,15 @@
using System.Data;
using System.IO;
using EnvelopeGenerator.CommonServices.Jobs.FinalizeDocument.FinalizeDocumentExceptions;
using EnvelopeGenerator.Domain.Entities;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using static EnvelopeGenerator.Jobs.FinalizeDocument.FinalizeDocumentExceptions;
using LayoutDocument = iText.Layout.Document;
namespace EnvelopeGenerator.CommonServices.Jobs.FinalizeDocument;
namespace EnvelopeGenerator.Jobs.FinalizeDocument;
public class ReportCreator
{
@@ -32,7 +32,7 @@ public class ReportCreator
using var stream = new MemoryStream();
using var writer = new PdfWriter(stream);
using var pdf = new PdfDocument(writer);
using var document = new Document(pdf);
using var document = new LayoutDocument(pdf);
document.Add(new Paragraph("Envelope Finalization Report").SetFontSize(16));
document.Add(new Paragraph($"Envelope Id: {envelope.Id}"));

View File

@@ -1,7 +1,7 @@
using EnvelopeGenerator.Domain.Constants;
using EnvelopeGenerator.Domain.Entities;
namespace EnvelopeGenerator.CommonServices.Jobs.FinalizeDocument;
namespace EnvelopeGenerator.Jobs.FinalizeDocument;
public class ReportItem
{

View File

@@ -1,6 +1,6 @@
using System.Collections.Generic;
namespace EnvelopeGenerator.CommonServices.Jobs.FinalizeDocument;
namespace EnvelopeGenerator.Jobs.FinalizeDocument;
public class ReportSource
{