logo

Generating a file using a script

This article provides an example of a script for generating a file by a template. The template is a document version.

 Script with PublicAPI

Note
Documentation on PublicAPI is available here
Attention
The script below is relevant for ELMA 3.8 to 3.12.2 inclusive.
Namespaces:
using EleWise.ELMA.Documents.Managers;
using EleWise.ELMA.Runtime.Managers;
using EleWise.ELMA.Services;
 
Script text:
int id_doc = 26; //ID of the template document
var versionFile = Locator.GetServiceNotNull<IDocumentFileManager>().GetFileByDocumentId(id_doc);
var file = Locator.GetServiceNotNull<ITemplateFileManager>().CreateFromTemplate(versionFile.Id);
// Create a generator
PublicAPI.Services.DocumentGenerator.Generate(file, context);
// Save the generated document file
PublicAPI.Services.File.SaveFile(file);
// Write the result to a variable
context.ResultFile = file;

Script without PublicAPI

For the script to work, add assemblies:

Elewise.ELMA.Documents 
Elewise.TemplateGenerator.Merged
 
Namespaces:
using EleWise.ELMA.Documents.Managers; 
using EleWise.ELMA.Runtime.Managers; 
using EleWise.ELMA.Services; 
using EleWise.ELMA.Templates;
 
Script:
int id_doc=26; //ID of the template document 
var versionFile = Locator.GetServiceNotNull<IDocumentFileManager>().GetFileByDocumentId(id_doc); 
var file = Locator.GetServiceNotNull<ITemplateFileManager>().CreateFromTemplate(versionFile.Id); 
// Create a generator 
var generator = Locator.GetServiceNotNull<DocumentGeneratorManager>().Create(file.ContentFilePath); 
// Generate a document
generator.Generate(context); 
// Save the generated file
Locator.GetServiceNotNull<IFileManager>().SaveFile(file); 
// Write the result to the variable
context.ResultFile = file;