ID of this attachment document.
public string DocumentId {get; set;}
Public Property DocumentId() As String
Get
Set
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 using
var cache = new FileCache();
cache.PolicySerializationMode = CacheSerializationMode.Json;
cache.DataSerializationMode = CacheSerializationMode.Json;
cache.CacheDirectory = @"c:\cache-dir";
// The main document, its a PDF
string mainDocumentFile = @"C:\LEADTOOLS21\Resources\Images\leadtools.pdf";
// Attachments we will be adding: a TIF, another PDF, and a JPG file
string[] attachmentFiles =
{
@"C:\LEADTOOLS21\Resources\Images\ocr1.tif",
@"C:\LEADTOOLS21\Resources\Images\employee benefits survey.pdf",
@"C:\LEADTOOLS21\Resources\Images\cannon.jpg"
};
// Load the main document
string 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 later
mainDocumentId = mainDocument.DocumentId;
// Show this document, it should have no attachments at this point
ShowDocumentWithAttachments("Initial", mainDocument, 0);
mainDocument.IsReadOnly = false;
// Now, load each attachment document and add it to the main document
foreach (string attachmentFile in attachmentFiles)
{
int attachmentNumber = 1;
using (LEADDocument attachmentDocument = DocumentFactory.LoadFromFile(attachmentFile, loadDocumentOptions))
{
// Save this document to the cache
attachmentDocument.AutoDeleteFromCache = false;
attachmentDocument.SaveToCache();
// Add it to the main document as an attachment
var attachment = new DocumentAttachment();
// The attachment number.
attachment.AttachmentNumber = attachmentNumber;
// This is an external attachment
attachment.IsEmbedded = false;
// And the document ID
attachment.DocumentId = attachmentDocument.DocumentId;
// The rest of the properties are optional but setting them might be helpful to an application user interface
attachment.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 attachments
ShowDocumentWithAttachments("After adding attachments", mainDocument, attachmentFiles.Length);
// Ensure that attachments will be deleted when the owner document is removed
mainDocument.AutoDeleteAttachmentsFromCache = true;
// Save this document to the cache
mainDocument.AutoDeleteFromCache = false;
mainDocument.SaveToCache();
}
// Re-load the main document from the cache
var loadFromCacheOptions = new LoadFromCacheOptions();
loadFromCacheOptions.Cache = cache;
loadFromCacheOptions.DocumentId = mainDocumentId;
using (LEADDocument mainDocument = DocumentFactory.LoadFromCache(loadFromCacheOptions))
{
// Show this document, it should have the attachments we saved
ShowDocumentWithAttachments("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 document
loadFromCacheOptions = new LoadFromCacheOptions();
loadFromCacheOptions.Cache = cache;
loadFromCacheOptions.DocumentId = mainDocumentId;
// For loading the attachment
var 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 earlier
loadFromCacheOptions = 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 attachments
Console.WriteLine(message);
Console.WriteLine(" " + GetDocumentInfo(document));
// Sanity check
Debug.Assert(attachmentCount == document.Attachments.Count);
if (document.Attachments.Count > 0)
{
Console.WriteLine(" Attachments:");
// Show the attachments
foreach (DocumentAttachment attachment in document.Attachments)
{
// Load the attachment as a LEADDocument using the document ID
// This method works for external attachments only
var 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 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 ExternalAttachmentsExample()
' The cache we are using
Dim cache As New FileCache()
cache.PolicySerializationMode = CacheSerializationMode.Json
cache.DataSerializationMode = CacheSerializationMode.Json
cache.CacheDirectory = "c:\cache-dir"
' The main document is a PDF
Dim mainDocumentFile As String = "C:\LEADTOOLS21\Resources\Images\leadtools.pdf"
' Attachments we will be adding: a TIF, another PDF, and a JPG file
Dim 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 document
Dim mainDocumentId As String
Dim loadDocumentOptions As New LoadDocumentOptions()
loadDocumentOptions.Cache = cache
Using mainDocument As LEADDocument = DocumentFactory.LoadFromFile(mainDocumentFile, loadDocumentOptions)
' Save the document ID so we can load it from cache later
mainDocumentId = mainDocument.DocumentId
' Show this document, it should have no attachments at this point
ShowDocumentWithAttachments("Initial", mainDocument, 0)
mainDocument.IsReadOnly = False
' Now, load each attachment document and add it to the main document
For Each attachmentFile As String In attachmentFiles
Dim attachmentNumber As Integer = 1
Using attachmentDocument As LEADDocument = DocumentFactory.LoadFromFile(attachmentFile, loadDocumentOptions)
' Save this document to the cache
attachmentDocument.AutoDeleteFromCache = False
attachmentDocument.SaveToCache()
' Add it to the main document as an attachment
Dim attachment As New DocumentAttachment()
' The attachment number.
attachment.AttachmentNumber = attachmentNumber
' This Is an external attachment
attachment.IsEmbedded = False
' And the document ID
attachment.DocumentId = attachmentDocument.DocumentId
' The rest of the properties are optional but setting them might be helpful to an application user interface
attachment.FileName = attachmentDocument.Name
attachment.DisplayName = attachmentDocument.Name
attachment.FileLength = attachmentDocument.FileLength
attachment.MimeType = attachmentDocument.MimeType
mainDocument.Attachments.Add(attachment)
attachmentNumber = attachmentNumber + 1
End Using
Next
mainDocument.IsReadOnly = True
' Now, show the document info again, it should have 3 attachments
ShowDocumentWithAttachments("After adding attachments", mainDocument, attachmentFiles.Length)
' Ensure that attachments will be deleted when the owner document Is removed
mainDocument.AutoDeleteAttachmentsFromCache = True
' Save this document to the cache
mainDocument.AutoDeleteFromCache = False
mainDocument.SaveToCache()
End Using
' Re-load the main document from the cache
Dim loadFromCacheOptions As New LoadFromCacheOptions()
loadFromCacheOptions.Cache = cache
loadFromCacheOptions.DocumentId = mainDocumentId
Using mainDocument As LEADDocument = DocumentFactory.LoadFromCache(loadFromCacheOptions)
' Show this document, it should have the attachments we saved
ShowDocumentWithAttachments("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 document
loadFromCacheOptions = New LoadFromCacheOptions()
loadFromCacheOptions.Cache = cache
loadFromCacheOptions.DocumentId = mainDocumentId
' For loading the attachment
Dim loadAttachmentOptions As New LoadAttachmentOptions()
loadAttachmentOptions.AttachmentNumber = attachmentNumber
Using attachmentDocument As LEADDocument = DocumentFactory.LoadDocumentAttachment(loadFromCacheOptions, loadAttachmentOptions)
Console.WriteLine(" " + GetDocumentInfo(attachmentDocument))
End Using
Next
' 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 earlier
loadFromCacheOptions = New LoadFromCacheOptions()
loadFromCacheOptions.Cache = cache
loadFromCacheOptions.DocumentId = mainDocumentId
DocumentFactory.DeleteFromCache(loadFromCacheOptions)
End Sub
Private Shared Sub ShowDocumentWithAttachments(message As String, document As LEADDocument, attachmentCount As Integer)
' Show info about this document And any attachments
Console.WriteLine(message)
Console.WriteLine(" " + GetDocumentInfo(document))
' Sanity check
Debug.Assert(attachmentCount = document.Attachments.Count)
If document.Attachments.Count > 0 Then
Console.WriteLine(" Attachments:")
' Show the attachments
For Each attachment As DocumentAttachment In document.Attachments
' Load the attachment as a LEADDocument using the document ID
' This method works for external attachments only
Dim loadFromCacheOptions As New LoadFromCacheOptions()
loadFromCacheOptions.Cache = document.GetCache()
loadFromCacheOptions.DocumentId = attachment.DocumentId
Using attachmentDocument As LEADDocument = DocumentFactory.LoadFromCache(loadFromCacheOptions)
Console.WriteLine(" " + GetDocumentInfo(attachmentDocument))
End Using
Next
End If
End Sub
Private Shared Function GetDocumentInfo(document As LEADDocument) As String
Return $"DocumentId:{document.DocumentId} MimeType:{document.MimeType} Pages:{document.Pages.Count} Attachments:{document.Attachments.Count}"
End Function
Public Shared Function GetCache() As ObjectCache
' Create a LEADTOOLS FileCache object
Dim cacheDir As String = Path.Combine(ImagesPath.Path, "cache")
If Directory.Exists(cacheDir) Then
Directory.Delete(cacheDir, True)
End If
Directory.CreateDirectory(cacheDir)
Dim cache As New FileCache()
cache.CacheDirectory = cacheDir
Return cache
End 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