←Select platform

ExtractAttachment(Stream,int,string) Method

Summary

Extracts an attachment from an owner's file stored in a stream, and saves it to an output file.

Syntax
C#
C++/CLI
Python
public void ExtractAttachment( 
   Stream stream, 
   int attachmentNumber, 
   string outputFileName 
) 
public:  
   void ExtractAttachment( 
      Stream^ stream, 
      Int32 attachmentNumber, 
      String^ outputFileName 
   ) 
def ExtractAttachment(self,stream,attachmentNumber,outputFileName): 

Parameters

stream

Stream containing the data of the owner's file.

attachmentNumber

1-based attachment number to extract. This value must be between 1 and the number of attachments in the file.

outputFileName

File that will contain the attachment file's data.

Remarks

Call CodecsImageInfo.AttachmentCount to quickly determine the number of attachments found a file.

Call ReadAttachments to obtain the number and properties of all attachments found a file.

Example
C#
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)) 
      { 
         Debug.WriteLine("Information"); 
         Debug.WriteLine("Format:" + imageInfo.Format); 
         // If PDF, check if it is portfolio 
         if (imageInfo.Format == RasterImageFormat.RasPdf) 
            Debug.WriteLine("IsPortfolio:" + imageInfo.IsPortfolio); 
         attachmentCount = imageInfo.AttachmentCount; 
         Debug.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 
         Debug.WriteLine("Attachments is not supported for this file format"); 
         return; 
      } 
 
      // Sanity check 
      Debug.Assert(attachments.Count == attachmentCount); 
      Debug.Assert(attachments.OriginalFormat == RasterImageFormat.Unknown); 
 
      if (attachments.Count == 0) 
      { 
         Debug.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); 
 
         var description = attachment.Description; 
         var displayName = attachment.DisplayName; 
         var fileLength = attachment.FileLength; 
         var metaData = attachment.Metadata; 
         var timeCreated = attachment.TimeCreated; 
         var timeModified = attachment.TimeModified; 
                
         Debug.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)) 
            { 
               Debug.WriteLine($" attachment format is {imageInfo.Format} and has {imageInfo.TotalPages} pages"); 
            } 
         } 
         catch (Exception ex) 
         { 
            Debug.WriteLine($" attachment format could not be obtained, error {ex.Message}"); 
         } 
      } 
   } 
} 
Requirements

Target Platforms

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

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