Hi Michael,
The below code snippet should do what you are looking for. Note that it is not completely tested, and it does not make any effort to catch exceptions. There are of course errors if invalid arguments are provided. I assume that you can create a Session object, have TIF images on disk to be used as pages and know their paths, and can create a Dictionary containing field names and values of the appropriate type. Let me know if you have any questions.
Edit: I have updated this code with the changes noted by Michael Allen in his reply. I changed the creation of the DocumentInfo to use DocumentInfo.Create instead of Document.Create, and I added a comment about the fact that a DocumentInfo is returned that must be disposed.
public static DocumentInfo CreateDocument(
// The folder to create the document in
string folderPath,
// The name for the document
string name,
// The session to be used to create the document. Must be already authenticated
Session lfSession,
// Paths to images on disk to use as the pages for the document. Assumed to be .tif
List<string> pageFilePaths,
// The name of the template to apply to the document
string template,
// A dictionary containing all of the fields to set and their values
Dictionary<string, object> fields)
{
// Find the requested parent folder
FolderInfo parentFolder = Folder.GetFolderInfo(folderPath, lfSession);
// Different creation method, as suggested by Michael Allen
// Create the Document using DocumentInfo.Create
DocumentInfo document = new DocumentInfo(lfSession);
document.Create(
parentFolder,
name,
// If you want to overwrite an existing file, or rename if there is a name conflict, use
// EntryNameOption.Overwrite or EntryNameOption.AutoRename
EntryNameOption.None);
/* Old code. As noted by Michael Allen, it is more efficient to use the DocumentInfo.Create method (above)
* than to use the Document.Create method and then construct a DocumentInfo object from the returned id.
*
* // Create the document, and get a DocumentInfo object corresponding to the new document
* int documentEntryId = Document.Create(
* parentFolder,
* name,
* // If you want to overwrite an existing file, or rename if there is a name conflict, use
* // EntryNameOption.Overwrite or EntryNameOption.AutoRename
* EntryNameOption.None,
* lfSession);
* DocumentInfo document = Document.GetDocumentInfo(documentEntryId, lfSession);
*/
// Loop through the image paths given. For each one, read the image into memory, create a new page in the
// document, write the image to the page, and save the page.
foreach (string pageFilePath in pageFilePaths)
{
byte[] image = File.ReadAllBytes(pageFilePath);
PageInfo newPage = document.AppendPage();
Stream pageStream = newPage.WritePagePart(PagePart.Image, image.Length);
pageStream.Write(image, 0, image.Length);
pageStream.Dispose();
newPage.Save();
}
// Read the given field values into a FieldValueCollection
FieldValueCollection fieldValues = new FieldValueCollection();
foreach (string field in fields.Keys)
fieldValues.Add(field, fields[field]);
// Set the template on the document, and all of the field values. Note that items in the
// FieldValueCollection need not be on the provided template
document.SetTemplate(template, fieldValues);
// Save and return the document
document.Save();
/* If you don't care about the DocumentInfo object after the document is created, you can have this method
* not return anything (change the signature to public static void), and instead do
* document.Dispose()
* here. Otherwise, the caller is expected to call Dispose on the returned DocumentInfo object.
*/
return document;
}