←Select platform

AttachmentNumber Property

Summary

Attachment number to load.

Syntax
C#
C++/CLI
Python
public int AttachmentNumber {get; set;} 
public:  
   property Int32 AttachmentNumber 
   { 
      Int32 get() 
      void set(Int32 value) 
   } 
AttachmentNumber # get and set (LoadAttachmentOptions) 

Property Value

1-based attachment number to load. The default value is 0.

Remarks

For more information, refer to DocumentFactory.LoadDocumentAttachment and Document Attachments.

Example
C#
Java
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Document.Writer; 
 
using Leadtools.Document; 
using Leadtools.Caching; 
using Leadtools.Annotations.Engine; 
using Leadtools.Ocr; 
using Leadtools.Barcode; 
using Leadtools.Document.Converter; 
 
public void AttachmentsExample() 
{ 
   // The PDF document can be one of the following: 
   // 1. Normal PDF without attachments. Has one or more pages and no attachments. 
   // 2. Normal PDF with attachments. Has one or more pages and one or more attachments. 
   // 3. PDF Portfolio. Has a placeholder page and one or more attachments. 
 
   // The value of LoadDocumentOptions.LoadAttachmentsMode affects loading the documents as follows: 
 
   // 1. Normal PDF without attachments: Will not be affected. 
   // 2. Normal PDF with attachments: Number of pages is not affected. 
   //    Number of attachments is: 
   //       LoadAttachmentsMode = None then 0 
   //       LoadAttachmentsMode = AsAttachments then number of attachments found in the document. 
   // 3. PDF Portfolio: Number of pages is: 
   //       LoadAttachmentsMode = None then 1 (the placeholder page) 
   //       LoadAttachmentsMode = AsAttachments then 0 (the document has no pages). 
   //    Number of attachments is: 
   //       LoadAttachmentsMode = None then 0 
   //       LoadAttachmentsMode = AsAttachments then number of attachments found in the document. 
 
   // First, load the document without attachments 
   string pdfFile = Path.Combine(LEAD_VARS.ImagesDir, "Leadtools.pdf"); 
   string outputDirectory = Path.GetDirectoryName(LEAD_VARS.ImagesDir); 
 
   Console.WriteLine($"Loading with DocumentLoadAttachmentsMode.None"); 
   var loadDocumentOptions = new LoadDocumentOptions(); 
   loadDocumentOptions.LoadAttachmentsMode = DocumentLoadAttachmentsMode.None; 
   using (LEADDocument document = DocumentFactory.LoadFromFile(pdfFile, loadDocumentOptions)) 
   { 
      Console.WriteLine(document.GetDocumentFileName()); 
      Console.WriteLine($"Document has {document.Pages.Count} pages"); 
 
      // Find out if the document is portfolio 
      bool isPortfolio = 
         document.Metadata.ContainsKey(LEADDocument.MetadataKey_IsPortfolio) && 
         bool.Parse(document.Metadata[LEADDocument.MetadataKey_IsPortfolio]); 
      Console.WriteLine($"IsPortfolio:{isPortfolio}"); 
 
      // Check that everything is as expected 
 
      // We should have 0 attachments since we did not instruct the toolkit to load these 
      Debug.Assert(document.Attachments.Count == 0); 
 
      // We should have one or more pages regardless of whether the document is PDF portfolio 
      Debug.Assert(document.Pages.Count > 0); 
   } 
 
   // Next, load the document with attachments 
   Console.WriteLine($"Loading with DocumentLoadAttachmentsMode.AsAttachments"); 
   loadDocumentOptions.LoadAttachmentsMode = DocumentLoadAttachmentsMode.AsAttachments; 
   using (LEADDocument document = DocumentFactory.LoadFromFile(pdfFile, loadDocumentOptions)) 
   { 
      document.AutoDeleteAttachmentsFromCache = true; 
             
      Console.WriteLine($"Document has {document.Pages.Count} pages"); 
 
      // Find out if the document is portfolio 
      bool isPortfolio = 
         document.Metadata.ContainsKey(LEADDocument.MetadataKey_IsPortfolio) && 
         bool.Parse(document.Metadata[LEADDocument.MetadataKey_IsPortfolio]); 
      Console.WriteLine($"IsPortfolio:{isPortfolio}"); 
 
      // Check that everything is as expected 
 
      // We may have 0 or more attachments depending on the file 
      Console.WriteLine($"Document has {document.Attachments.Count} attachments"); 
 
      // We should have one or more pages unless this is PDF portfolio, then it will have 0 pages 
      if (isPortfolio) 
      { 
         Debug.Assert(document.Pages.Count == 0); 
      } 
 
      // Extract all the attachments into the output directory 
      foreach (DocumentAttachment attachment in document.Attachments) 
      { 
         // Show info on this attachment 
         Console.WriteLine($"Attachment number {attachment.AttachmentNumber} file name is '{attachment.FileName}', " + 
            $"length is {attachment.FileLength} bytes, and ID is {attachment.DocumentId}, Is Embedded: {attachment.IsEmbedded}"); 
         // Get the attachment file name 
         // This value may not be unique, so combine it with the attachment number 
         string attachmentFileName = $"{attachment.AttachmentNumber}-{attachment.FileName}"; 
         attachmentFileName = Path.Combine(outputDirectory, attachmentFileName); 
 
         // Get the attachment as a stream, we do not want to save it to the cache so pass null for that 
         // DocumentAttachments reference 
         using (Stream attachmentStream = document.Attachments.CreateAttachmentStream(attachment.AttachmentNumber, null)) 
         { 
            if (attachmentStream != null) 
            { 
               // Save it to the output file 
               using (var stream = File.Create(attachmentFileName)) 
               { 
                  document.SaveAttachmentToCache(null); 
                  attachmentStream.CopyTo(stream); 
               } 
            } 
         } 
 
         var saveOptions = new SaveAttachmentToCacheOptions(); 
         saveOptions.AttachmentNumber = 0; 
         saveOptions.UpdateAttachmentDocumentId = true; 
         saveOptions.UploadDocumentOptions = new UploadDocumentOptions(); 
         document.SaveAttachmentToCache(saveOptions); 
 
         // Or load this attachment as a LEADDocument, this might fail if the attachment is not of a valid image 
         // or document format 
         try 
         { 
            Console.WriteLine("Loading as LEADDocument"); 
            var loadAttachmentOptions = new LoadAttachmentOptions(); 
            loadAttachmentOptions.AttachmentNumber = attachment.AttachmentNumber; 
            loadAttachmentOptions.UpdateAttachmentDocumentId = true; 
            using (LEADDocument attachmentDocument = document.LoadDocumentAttachment(loadAttachmentOptions)) 
            { 
               Console.WriteLine($"Success, attachment document mime type is {attachmentDocument.MimeType} and has {attachmentDocument.Pages.Count} pages"); 
            } 
         } 
         catch (Exception ex) 
         { 
            Console.WriteLine($"Error {ex.Message}"); 
         } 
      } 
   } 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images"; 
} 
 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.net.MalformedURLException; 
