Properties of an attachment in a LEADDocument.
[SerializableAttribute()][DataContractAttribute()]public class DocumentAttachment
<SerializableAttribute(),DataContractAttribute()>Public Class DocumentAttachment
public:[SerializableAttribute,DataContractAttribute]ref class DocumentAttachment
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.
This example will extract the attachments embedded in a PDF file.
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 attachmentsConsole.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 portfoliobool 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 theseDebug.Assert(document.Attachments.Count == 0);// We should have one or more pages regardless of whether the document is PDF portfolioDebug.Assert(document.Pages.Count > 0);}// Next, load the document with attachmentsConsole.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 portfoliobool 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 fileConsole.WriteLine($"Document has {document.Attachments.Count} attachments");// We should have one or more pages unless this is PDF portfolio, then it will have 0 pagesif (isPortfolio){Debug.Assert(document.Pages.Count == 0);}// Extract all the attachments into the output directoryforeach (DocumentAttachment attachment in document.Attachments){// Show info on this attachmentConsole.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 numberstring 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 thatusing (Stream attachmentStream = document.Attachments.CreateAttachmentStream(attachment.AttachmentNumber, null)){if (attachmentStream != null){// Save it to the output fileusing (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 formattry{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 LeadtoolsImports Leadtools.CodecsImports Leadtools.Document.WriterImports Leadtools.SvgImports Leadtools.DocumentImports Leadtools.CachingImports Leadtools.Annotations.EngineImports Leadtools.BarcodeImports Leadtools.OcrImports LeadtoolsDocumentExamples.LeadtoolsExamples.CommonImports Leadtools.Document.ConverterPublic 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 attachmentsConsole.WriteLine($"Loading with DocumentLoadAttachmentsMode.None")Dim loadDocumentOptions As New LoadDocumentOptions()loadDocumentOptions.LoadAttachmentsMode = DocumentLoadAttachmentsMode.NoneUsing document As LEADDocument = DocumentFactory.LoadFromFile(pdfFile, loadDocumentOptions)Console.WriteLine($"Document has {document.Pages.Count} pages")' Find out if the document is portfolioDim isPortfolio As Boolean =document.Metadata.ContainsKey(LEADDocument.MetadataKey_IsPortfolio) AndAlsoBoolean.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 theseDebug.Assert(document.Attachments.Count = 0)' We should have one or more pages regardless of whether the document is PDF portfolioDebug.Assert(document.Pages.Count > 0)End Using' Next, load the document with attachmentsConsole.WriteLine($"Loading with DocumentLoadAttachmentsMode.AsAttachments")loadDocumentOptions.LoadAttachmentsMode = DocumentLoadAttachmentsMode.AsAttachmentsUsing document As LEADDocument = DocumentFactory.LoadFromFile(pdfFile, loadDocumentOptions)Console.WriteLine($"Document has {document.Pages.Count} pages")' Find out if the document is portfolioDim isPortfolio As Boolean =document.Metadata.ContainsKey(LEADDocument.MetadataKey_IsPortfolio) AndAlsoBoolean.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 fileConsole.WriteLine($"Document has {document.Attachments.Count} attachments")' We should have one or more pages unless this is PDF portfolio, then it will have 0 pagesIf isPortfolio ThenDebug.Assert(document.Pages.Count = 0)End If' Extract all the attachments into the output directoryFor Each attachment As DocumentAttachment In document.Attachments' Show info on this attachmentConsole.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 numberDim 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 thatUsing attachmentStream As Stream = document.Attachments.CreateAttachmentStream(attachment.AttachmentNumber, Nothing)If Not IsNothing(attachmentStream) Then' Save it to the output fileUsing stream As Stream = File.Create(attachmentFileName)attachmentStream.CopyTo(stream)End UsingEnd IfEnd Using' Or load this attachment as a LEADDocument, this might fail if the attachment is not of a valid image' or document formatTryConsole.WriteLine("Loading as LEADDocument")Dim loadAttachmentOptions As New LoadAttachmentOptions()loadAttachmentOptions.AttachmentNumber = attachment.AttachmentNumberUsing attachmentDocument As LEADDocument = document.LoadDocumentAttachment(loadAttachmentOptions)Console.WriteLine($"Success, attachment document mime type is {attachmentDocument.MimeType} and has {attachmentDocument.Pages.Count} pages")End UsingCatch ex As ExceptionConsole.WriteLine($"Error {ex.Message}")End TryNextEnd UsingEnd Sub
Help Collections
Raster .NET | C API | C++ Class Library | HTML5 JavaScript
Document .NET | C API | C++ Class Library | HTML5 JavaScript
Medical .NET | C API | C++ Class Library | HTML5 JavaScript
Medical Web Viewer .NET
Multimedia
Direct Show .NET | C API | Filters
Media Foundation .NET | C API | Transforms
Supported Platforms
.NET, Java, Android, and iOS/macOS Assemblies
Imaging, Medical, and Document
C API/C++ Class Libraries
Imaging, Medical, and Document
HTML5 JavaScript Libraries
Imaging, Medical, and Document
