Display name of the attachment.
public string DisplayName {get; set;}
The display name of the attachment. The default value is null.
DisplayName is parsed from the original document, and is optional. For attachments embedded in PDF documents, DisplayName will be equal to FileName.
For more information, refer to Document Attachments.
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:\LEADTOOLS22\Resources\Images\leadtools.pdf";
// Attachments we will be adding: a TIF, another PDF, and a JPG file
string[] attachmentFiles =
{
@"C:\LEADTOOLS22\Resources\Images\ocr1.tif",
@"C:\LEADTOOLS22\Resources\Images\employee benefits survey.pdf",
@"C:\LEADTOOLS22\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}";
}