diff --git a/EnvelopeGenerator.Application/Pdf/BurnPdfCommand.cs b/EnvelopeGenerator.Application/Pdf/BurnPdfCommand.cs index 98bdc1fd..c94ffc23 100644 --- a/EnvelopeGenerator.Application/Pdf/BurnPdfCommand.cs +++ b/EnvelopeGenerator.Application/Pdf/BurnPdfCommand.cs @@ -3,10 +3,10 @@ using EnvelopeGenerator.Application.Common.Configurations; using EnvelopeGenerator.Application.Exceptions; using EnvelopeGenerator.Domain.Constants; using EnvelopeGenerator.Domain.Entities; -using EnvelopeGenerator.PdfEditor; using GdPicture14; using MediatR; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; namespace EnvelopeGenerator.Application.Pdf; @@ -31,17 +31,21 @@ public class BurnPdfCommandHandler : IRequestHandler private readonly IRepository _signRepo; + private readonly ILogger _logger; + /// /// /// /// /// /// - public BurnPdfCommandHandler(PDFBurnerParams pdfBurnerParams, AnnotationManager manager, IRepository signRepo) + /// + public BurnPdfCommandHandler(PDFBurnerParams pdfBurnerParams, AnnotationManager manager, IRepository signRepo, ILogger logger) { _pdfBurnerParams = pdfBurnerParams; _manager = manager; _signRepo = signRepo; + _logger = logger; } /// @@ -162,6 +166,52 @@ public class BurnPdfCommandHandler : IRequestHandler /// /// public byte[] BurnInstantJSONAnnotsToPDF(byte[] pSourceBuffer, List pInstantJSONList) + { + GdPictureStatus oResult; + + using var oSourceStream = new MemoryStream(pSourceBuffer); + // Open PDF + oResult = _manager.InitFromStream(oSourceStream); + if (oResult != GdPictureStatus.OK) + { + throw new BurnAnnotationException($"Could not open document for burning: [{oResult}]"); + } + + // Add annotation to PDF + foreach (var oJSON in pInstantJSONList) + { + try + { + AddInstantJSONAnnotationToPDF(oJSON); + } + catch (Exception ex) + { + _logger.LogWarning("Error in AddInstantJSONAnnotationToPDF - oJson: "); + _logger.LogWarning(oJSON); + throw new BurnAnnotationException("Adding Annotation failed", ex); + } + } + + oResult = _manager.BurnAnnotationsToPage(RemoveInitialAnnots: true, VectorMode: true); + if (oResult != GdPictureStatus.OK) + { + throw new BurnAnnotationException($"Could not burn annotations to file: [{oResult}]"); + } + + // Save PDF + using var oNewStream = new MemoryStream(); + oResult = _manager.SaveDocumentToPDF(oNewStream); + if (oResult != GdPictureStatus.OK) + { + throw new BurnAnnotationException($"Could not save document to stream: [{oResult}]"); + } + + _manager.Close(); + + return oNewStream.ToArray(); + } + + private void AddInstantJSONAnnotationToPDF(string pInstantJSON) { throw new NotImplementedException(); }