From 02272f980f972b049fca6d7bc91f3fc56c23b161 Mon Sep 17 00:00:00 2001 From: Jonathan Jenne Date: Tue, 14 Nov 2023 10:10:05 +0100 Subject: [PATCH] Add PDFConverter --- Interfaces/Interfaces.vbproj | 1 + Interfaces/ZUGFeRDInterface.vb | 2 - Interfaces/ZUGFeRDInterface/PDFConverter.vb | 62 +++++++++++++++++++++ 3 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 Interfaces/ZUGFeRDInterface/PDFConverter.vb diff --git a/Interfaces/Interfaces.vbproj b/Interfaces/Interfaces.vbproj index a7c9f220..e8652081 100644 --- a/Interfaces/Interfaces.vbproj +++ b/Interfaces/Interfaces.vbproj @@ -111,6 +111,7 @@ Settings.settings True + diff --git a/Interfaces/ZUGFeRDInterface.vb b/Interfaces/ZUGFeRDInterface.vb index cc3dccf9..0b6a59da 100644 --- a/Interfaces/ZUGFeRDInterface.vb +++ b/Interfaces/ZUGFeRDInterface.vb @@ -358,6 +358,4 @@ Public Class ZUGFeRDInterface Throw New ZUGFeRDExecption(ErrorType.NoValidZugferd, oMessage) End Try End Function - - End Class diff --git a/Interfaces/ZUGFeRDInterface/PDFConverter.vb b/Interfaces/ZUGFeRDInterface/PDFConverter.vb new file mode 100644 index 00000000..19caa813 --- /dev/null +++ b/Interfaces/ZUGFeRDInterface/PDFConverter.vb @@ -0,0 +1,62 @@ +Imports DigitalData.Modules.Logging +Imports GdPicture14 + +Public Class PDFConverter + Private ReadOnly _LogConfig As LogConfig + Private ReadOnly _Logger As Logger + + Public Sub New(pLogConfig As LogConfig) + _LogConfig = pLogConfig + _Logger = pLogConfig.GetLogger() + End Sub + + Public Function ConvertPDFADocumentToPDFDocument(pFilePath As String, pNewFilePath As String) As Boolean + Try + Using oGdPicturePDF As New GdPicturePDF() + Using oGdPicturePDFDestination As New GdPicturePDF() + + ' Load the source file into memory + If oGdPicturePDF.LoadFromFile(pFilePath, True) <> GdPictureStatus.OK Then + Throw New ApplicationException("File could not be loaded!") + End If + + ' Create a new pdf file + If oGdPicturePDFDestination.NewPDF() <> GdPictureStatus.OK Then + Throw New ApplicationException("New Pdf could not be created!") + End If + + ' Copy all pages from the source into the new pdf + If oGdPicturePDFDestination.ClonePages(oGdPicturePDF, "*") <> GdPictureStatus.OK Then + Throw New ApplicationException("Document could not be copied into new pdf!") + End If + + ' Close the source document + If oGdPicturePDF.CloseDocument() <> GdPictureStatus.OK Then + Throw New ApplicationException("Source document could not be closed!") + End If + + ' Set the file path + Dim oFinalFilePath = pFilePath + If Not String.IsNullOrWhiteSpace(pNewFilePath) Then + oFinalFilePath = pNewFilePath + End If + + ' Save the file to disk + If oGdPicturePDFDestination.SaveToFile(oFinalFilePath) <> GdPictureStatus.OK Then + Throw New ApplicationException("New document could not be saved to disk!") + End If + + If oGdPicturePDFDestination.CloseDocument() <> GdPictureStatus.OK Then + Throw New ApplicationException("Destination document could not be closed!") + End If + + End Using + End Using + + Return True + Catch ex As Exception + _Logger.Error(ex) + Return False + End Try + End Function +End Class