←Select platform

ReadAttachments(Stream) Method

Summary

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

Syntax
C#
C++/CLI
Python
public CodecsAttachments ReadAttachments( 
   Stream stream 
) 
public:  
   CodecsAttachments^ ReadAttachments( 
      Stream^ stream 
   ) 
def ReadAttachments(self,stream): 

Parameters

stream

Stream containing the owner file that may contain embedded attachments.

Return Value

Collection of CodecsAttachment objects containing the properties of any attachments found. If the owner file neither contains nor supports attachments, then an empty collection is returned.

Remarks

This method returns an empty collection in the following situations:

  • The owner file does not contain any attachments
  • The owner file does not support attachments
  • The owner file supports attachments, but reading support is not available in this version of the LEADTOOLS toolkit

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 the properties of its attachment such as its FileName, FileLength, and other extra information provided by the owner document.

Use ExtractAttachment to extract the binary content of an attachment file onto an output disk file or stream for further processing. Examples of further processing include calling GetInformation or Load.

LEADTOOLS supports reading the attachments embedded in the following file formats:

PDF

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" and any number of attachments and a schema to control how to view the document. The value of CodecsImageInfo.IsPortfolio will be true if the file is a PDF portfolio; it is up to the application to determine how to further handle the file.

Example
C#
Java
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}"); 
         } 
      } 
   } 
} 
 
import java.io.*; 
import java.net.*; 
import java.nio.file.Paths; 
import java.util.*; 
import java.time.Instant; 
import java.time.Duration; 
 
import org.junit.*; 
import org.junit.runner.JUnitCore; 
import org.junit.runner.Result; 
import org.junit.runner.notification.Failure; 
import static org.junit.Assert.*; 
 
import leadtools.*; 
import leadtools.codecs.*; 
import leadtools.codecs.RasterCodecs.FeedCallbackThunk; 
import leadtools.drawing.internal.*; 
import leadtools.imageprocessing.*; 
import leadtools.imageprocessing.color.ChangeIntensityCommand; 
import leadtools.svg.*; 
 
 
public void readAttachmentsExample() throws IOException { 
   final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images"; 
   String fileName = combine(LEAD_VARS_IMAGES_DIR, "pdfsegmentation.pdf"); 
   String outputDir = combine(LEAD_VARS_IMAGES_DIR, "attachments_test.pdf"); 
   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. 
   CodecsImageInfo imageInfo = rasterCodecs.getInformation(fileName, true); 
 
   System.out.println("Information"); 
   System.out.println("Format:" + imageInfo.getFormat()); 
   // If PDF, check if it is portfolio 
   if (imageInfo.getFormat() == RasterImageFormat.RAS_PDF) 
      System.out.println("IsPortfolio:" + imageInfo.isPortfolio()); 
   attachmentCount = imageInfo.getAttachmentCount(); 
   System.out.println("Attachments:" + imageInfo.getAttachmentCount()); 
 
   // 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 
      System.out.println("Attachments is not supported for this file format"); 
      return; 
   } 
 
   // Sanity check 
   assertTrue(attachments.size() == attachmentCount); 
   assertTrue(attachments.getOriginalFormat() == RasterImageFormat.UNKNOWN); 
 
   if (attachments.size() == 0) { 
      System.out.println("No attachments to extract"); 
      return; 
   } 
 
   // Create the output directory if it does not exist 
   File outputDirFile = new File(outputDir); 
   if (!outputDirFile.exists()) 
      outputDirFile.createNewFile(); 
 
   // Extract the attachments 
   for (CodecsAttachment attachment : attachments) { 
      // Get the output file name 
      String outputFileName = combine(outputDir, attachment.getFileName()); 
 
      String description = attachment.getDescription(); 
      String displayName = attachment.getDisplayName(); 
      long fileLength = attachment.getFileLength(); 
      Map<String, String> metaData = attachment.getMetadata(); 
      Date timeCreated = attachment.getTimeCreated(); 
      Date timeModified = attachment.getTimeModified(); 
 
      System.out.println("Description: " + description); 
      System.out.println("Display name: " + displayName); 
      System.out.println("File length: " + fileLength); 
      System.out.println("Meta data: " + metaData); 
      System.out.println("Time created: " + timeCreated); 
      System.out.println("Time modified: " + timeModified); 
 
      System.out.println("Extracting attachment to output file: " + outputFileName); 
      rasterCodecs.extractAttachment(fileName, attachment.getAttachmentNumber(), outputFileName); 
 
      // Show information on this attachment 
      try { 
         imageInfo = rasterCodecs.getInformation(outputFileName, true); 
 
         System.out.println(" attachment format is {imageInfo.Format} and has {imageInfo.TotalPages} pages"); 
 
      } catch (Exception ex) { 
         System.out.println(" attachment format could not be obtained, error {ex.Message}"); 
      } 
   } 
} 
Requirements

Target Platforms

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

Leadtools.Codecs Assembly

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