ID of this attachment document.
public string DocumentId {get; set;} Public Property DocumentId() As StringGetSet
public:property String^ DocumentId{String^ get()void set(String^ value)}
ID of this attachment document. The default value is null.
IsEmbedded is set to true when embedded attachments are read from a document and the value of DocumentId will be null.
Application programs adding external attachments should set the value of IsEmbedded to false and set DocumentId to the ID of the document representing the attachment.
For more information, refer to Document Attachments.
For an example of using embedded attachments, refer to DocumentAttachment.
This example will add TIF, PDF, and JPEG documents as attachments to a LEADDocument.
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 ExternalAttachmentsExample(){// The cache we are usingvar cache = new FileCache();cache.PolicySerializationMode = CacheSerializationMode.Json;cache.DataSerializationMode = CacheSerializationMode.Json;cache.CacheDirectory = @"c:\cache-dir";// The main document, its a PDFstring mainDocumentFile = @"C:\LEADTOOLS21\Resources\Images\leadtools.pdf";// Attachments we will be adding: a TIF, another PDF, and a JPG filestring[] attachmentFiles ={@"C:\LEADTOOLS21\Resources\Images\ocr1.tif",@"C:\LEADTOOLS21\Resources\Images\employee benefits survey.pdf",@"C:\LEADTOOLS21\Resources\Images\cannon.jpg"};// Load the main documentstring mainDocumentId;var loadDocumentOptions = new LoadDocumentOptions();loadDocumentOptions.Cache = cache;using (LEADDocument mainDocument = DocumentFactory.LoadFromFile(mainDocumentFile, loadDocumentOptions)){// Save the document ID so we can load it from cache latermainDocumentId = mainDocument.DocumentId;// Show this document, it should have no attachments at this pointShowDocumentWithAttachments("Initial", mainDocument, 0);mainDocument.IsReadOnly = false;// Now, load each attachment document and add it to the main documentforeach (string attachmentFile in attachmentFiles){int attachmentNumber = 1;using (LEADDocument attachmentDocument = DocumentFactory.LoadFromFile(attachmentFile, loadDocumentOptions)){// Save this document to the cacheattachmentDocument.AutoDeleteFromCache = false;attachmentDocument.SaveToCache();// Add it to the main document as an attachmentvar attachment = new DocumentAttachment();// The attachment number.attachment.AttachmentNumber = attachmentNumber;// This is an external attachmentattachment.IsEmbedded = false;// And the document IDattachment.DocumentId = attachmentDocument.DocumentId;// The rest of the properties are optional but setting them might be helpful to an application user interfaceattachment.FileName = attachmentDocument.Name;attachment.DisplayName = attachmentDocument.Name;attachment.FileLength = attachmentDocument.FileLength;attachment.MimeType = attachmentDocument.MimeType;mainDocument.Attachments.Add(attachment);attachmentNumber++;}}mainDocument.IsReadOnly = true;// Now, show the document info again, it should have 3 attachmentsShowDocumentWithAttachments("After adding attachments", mainDocument, attachmentFiles.Length);// Ensure that attachments will be deleted when the owner document is removedmainDocument.AutoDeleteAttachmentsFromCache = true;// Save this document to the cachemainDocument.AutoDeleteFromCache = false;mainDocument.SaveToCache();}// Re-load the main document from the cachevar loadFromCacheOptions = new LoadFromCacheOptions();loadFromCacheOptions.Cache = cache;loadFromCacheOptions.DocumentId = mainDocumentId;using (LEADDocument mainDocument = DocumentFactory.LoadFromCache(loadFromCacheOptions)){// Show this document, it should have the attachments we savedShowDocumentWithAttachments("Loaded from cache", mainDocument, attachmentFiles.Length);}// Another way of loading this attachment is with DocumentFactory.LoadDocumentAttachment using the attachment number.// This method can be used to load both external and embedded attachments using the owner document ID and the attachment number.Console.WriteLine("Using LoadDocumentAttachments:");for (var attachmentNumber = 1; attachmentNumber <= attachmentFiles.Length; attachmentNumber++){// For loading the main documentloadFromCacheOptions = new LoadFromCacheOptions();loadFromCacheOptions.Cache = cache;loadFromCacheOptions.DocumentId = mainDocumentId;// For loading the attachmentvar loadAttachmentOptions = new LoadAttachmentOptions();loadAttachmentOptions.AttachmentNumber = attachmentNumber;using (LEADDocument attachmentDocument = DocumentFactory.LoadDocumentAttachment(loadFromCacheOptions, loadAttachmentOptions)){Console.WriteLine(" " + GetDocumentInfo(attachmentDocument));}}// Clean up by deleting the original document from the cache. This will cause all attachment documents to be deleted as well// Since we set AutoDeleteAttachmentsFromCache to true earlierloadFromCacheOptions = new LoadFromCacheOptions();loadFromCacheOptions.Cache = cache;loadFromCacheOptions.DocumentId = mainDocumentId;DocumentFactory.DeleteFromCache(loadFromCacheOptions);}private static void ShowDocumentWithAttachments(string message, LEADDocument document, int attachmentCount){// Show info about this document and any attachmentsConsole.WriteLine(message);Console.WriteLine(" " + GetDocumentInfo(document));// Sanity checkDebug.Assert(attachmentCount == document.Attachments.Count);if (document.Attachments.Count > 0){Console.WriteLine(" Attachments:");// Show the attachmentsforeach (DocumentAttachment attachment in document.Attachments){// Load the attachment as a LEADDocument using the document ID// This method works for external attachments onlyvar loadFromCacheOptions = new LoadFromCacheOptions();loadFromCacheOptions.Cache = document.GetCache();loadFromCacheOptions.DocumentId = attachment.DocumentId;using (LEADDocument attachmentDocument = DocumentFactory.LoadFromCache(loadFromCacheOptions)){Console.WriteLine(" " + GetDocumentInfo(attachmentDocument));}}}}private static string GetDocumentInfo(LEADDocument document){return $"DocumentId:{document.DocumentId} MimeType:{document.MimeType} Pages:{document.Pages.Count} Attachments:{document.Attachments.Count}";}
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 ExternalAttachmentsExample()' The cache we are usingDim cache As New FileCache()cache.PolicySerializationMode = CacheSerializationMode.Jsoncache.DataSerializationMode = CacheSerializationMode.Jsoncache.CacheDirectory = "c:\cache-dir"' The main document is a PDFDim mainDocumentFile As String = "C:\LEADTOOLS21\Resources\Images\leadtools.pdf"' Attachments we will be adding: a TIF, another PDF, and a JPG fileDim attachmentFiles() As String ={"C:\LEADTOOLS21\Resources\Images\ocr1.tif","C:\LEADTOOLS21\Resources\Images\employee benefits survey.pdf","C:\LEADTOOLS21\Resources\Images\cannon.jpg"}' Load the main documentDim mainDocumentId As StringDim loadDocumentOptions As New LoadDocumentOptions()loadDocumentOptions.Cache = cacheUsing mainDocument As LEADDocument = DocumentFactory.LoadFromFile(mainDocumentFile, loadDocumentOptions)' Save the document ID so we can load it from cache latermainDocumentId = mainDocument.DocumentId' Show this document, it should have no attachments at this pointShowDocumentWithAttachments("Initial", mainDocument, 0)mainDocument.IsReadOnly = False' Now, load each attachment document and add it to the main documentFor Each attachmentFile As String In attachmentFilesDim attachmentNumber As Integer = 1Using attachmentDocument As LEADDocument = DocumentFactory.LoadFromFile(attachmentFile, loadDocumentOptions)' Save this document to the cacheattachmentDocument.AutoDeleteFromCache = FalseattachmentDocument.SaveToCache()' Add it to the main document as an attachmentDim attachment As New DocumentAttachment()' The attachment number.attachment.AttachmentNumber = attachmentNumber' This Is an external attachmentattachment.IsEmbedded = False' And the document IDattachment.DocumentId = attachmentDocument.DocumentId' The rest of the properties are optional but setting them might be helpful to an application user interfaceattachment.FileName = attachmentDocument.Nameattachment.DisplayName = attachmentDocument.Nameattachment.FileLength = attachmentDocument.FileLengthattachment.MimeType = attachmentDocument.MimeTypemainDocument.Attachments.Add(attachment)attachmentNumber = attachmentNumber + 1End UsingNextmainDocument.IsReadOnly = True' Now, show the document info again, it should have 3 attachmentsShowDocumentWithAttachments("After adding attachments", mainDocument, attachmentFiles.Length)' Ensure that attachments will be deleted when the owner document Is removedmainDocument.AutoDeleteAttachmentsFromCache = True' Save this document to the cachemainDocument.AutoDeleteFromCache = FalsemainDocument.SaveToCache()End Using' Re-load the main document from the cacheDim loadFromCacheOptions As New LoadFromCacheOptions()loadFromCacheOptions.Cache = cacheloadFromCacheOptions.DocumentId = mainDocumentIdUsing mainDocument As LEADDocument = DocumentFactory.LoadFromCache(loadFromCacheOptions)' Show this document, it should have the attachments we savedShowDocumentWithAttachments("Loaded from cache", mainDocument, attachmentFiles.Length)End Using' Another way of loading this attachment Is with DocumentFactory.LoadDocumentAttachment using the attachment number.' This method can be used to load both external And embedded attachments using the owner document ID And the attachment number.Console.WriteLine("Using LoadDocumentAttachments:")For attachmentNumber As Integer = 1 To attachmentFiles.Length' For loading the main documentloadFromCacheOptions = New LoadFromCacheOptions()loadFromCacheOptions.Cache = cacheloadFromCacheOptions.DocumentId = mainDocumentId' For loading the attachmentDim loadAttachmentOptions As New LoadAttachmentOptions()loadAttachmentOptions.AttachmentNumber = attachmentNumberUsing attachmentDocument As LEADDocument = DocumentFactory.LoadDocumentAttachment(loadFromCacheOptions, loadAttachmentOptions)Console.WriteLine(" " + GetDocumentInfo(attachmentDocument))End UsingNext' Clean up by deleting the original document from the cache. This will cause all attachment documents to be deleted as well' Since we set AutoDeleteAttachmentsFromCache to true earlierloadFromCacheOptions = New LoadFromCacheOptions()loadFromCacheOptions.Cache = cacheloadFromCacheOptions.DocumentId = mainDocumentIdDocumentFactory.DeleteFromCache(loadFromCacheOptions)End SubPrivate Shared Sub ShowDocumentWithAttachments(message As String, document As LEADDocument, attachmentCount As Integer)' Show info about this document And any attachmentsConsole.WriteLine(message)Console.WriteLine(" " + GetDocumentInfo(document))' Sanity checkDebug.Assert(attachmentCount = document.Attachments.Count)If document.Attachments.Count > 0 ThenConsole.WriteLine(" Attachments:")' Show the attachmentsFor Each attachment As DocumentAttachment In document.Attachments' Load the attachment as a LEADDocument using the document ID' This method works for external attachments onlyDim loadFromCacheOptions As New LoadFromCacheOptions()loadFromCacheOptions.Cache = document.GetCache()loadFromCacheOptions.DocumentId = attachment.DocumentIdUsing attachmentDocument As LEADDocument = DocumentFactory.LoadFromCache(loadFromCacheOptions)Console.WriteLine(" " + GetDocumentInfo(attachmentDocument))End UsingNextEnd IfEnd SubPrivate Shared Function GetDocumentInfo(document As LEADDocument) As StringReturn $"DocumentId:{document.DocumentId} MimeType:{document.MimeType} Pages:{document.Pages.Count} Attachments:{document.Attachments.Count}"End FunctionPublic Shared Function GetCache() As ObjectCache' Create a LEADTOOLS FileCache objectDim cacheDir As String = Path.Combine(ImagesPath.Path, "cache")If Directory.Exists(cacheDir) ThenDirectory.Delete(cacheDir, True)End IfDirectory.CreateDirectory(cacheDir)Dim cache As New FileCache()cache.CacheDirectory = cacheDirReturn cacheEnd Function
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
