PDFMerger now uses dependency injection for LicenseManager and AnnotationManager, improving modularity and testability. Removed inheritance from BaseClass and cleaned up unused usings.
61 lines
2.1 KiB
C#
61 lines
2.1 KiB
C#
using EnvelopeGenerator.ServiceHost.Exceptions;
|
|
using GdPicture14;
|
|
using Microsoft.Extensions.Options;
|
|
|
|
namespace EnvelopeGenerator.ServiceHost.Jobs.FinalizeDocument;
|
|
|
|
public class PDFMerger
|
|
{
|
|
private readonly AnnotationManager _manager;
|
|
private readonly LicenseManager _licenseManager;
|
|
|
|
private const bool AllowRasterization = true;
|
|
private const bool AllowVectorization = true;
|
|
|
|
private readonly PdfConversionConformance _pdfaConformanceLevel = PdfConversionConformance.PDF_A_1b;
|
|
|
|
public PDFMerger(LicenseManager licenseManager, AnnotationManager annotationManager)
|
|
{
|
|
_licenseManager = licenseManager;
|
|
_manager = annotationManager;
|
|
}
|
|
|
|
public byte[] MergeDocuments(byte[] document, byte[] report)
|
|
{
|
|
using var documentStream = new MemoryStream(document);
|
|
using var reportStream = new MemoryStream(report);
|
|
using var finalStream = new MemoryStream();
|
|
using var documentPdf = new GdPicturePDF();
|
|
using var reportPdf = new GdPicturePDF();
|
|
|
|
documentPdf.LoadFromStream(documentStream, true);
|
|
var status = documentPdf.GetStat();
|
|
if (status != GdPictureStatus.OK)
|
|
{
|
|
throw new MergeDocumentException($"Document could not be loaded: {status}");
|
|
}
|
|
|
|
reportPdf.LoadFromStream(reportStream, true);
|
|
status = reportPdf.GetStat();
|
|
if (status != GdPictureStatus.OK)
|
|
{
|
|
throw new MergeDocumentException($"Report could not be loaded: {status}");
|
|
}
|
|
|
|
var mergedPdf = documentPdf.Merge2Documents(documentPdf, reportPdf);
|
|
status = mergedPdf.GetStat();
|
|
if (status != GdPictureStatus.OK)
|
|
{
|
|
throw new MergeDocumentException($"Documents could not be merged: {status}");
|
|
}
|
|
|
|
mergedPdf.ConvertToPDFA(finalStream, _pdfaConformanceLevel, AllowVectorization, AllowRasterization);
|
|
status = documentPdf.GetStat();
|
|
if (status != GdPictureStatus.OK)
|
|
{
|
|
throw new MergeDocumentException($"Document could not be converted to PDF/A: {status}");
|
|
}
|
|
|
|
return finalStream.ToArray();
|
|
}
|
|
} |