import java.net.URI; 
import java.net.URISyntaxException; 
import java.net.URL; 
import java.nio.file.Files; 
import java.nio.file.Paths; 
import java.util.ArrayList; 
import java.util.Calendar; 
import java.util.List; 
import java.util.concurrent.Callable; 
import java.util.concurrent.ExecutorService; 
import java.util.concurrent.Executors; 
import java.util.concurrent.Future; 
import java.util.regex.Pattern; 
 
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.annotations.engine.*; 
import leadtools.barcode.*; 
import leadtools.caching.*; 
import leadtools.codecs.*; 
import leadtools.document.*; 
import leadtools.document.DocumentMimeTypes.UserGetDocumentStatusHandler; 
import leadtools.document.converter.*; 
import leadtools.document.writer.*; 
import leadtools.ocr.*; 
 
 
public void attachmentsExample() throws IOException { 
 
   final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images"; 
   // The PDF document can be one of the following: 
   // 1. Normal PDF without attachments. Has one or more pages and no attachments. 
   // 2. Normal PDF with attachments. Has one or more pages and one or more 
   // attachments. 
   // 3. PDF Portfolio. Has a placeholder page and one or more attachments. 
 
   // The value of LoadDocumentOptions.LoadAttachmentsMode affects loading the 
   // documents as follows: 
 
   // 1. Normal PDF without attachments: Will not be affected. 
   // 2. Normal PDF with attachments: Number of pages is not affected. 
   // Number of attachments is: 
   // LoadAttachmentsMode = None then 0 
   // LoadAttachmentsMode = AsAttachments then number of attachments found in the 
   // document. 
   // 3. PDF Portfolio: Number of pages is: 
   // LoadAttachmentsMode = None then 1 (the placeholder page) 
   // LoadAttachmentsMode = AsAttachments then 0 (the document has no pages). 
   // Number of attachments is: 
   // LoadAttachmentsMode = None then 0 
   // LoadAttachmentsMode = AsAttachments then number of attachments found in the 
   // document. 
 
   // First, load the document without attachments 
   String pdfFile = combine(LEAD_VARS_IMAGES_DIR, "Leadtools.pdf"); 
   String outputDirectory = LEAD_VARS_IMAGES_DIR; 
 
   System.out.println("Loading with DocumentLoadAttachmentsMode.None"); 
   LoadDocumentOptions loadDocumentOptions = new LoadDocumentOptions(); 
   loadDocumentOptions.setLoadAttachmentsMode(DocumentLoadAttachmentsMode.NONE); 
   LEADDocument document = DocumentFactory.loadFromFile(pdfFile, loadDocumentOptions); 
 
   System.out.println(document.getDocumentFileName()); 
   System.out.println("Document has " + document.getPages().size() + " pages"); 
 
   // Find out if the document is portfolio 
   boolean isPortfolio = document.getMetadata().containsKey(LEADDocument.METADATA_KEY_ISPORTFOLIO) && 
         Boolean.getBoolean(document.getMetadata().get(LEADDocument.METADATA_KEY_ISPORTFOLIO)); 
   System.out.println("IsPortfolio:" + isPortfolio); 
 
   // Check that everything is as expected 
 
   // We should have 0 attachments since we did not instruct the toolkit to load 
   // these 
   assertTrue(document.getAttachments().size() == 0); 
 
   // We should have one or more pages regardless of whether the document is PDF 
   // portfolio 
   assertTrue(document.getPages().size() > 0); 
 
   // Next, load the document with attachments 
   System.out.println("Loading with DocumentLoadAttachmentsMode.AsAttachments"); 
   loadDocumentOptions.setLoadAttachmentsMode(DocumentLoadAttachmentsMode.AS_ATTACHMENTS); 
   document = DocumentFactory.loadFromFile(pdfFile, loadDocumentOptions); 
 
   document.setAutoDeleteAttachmentsFromCache(true); 
 
   System.out.println("Document has " + document.getPages().size() + " pages"); 
 
   // Find out if the document is portfolio 
   isPortfolio = document.getMetadata().containsKey(LEADDocument.METADATA_KEY_ISPORTFOLIO) && 
         Boolean.getBoolean(document.getMetadata().get(LEADDocument.METADATA_KEY_ISPORTFOLIO)); 
   System.out.println("IsPortfolio:" + isPortfolio); 
 
   // Check that everything is as expected 
 
   // We may have 0 or more attachments depending on the file 
   System.out.println("Document has " + document.getAttachments().size() + " attachments"); 
 
   // We should have one or more pages unless this is PDF portfolio, then it will 
   // have 0 pages 
   if (isPortfolio) { 
      assertTrue(document.getPages().size() == 0); 
   } 
 
   // Extract all the attachments into the output directory 
   for (DocumentAttachment attachment : document.getAttachments()) { 
      // Show info on this attachment 
      System.out.println("Attachment number " + attachment.getAttachmentNumber() + " file name is '" 
            + attachment.getFileName() + "', " + 
            "length is " + attachment.getFileLength() + " bytes, and ID is " + attachment.getDocumentId() 
            + ", Is Embedded: " + attachment.isEmbedded()); 
      // Get the attachment file name 
      // This value may not be unique, so combine it with the attachment number 
      String attachmentFileName = attachment.getAttachmentNumber() + "-" + attachment.getFileName(); 
      attachmentFileName = combine(outputDirectory, attachmentFileName); 
 
      // Get the attachment as a stream, we do not want to save it to the cache so 
      // pass null for that 
      // DocumentAttachments reference 
      ILeadStream attachmentStream = document.getAttachments() 
            .createAttachmentStream(attachment.getAttachmentNumber(), null); 
      if (attachmentStream != null) { 
         document.saveAttachmentToCache(null); 
         // Save it to the output file 
         FileOutputStream fileStream = new FileOutputStream(attachmentFileName); 
         //////////////////////////////////////////////////////////// fileStream.write(attachmentStream.toInputStream().readAllBytes()); 
         fileStream.close(); 
      } 
      attachmentStream.dispose(); 
 
      SaveAttachmentToCacheOptions saveOptions = new SaveAttachmentToCacheOptions(); 
      saveOptions.setAttachmentNumber(0); 
      saveOptions.setUpdateAttachmentDocumentId(true); 
      saveOptions.setUploadDocumentOptions(new UploadDocumentOptions()); 
      document.saveAttachmentToCache(saveOptions); 
 
      // Or load this attachment as a LEADDocument, this might fail if the attachment 
      // is not of a valid image 
      // or document format 
      try { 
         System.out.println("Loading as LEADDocument"); 
         LoadAttachmentOptions loadAttachmentOptions = new LoadAttachmentOptions(); 
         loadAttachmentOptions.setAttachmentNumber(attachment.getAttachmentNumber()); 
         loadAttachmentOptions.setUpdateAttachmentDocumentId(true); 
         LEADDocument attachmentDocument = document.loadDocumentAttachment(loadAttachmentOptions); 
 
         System.out.println("Success, attachment document mime type is " + attachmentDocument.getMimeType() 
               + " and has " + attachmentDocument.getPages().size() + " pages"); 
      } catch (Exception ex) { 
         System.out.println("Error " + ex.getMessage()); 
      } 
 
   } 
} 
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.Document Assembly
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.