←Select platform

DocumentAttachment Class

Summary

Properties of an attachment in a LEADDocument.

Syntax
C#
VB
C++
[SerializableAttribute()] 
[DataContractAttribute()] 
public class DocumentAttachment 
<SerializableAttribute(),  
 DataContractAttribute()>  
Public Class DocumentAttachment 
public: 
   [SerializableAttribute,  
   DataContractAttribute] 
   ref class DocumentAttachment 
Remarks

The Document library supports reading embedded attachments from documents such as PDF, as well as external attachments that are added manually.

For more information, refer to Document Attachments.

Example

This example will extract the attachments embedded in a PDF file.

C#
VB
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(string pdfFile, string outputDirectory) 
{ 
	// 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 
	Console.WriteLine($"Loading with DocumentLoadAttachmentsMode.None"); 
	var loadDocumentOptions = new LoadDocumentOptions(); 
	loadDocumentOptions.LoadAttachmentsMode = DocumentLoadAttachmentsMode.None; 
	using (LEADDocument document = DocumentFactory.LoadFromFile(pdfFile, loadDocumentOptions)) 
	{ 
		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)) 
	{ 
		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}' and length is {attachment.FileLength} bytes"); 
			// 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 
			using (Stream attachmentStream = document.Attachments.CreateAttachmentStream(attachment.AttachmentNumber, null)) 
			{ 
				if (attachmentStream != null) 
				{ 
					// Save it to the output file 
					using (var stream = File.Create(attachmentFileName)) 
					{ 
						attachmentStream.CopyTo(stream); 
					} 
				} 
			} 
 
			// 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; 
				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}"); 
			} 
		} 
	} 
} 
Imports Leadtools 
Imports Leadtools.Codecs 
Imports Leadtools.Document.Writer 
Imports Leadtools.Svg 
Imports Leadtools.Document 
Imports Leadtools.Caching 
Imports Leadtools.Annotations.Engine 
Imports Leadtools.Barcode 
Imports Leadtools.Ocr 
Imports LeadtoolsDocumentExamples.LeadtoolsExamples.Common 
Imports Leadtools.Document.Converter 
 
Public Shared Sub AttachmentsExample(pdfFile As String, outputDirectory As String) 
   ' 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 
   Console.WriteLine($"Loading with DocumentLoadAttachmentsMode.None") 
   Dim loadDocumentOptions As New LoadDocumentOptions() 
   loadDocumentOptions.LoadAttachmentsMode = DocumentLoadAttachmentsMode.None 
   Using document As LEADDocument = DocumentFactory.LoadFromFile(pdfFile, loadDocumentOptions) 
      Console.WriteLine($"Document has {document.Pages.Count} pages") 
 
      ' Find out if the document is portfolio 
      Dim isPortfolio As Boolean = 
         document.Metadata.ContainsKey(LEADDocument.MetadataKey_IsPortfolio) AndAlso 
         Boolean.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) 
   End Using 
 
   ' Next, load the document with attachments 
   Console.WriteLine($"Loading with DocumentLoadAttachmentsMode.AsAttachments") 
   loadDocumentOptions.LoadAttachmentsMode = DocumentLoadAttachmentsMode.AsAttachments 
   Using document As LEADDocument = DocumentFactory.LoadFromFile(pdfFile, loadDocumentOptions) 
      Console.WriteLine($"Document has {document.Pages.Count} pages") 
 
      ' Find out if the document is portfolio 
      Dim isPortfolio As Boolean = 
         document.Metadata.ContainsKey(LEADDocument.MetadataKey_IsPortfolio) AndAlso 
         Boolean.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 Then 
         Debug.Assert(document.Pages.Count = 0) 
      End If 
 
      ' Extract all the attachments into the output directory 
      For Each attachment As DocumentAttachment In document.Attachments 
         ' Show info on this attachment 
         Console.WriteLine($"Attachment number {attachment.AttachmentNumber} file name is '{attachment.FileName}' and length is {attachment.FileLength} bytes") 
         ' Get the attachment file name 
         ' This value may not be unique, so combine it with the attachment number 
         Dim attachmentFileName As String = $"{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 
         Using attachmentStream As Stream = document.Attachments.CreateAttachmentStream(attachment.AttachmentNumber, Nothing) 
            If Not IsNothing(attachmentStream) Then 
               ' Save it to the output file 
               Using stream As Stream = File.Create(attachmentFileName) 
                  attachmentStream.CopyTo(stream) 
               End Using 
            End If 
         End Using 
 
         ' 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") 
            Dim loadAttachmentOptions As New LoadAttachmentOptions() 
            loadAttachmentOptions.AttachmentNumber = attachment.AttachmentNumber 
            Using attachmentDocument As LEADDocument = document.LoadDocumentAttachment(loadAttachmentOptions) 
               Console.WriteLine($"Success, attachment document mime type is {attachmentDocument.MimeType} and has {attachmentDocument.Pages.Count} pages") 
            End Using 
         Catch ex As Exception 
            Console.WriteLine($"Error {ex.Message}") 
         End Try 
      Next 
   End Using 
End Sub 
Requirements
Target Platforms
Help Version 21.0.2021.7.2
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.

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