2024-01-04 10:15:55 +01:00

72 lines
3.1 KiB
VB.net

Imports DigitalData.Modules.Base
Imports DigitalData.Modules.Logging
Imports EnvelopeGenerator.Common.Jobs.FinalizeDocument.FinalizeDocumentExceptions
Imports GdPicture14
Imports System.IO
Public Class PDFMerger
Inherits BaseClass
Private ReadOnly Manager As AnnotationManager
Private ReadOnly LicenseManager As LicenseManager
Private Const ALLOW_RASTERIZATION = True
Private Const ALLOW_VECTORIZATION = True
Private ReadOnly PDFAConformanceLevel As PdfConversionConformance = PdfConversionConformance.PDF_A_1b
Public Sub New(pLogConfig As LogConfig, pGDPictureLicenseKey As String)
MyBase.New(pLogConfig)
LicenseManager = New LicenseManager()
LicenseManager.RegisterKEY(pGDPictureLicenseKey)
Manager = New AnnotationManager()
End Sub
Public Function MergeDocuments(pDocument As Byte(), pReport As Byte()) As Byte()
Using oDocumentStream As New MemoryStream(pDocument)
Using oReportStream As New MemoryStream(pReport)
Using oFinalStream As New MemoryStream()
Using oDocumentPDF As New GdPicturePDF()
Using oReportPDF As New GdPicturePDF()
Dim oStatus As GdPictureStatus = GdPictureStatus.OK
' Load the source file into memory
oDocumentPDF.LoadFromStream(oDocumentStream, True)
oStatus = oDocumentPDF.GetStat()
If oStatus <> GdPictureStatus.OK Then
Throw New MergeDocumentException($"Document could not be loaded: {oStatus}")
End If
' Load the report file into memory
oReportPDF.LoadFromStream(oReportStream, True)
oStatus = oReportPDF.GetStat()
If oStatus <> GdPictureStatus.OK Then
Throw New MergeDocumentException($"Report could not be loaded: {oStatus}")
End If
' Merge the documents
Dim oMergedPDF = oDocumentPDF.Merge2Documents(oDocumentPDF, oReportPDF)
oStatus = oMergedPDF.GetStat()
If oStatus <> GdPictureStatus.OK Then
Throw New MergeDocumentException($"Documents could not be merged: {oStatus}")
End If
' Convert to PDF/A
oMergedPDF.ConvertToPDFA(oFinalStream, PDFAConformanceLevel, ALLOW_VECTORIZATION, ALLOW_RASTERIZATION)
oStatus = oDocumentPDF.GetStat()
If oStatus <> GdPictureStatus.OK Then
Throw New MergeDocumentException($"Document could not be converted to PDF/A: {oStatus}")
End If
Return oFinalStream.ToArray()
End Using
End Using
End Using
End Using
End Using
End Function
End Class