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:
@@ -1,7 +0,0 @@
|
|||||||
namespace EnvelopeGenerator.Jobs
|
|
||||||
{
|
|
||||||
public class Class1
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -19,23 +19,4 @@
|
|||||||
<ProjectReference Include="..\EnvelopeGenerator.PdfEditor\EnvelopeGenerator.PdfEditor.csproj" />
|
<ProjectReference Include="..\EnvelopeGenerator.PdfEditor\EnvelopeGenerator.PdfEditor.csproj" />
|
||||||
</ItemGroup>
|
</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>
|
</Project>
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ using Microsoft.Extensions.Logging;
|
|||||||
using Microsoft.Extensions.Logging.Abstractions;
|
using Microsoft.Extensions.Logging.Abstractions;
|
||||||
using Quartz;
|
using Quartz;
|
||||||
|
|
||||||
namespace EnvelopeGenerator.CommonServices.Jobs.APIBackendJobs;
|
namespace EnvelopeGenerator.Jobs.APIBackendJobs;
|
||||||
|
|
||||||
public class APIEnvelopeJob : IJob
|
public class APIEnvelopeJob : IJob
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Data;
|
using System.Data;
|
||||||
|
|
||||||
namespace EnvelopeGenerator.CommonServices.Jobs;
|
namespace EnvelopeGenerator.Jobs;
|
||||||
|
|
||||||
public static class DataRowExtensions
|
public static class DataRowExtensions
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
namespace EnvelopeGenerator.CommonServices.Jobs.FinalizeDocument;
|
namespace EnvelopeGenerator.Jobs.FinalizeDocument;
|
||||||
|
|
||||||
public static class FinalizeDocumentExceptions
|
public static class FinalizeDocumentExceptions
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -4,15 +4,14 @@ using System.IO;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using EnvelopeGenerator.CommonServices.Jobs.FinalizeDocument.FinalizeDocumentExceptions;
|
|
||||||
using EnvelopeGenerator.Domain.Constants;
|
using EnvelopeGenerator.Domain.Constants;
|
||||||
using EnvelopeGenerator.Domain.Entities;
|
using EnvelopeGenerator.Domain.Entities;
|
||||||
using Microsoft.Data.SqlClient;
|
using Microsoft.Data.SqlClient;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Microsoft.Extensions.Logging.Abstractions;
|
|
||||||
using Quartz;
|
using Quartz;
|
||||||
|
using static EnvelopeGenerator.Jobs.FinalizeDocument.FinalizeDocumentExceptions;
|
||||||
|
|
||||||
namespace EnvelopeGenerator.CommonServices.Jobs.FinalizeDocument;
|
namespace EnvelopeGenerator.Jobs.FinalizeDocument;
|
||||||
|
|
||||||
public class FinalizeDocumentJob : IJob
|
public class FinalizeDocumentJob : IJob
|
||||||
{
|
{
|
||||||
@@ -23,17 +22,16 @@ public class FinalizeDocumentJob : IJob
|
|||||||
|
|
||||||
private record ConfigSettings(string DocumentPath, string DocumentPathOrigin, string ExportPath);
|
private record ConfigSettings(string DocumentPath, string DocumentPathOrigin, string ExportPath);
|
||||||
|
|
||||||
public FinalizeDocumentJob()
|
public FinalizeDocumentJob(
|
||||||
: this(NullLogger<FinalizeDocumentJob>.Instance)
|
ILogger<FinalizeDocumentJob> logger,
|
||||||
{
|
PDFBurner pdfBurner,
|
||||||
}
|
PDFMerger pdfMerger,
|
||||||
|
ReportCreator reportCreator)
|
||||||
public FinalizeDocumentJob(ILogger<FinalizeDocumentJob> logger)
|
|
||||||
{
|
{
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_pdfBurner = new PDFBurner();
|
_pdfBurner = pdfBurner;
|
||||||
_pdfMerger = new PDFMerger();
|
_pdfMerger = pdfMerger;
|
||||||
_reportCreator = new ReportCreator();
|
_reportCreator = reportCreator;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task Execute(IJobExecutionContext context)
|
public async Task Execute(IJobExecutionContext context)
|
||||||
@@ -77,20 +75,22 @@ public class FinalizeDocumentJob : IJob
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var data = envelopeData.Value;
|
||||||
|
|
||||||
var envelope = new Envelope
|
var envelope = new Envelope
|
||||||
{
|
{
|
||||||
Id = envelopeId,
|
Id = envelopeId,
|
||||||
Uuid = envelopeData.EnvelopeUuid ?? string.Empty,
|
Uuid = data.EnvelopeUuid ?? string.Empty,
|
||||||
Title = envelopeData.Title ?? string.Empty,
|
Title = data.Title ?? string.Empty,
|
||||||
FinalEmailToCreator = FinalEmailType.No,
|
FinalEmailToCreator = (int)FinalEmailType.No,
|
||||||
FinalEmailToReceivers = 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 report = _reportCreator.CreateReport(connection, envelope);
|
||||||
var merged = _pdfMerger.MergeDocuments(burned, report);
|
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);
|
Directory.CreateDirectory(outputDirectory);
|
||||||
var outputPath = Path.Combine(outputDirectory, $"{envelope.Uuid}.pdf");
|
var outputPath = Path.Combine(outputDirectory, $"{envelope.Uuid}.pdf");
|
||||||
await File.WriteAllBytesAsync(outputPath, merged, context.CancellationToken);
|
await File.WriteAllBytesAsync(outputPath, merged, context.CancellationToken);
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using EnvelopeGenerator.CommonServices.Jobs.FinalizeDocument.FinalizeDocumentExceptions;
|
|
||||||
using iText.IO.Image;
|
using iText.IO.Image;
|
||||||
using iText.Kernel.Colors;
|
using iText.Kernel.Colors;
|
||||||
using iText.Kernel.Pdf;
|
using iText.Kernel.Pdf;
|
||||||
@@ -11,8 +10,10 @@ using iText.Layout.Element;
|
|||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Microsoft.Extensions.Logging.Abstractions;
|
using Microsoft.Extensions.Logging.Abstractions;
|
||||||
using Newtonsoft.Json;
|
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
|
public class PDFBurner
|
||||||
{
|
{
|
||||||
@@ -110,17 +111,20 @@ public class PDFBurner
|
|||||||
}
|
}
|
||||||
|
|
||||||
var page = pdf.GetPage(annotation.pageIndex + 1);
|
var page = pdf.GetPage(annotation.pageIndex + 1);
|
||||||
var canvas = new PdfCanvas(page);
|
|
||||||
var bounds = annotation.bbox.Select(ToInches).ToList();
|
var bounds = annotation.bbox.Select(ToInches).ToList();
|
||||||
var x = bounds[0];
|
var x = (float)bounds[0];
|
||||||
var y = bounds[1];
|
var y = (float)bounds[1];
|
||||||
var width = bounds[2];
|
var width = (float)bounds[2];
|
||||||
var height = bounds[3];
|
var height = (float)bounds[3];
|
||||||
|
|
||||||
var imageBytes = Convert.FromBase64String(attachment.binary);
|
var imageBytes = Convert.FromBase64String(attachment.binary);
|
||||||
var imageData = ImageDataFactory.Create(imageBytes);
|
var imageData = ImageDataFactory.Create(imageBytes);
|
||||||
canvas.AddImageAt(imageData, x, y, false)
|
var image = new LayoutImage(imageData)
|
||||||
.ScaleToFit(width, height);
|
.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)
|
private void AddInkAnnotation(PdfDocument pdf, Annotation annotation)
|
||||||
@@ -159,21 +163,31 @@ public class PDFBurner
|
|||||||
private void AddFormFieldValue(PdfDocument pdf, Annotation annotation, string value)
|
private void AddFormFieldValue(PdfDocument pdf, Annotation annotation, string value)
|
||||||
{
|
{
|
||||||
var bounds = annotation.bbox.Select(ToInches).ToList();
|
var bounds = annotation.bbox.Select(ToInches).ToList();
|
||||||
var x = bounds[0];
|
var x = (float)bounds[0];
|
||||||
var y = bounds[1];
|
var y = (float)bounds[1];
|
||||||
var width = bounds[2];
|
var width = (float)bounds[2];
|
||||||
var height = bounds[3];
|
var height = (float)bounds[3];
|
||||||
|
|
||||||
var page = pdf.GetPage(annotation.pageIndex + 1);
|
var page = pdf.GetPage(annotation.pageIndex + 1);
|
||||||
var canvas = new Canvas(new PdfCanvas(page), page.GetPageSize());
|
var canvas = new Canvas(new PdfCanvas(page), page.GetPageSize());
|
||||||
canvas.ShowTextAligned(new Paragraph(value)
|
var paragraph = new Paragraph(value)
|
||||||
.SetFontSize(_pdfBurnerParams.FontSize)
|
.SetFontSize(_pdfBurnerParams.FontSize)
|
||||||
.SetFontColor(ColorConstants.BLACK)
|
.SetFontColor(ColorConstants.BLACK)
|
||||||
.SetFontFamily(_pdfBurnerParams.FontName)
|
.SetFontFamily(_pdfBurnerParams.FontName);
|
||||||
.SetItalic(_pdfBurnerParams.FontStyle.HasFlag(FontStyle.Italic))
|
|
||||||
.SetBold(_pdfBurnerParams.FontStyle.HasFlag(FontStyle.Bold)),
|
if (_pdfBurnerParams.FontStyle.HasFlag(FontStyle.Italic))
|
||||||
x + _pdfBurnerParams.TopMargin,
|
{
|
||||||
y + _pdfBurnerParams.YOffset,
|
paragraph.SetItalic();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_pdfBurnerParams.FontStyle.HasFlag(FontStyle.Bold))
|
||||||
|
{
|
||||||
|
paragraph.SetBold();
|
||||||
|
}
|
||||||
|
|
||||||
|
canvas.ShowTextAligned(paragraph,
|
||||||
|
x + (float)_pdfBurnerParams.TopMargin,
|
||||||
|
y + (float)_pdfBurnerParams.YOffset,
|
||||||
annotation.pageIndex + 1,
|
annotation.pageIndex + 1,
|
||||||
iText.Layout.Properties.TextAlignment.LEFT,
|
iText.Layout.Properties.TextAlignment.LEFT,
|
||||||
iText.Layout.Properties.VerticalAlignment.TOP,
|
iText.Layout.Properties.VerticalAlignment.TOP,
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
|
|
||||||
namespace EnvelopeGenerator.CommonServices.Jobs.FinalizeDocument;
|
namespace EnvelopeGenerator.Jobs.FinalizeDocument;
|
||||||
|
|
||||||
public class PDFBurnerParams
|
public class PDFBurnerParams
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
using EnvelopeGenerator.CommonServices.Jobs.FinalizeDocument.FinalizeDocumentExceptions;
|
|
||||||
using iText.Kernel.Pdf;
|
using iText.Kernel.Pdf;
|
||||||
using iText.Kernel.Utils;
|
using iText.Kernel.Utils;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Microsoft.Extensions.Logging.Abstractions;
|
using Microsoft.Extensions.Logging.Abstractions;
|
||||||
|
using static EnvelopeGenerator.Jobs.FinalizeDocument.FinalizeDocumentExceptions;
|
||||||
|
|
||||||
namespace EnvelopeGenerator.CommonServices.Jobs.FinalizeDocument;
|
namespace EnvelopeGenerator.Jobs.FinalizeDocument;
|
||||||
|
|
||||||
public class PDFMerger
|
public class PDFMerger
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,15 +1,15 @@
|
|||||||
using System.Data;
|
using System.Data;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using EnvelopeGenerator.CommonServices.Jobs.FinalizeDocument.FinalizeDocumentExceptions;
|
|
||||||
using EnvelopeGenerator.Domain.Entities;
|
using EnvelopeGenerator.Domain.Entities;
|
||||||
using iText.Kernel.Pdf;
|
using iText.Kernel.Pdf;
|
||||||
using iText.Layout;
|
|
||||||
using iText.Layout.Element;
|
using iText.Layout.Element;
|
||||||
using Microsoft.Data.SqlClient;
|
using Microsoft.Data.SqlClient;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Microsoft.Extensions.Logging.Abstractions;
|
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
|
public class ReportCreator
|
||||||
{
|
{
|
||||||
@@ -32,7 +32,7 @@ public class ReportCreator
|
|||||||
using var stream = new MemoryStream();
|
using var stream = new MemoryStream();
|
||||||
using var writer = new PdfWriter(stream);
|
using var writer = new PdfWriter(stream);
|
||||||
using var pdf = new PdfDocument(writer);
|
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 Finalization Report").SetFontSize(16));
|
||||||
document.Add(new Paragraph($"Envelope Id: {envelope.Id}"));
|
document.Add(new Paragraph($"Envelope Id: {envelope.Id}"));
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
using EnvelopeGenerator.Domain.Constants;
|
using EnvelopeGenerator.Domain.Constants;
|
||||||
using EnvelopeGenerator.Domain.Entities;
|
using EnvelopeGenerator.Domain.Entities;
|
||||||
|
|
||||||
namespace EnvelopeGenerator.CommonServices.Jobs.FinalizeDocument;
|
namespace EnvelopeGenerator.Jobs.FinalizeDocument;
|
||||||
|
|
||||||
public class ReportItem
|
public class ReportItem
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace EnvelopeGenerator.CommonServices.Jobs.FinalizeDocument;
|
namespace EnvelopeGenerator.Jobs.FinalizeDocument;
|
||||||
|
|
||||||
public class ReportSource
|
public class ReportSource
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user