Imports System.Collections.Generic Imports System.IO Imports DigitalData.Modules.Logging Imports GdPicture14 Public Class PDFAttachments Private ReadOnly Logger As Logger Public Const ZUGFERD_XML_FILENAME = "ZUGFeRD-invoice.xml" Public Class AttachmentResult Public FileName As String Public FileContents As Byte() End Class Public Sub New(LogConfig As LogConfig) Logger = LogConfig.GetLogger End Sub ''' ''' Extracts all embedded files from a PDF file. ''' Note: This does NOT filter out `ZUGFeRD-invoice.xml` anymore to allow for a more generic use. ''' ''' ''' ''' Public Function Extract(FileName As String, AllowedExtensions As List(Of String)) As List(Of AttachmentResult) Dim oResults As New List(Of AttachmentResult) Dim oExtensions = AllowedExtensions.ConvertAll(New Converter(Of String, String)(Function(ext) ext.ToUpper)) Try Using oGDPicturePDF As New GdPicturePDF() If oGDPicturePDF.LoadFromFile(FileName, False) = GdPictureStatus.OK Then Dim oEmbeddedFileCount As Integer = oGDPicturePDF.GetEmbeddedFileCount() If oGDPicturePDF.GetStat() = GdPictureStatus.OK Then If oEmbeddedFileCount > 0 Then For index = 0 To oEmbeddedFileCount - 1 Dim oFileName As String = oGDPicturePDF.GetEmbeddedFileName(index) If oGDPicturePDF.GetStat() = GdPictureStatus.OK Then Dim oExtension = New FileInfo(oFileName).Extension.ToUpper.Substring(1) If oExtensions.Contains(oExtension) Then Dim FileSize As Integer = oGDPicturePDF.GetEmbeddedFileSize(index) If oGDPicturePDF.GetStat() = GdPictureStatus.OK Then Dim oFileData As Byte() = New Byte(FileSize) {} Dim status As GdPictureStatus = oGDPicturePDF.ExtractEmbeddedFile(index, oFileData) If status = GdPictureStatus.OK Then oResults.Add(New AttachmentResult() With { .FileContents = oFileData, .FileName = oFileName }) Else Logger.Error("The embedded file [{0}] has failed to extract. Status: {1}", oFileName, oGDPicturePDF.GetStat().ToString()) Continue For End If Else Logger.Error("An error occurred getting the file size for [{0}]. Status: {1}", oFileName, oGDPicturePDF.GetStat().ToString()) Continue For End If Else Logger.Warn("File [{0}] was skipped because its extension [{1}] is not allowed.", oFileName, oExtension) Continue For End If Else Logger.Error("An error occurred getting the file name for [{0}]. Status: {1}", oFileName, oGDPicturePDF.GetStat().ToString()) Continue For End If Next End If Else Dim oMessage = String.Format("An error occurred getting the number of embedded files. Status: {0}", oGDPicturePDF.GetStat().ToString()) Throw New ApplicationException(oMessage) End If Else Dim oMessage = String.Format("The file [{0}] can't be loaded. Status: [{1}]", FileName, oGDPicturePDF.GetStat().ToString()) Throw New ApplicationException(oMessage) End If End Using Return oResults Catch ex As Exception Logger.Warn("Unexpected Error while Extracting attachments from File [{0}]", FileName) Logger.Error(ex) Return Nothing End Try End Function End Class