←Select platform

ReadAttachments(string) Method

Summary

Reads the properties of the attachments embedded in the specified file.

Syntax
C#
VB
C++
public CodecsAttachments ReadAttachments( 
   string fileName 
) 
Public Function ReadAttachments( 
   ByVal fileName As String 
) As CodecsAttachments 
public:  
   CodecsAttachments^ ReadAttachments( 
      String^ fileName 
   ) 

Parameters

fileName

Owner file that may contain embedded attachments.

Return Value

Collection of CodecsAttachment with the properties of any attachments found.

Remarks

ReadAttachments(string) returns the following:

  • If the format of the owner file supports attachments, such as PDF, then a valid CodecsAttachments is always returned. The number of items in the collection equals the number of attachments found in the file.

  • If the format of the owner file does not support attachments, such as PNG or BMP, or LEADTOOLS does not support reading attachments of these types of formats, then null is returned.

The number of attachment items returned by this method is the same as the CodecsImageInfo.AttachmentCount value of the object returned by GetInformation.

Each CodecsAttachment contains properties of the attachment such as its FileName, FileLength, and other extra information provided by the owner document.

Call ExtractAttachment to extract the binary content of the attachment file into an output disk file or stream for further processing (such as using GetInformation or Load).

PDF

LEADTOOLS supports reading the attachments embedded in the PDF file format.

PDF files support embedded attachments of any number and file format (PDF, TIFF, XML, etc). PDF files can also be created as a portfolio, which contains multiple files assembled into an integrated unit. In these types of documents, the file contains a single generic help page with text such as "For the best experience, open this PDF portfolio in a compatible viewer". It also contains any number of attachments as well as a schema to control how to view the document. The value of CodecsImageInfo.IsPortfolio will be true if the file is a PDF portfolio and it is up to the application to determine any additional handling of the file.

Example

This example will do the following:

  • Show information on all the attachments embedded in the file
  • Extract the attachment files into an output directory
  • Show the file format and page count of each attachment
C#
VB
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.ImageProcessing; 
using Leadtools.ImageProcessing.Color; 
using Leadtools.Svg; 
 
 
private static void ReadAttachmentsExample(string fileName, string outputDir) 
{ 
	using (RasterCodecs rasterCodecs = new RasterCodecs()) 
	{ 
 
		int attachmentCount; 
 
		// Get information on the owner file 
		// This step is optional if we are not interested in determining whether the owner file format 
		// or whether it is a PDF portfolio. 
		using (CodecsImageInfo imageInfo = rasterCodecs.GetInformation(fileName, true)) 
		{ 
			Console.WriteLine("Information"); 
			Console.WriteLine("Format:" + imageInfo.Format); 
			// If PDF, check if it is portfolio 
			if (imageInfo.Format == RasterImageFormat.RasPdf) 
				Console.WriteLine("IsPortfolio:" + imageInfo.IsPortfolio); 
			attachmentCount = imageInfo.AttachmentCount; 
			Console.WriteLine("Attachments:" + imageInfo.AttachmentCount); 
		} 
 
		// Read the properties of the attachments embedded in this file 
		CodecsAttachments attachments = rasterCodecs.ReadAttachments(fileName); 
 
		if (attachments == null) 
		{ 
			// The format either: 
			// - Does not support attachments 
			// - LEADTOOLS does not support reading its attachments 
			Console.WriteLine("Attachments is not supported for this file format"); 
			return; 
		} 
 
		// Sanity check 
		Debug.Assert(attachments.Count == attachmentCount); 
 
		if (attachments.Count == 0) 
		{ 
			Console.WriteLine("No attachments to extract"); 
			return; 
		} 
 
		// Create the output directory if it does not exist 
		if (!Directory.Exists(outputDir)) 
			Directory.CreateDirectory(outputDir); 
 
		// Extract the attachments 
		foreach (CodecsAttachment attachment in attachments) 
		{ 
			// Get the output file name 
			string outputFileName = Path.Combine(outputDir, attachment.FileName); 
 
			Console.WriteLine("Extracting attachment to output file: " + outputFileName); 
			rasterCodecs.ExtractAttachment(fileName, attachment.AttachmentNumber, outputFileName); 
 
			// Show information on this attachment 
			try 
			{ 
				using (CodecsImageInfo imageInfo = rasterCodecs.GetInformation(outputFileName, true)) 
				{ 
					Console.WriteLine($" attachment format is {imageInfo.Format} and has {imageInfo.TotalPages} pages"); 
				} 
			} 
			catch (Exception ex) 
			{ 
				Console.WriteLine($" attachment format could not be obtained, error {ex.Message}"); 
			} 
		} 
	} 
} 
Imports Leadtools 
Imports Leadtools.Codecs 
Imports Leadtools.ImageProcessing 
Imports Leadtools.ImageProcessing.Color 
Imports Leadtools.Drawing 
Imports Leadtools.Svg 
 
Private Shared Sub ReadAttachmentsExample(fileName As String, outputDir As String) 
   Using rasterCodecs As New RasterCodecs() 
      Dim attachmentCount As Integer 
 
      ' Get information on the owner file 
      ' This step is optional if we are not interested in determining whether the owner file format 
      ' or whether it is a PDF portfolio. 
      Using imageInfo As CodecsImageInfo = rasterCodecs.GetInformation(fileName, True) 
         Console.WriteLine("Information") 
         Console.WriteLine("Format:" + imageInfo.Format.ToString()) 
         ' If PDF, check if it is portfolio 
         If imageInfo.Format = RasterImageFormat.RasPdf Then 
            Console.WriteLine("IsPortfolio:" + imageInfo.IsPortfolio.ToString()) 
         End If 
         attachmentCount = imageInfo.AttachmentCount 
         Console.WriteLine("Attachments:" + imageInfo.AttachmentCount.ToString()) 
      End Using 
 
      ' Read the properties of the attachments embedded in this file 
      Dim attachments As CodecsAttachments = rasterCodecs.ReadAttachments(fileName) 
 
      If IsNothing(attachments) Then 
         ' The format either: 
         ' - Does not support attachments 
         ' - LEADTOOLS does not support reading its attachments 
         Console.WriteLine("Attachments is not supported for this file format") 
         Return 
      End If 
 
      ' Sanity check 
      Debug.Assert(attachments.Count = attachmentCount) 
 
      If attachments.Count = 0 Then 
         Console.WriteLine("No attachments to extract") 
         Return 
      End If 
 
      ' Create the output directory if it does not exist 
      If Not Directory.Exists(outputDir) Then 
         Directory.CreateDirectory(outputDir) 
      End If 
 
      ' Extract the attachments 
      For Each attachment As CodecsAttachment In attachments 
         ' Get the output file name 
         Dim outputFileName As String = Path.Combine(outputDir, attachment.FileName) 
 
         Console.WriteLine("Extracting attachment to output file: " + outputFileName) 
         rasterCodecs.ExtractAttachment(fileName, attachment.AttachmentNumber, outputFileName) 
 
         ' Show information on this attachment 
         Try 
            Using imageInfo As CodecsImageInfo = rasterCodecs.GetInformation(outputFileName, True) 
               Console.WriteLine($" attachment format is {imageInfo.Format} and has {imageInfo.TotalPages} pages") 
            End Using 
         Catch ex As Exception 
            Console.WriteLine($" attachment format could not be obtained, error {ex.Message}") 
         End Try 
      Next 
   End Using 
End Sub 
Requirements

Target Platforms

Help Version 22.0.2021.9.9
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Codecs Assembly
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.