logo

Converting a file generated by a template to pdf in a business process

This article provides an example of a business process, during which a document is created, a file is generated by a template, converted to pdf and attached as a version to the created document.

Generate File by Template process

The process map looks like this:

 

Context variables:

Let's take a look at each activity:

1. Create Document script.

The script text:

var doc = InterfaceActivator.Create<File>();         
//Identify a folder
doc.Folder = (Folder)FolderManager.Instance.Load(531);
//Specify a name
doc.Name = "New document dated " + DateTime.Now.ToString();
//Save
doc.Save(); 
// Write the created document to the context variable
context.NewDocument = (File)doc;
 
Creating a similar script with PublicAPI
Documentation on PublicAPI is available here
var docFolder = PublicAPI.Docflow.Folder.LoadOrNull(531);
var docName = string.Format("New document dated {0}", DateTime.Now);
var doc = PublicAPI.Docflow.Types.File.Create(docFolder, docName);
doc.Save();
context.NewDocument = (File)doc;

The script:

  • Creates a new document of the File type in the folder with the specified ID.
  • If you need to change the document type, you need to replace File with the class name of the required document type. You also need to change the context variable type to New Document.
  • In the line doc.Folder = (Folder)FolderManager.Instance.Load(531); replace 531 with the ID of the folder, where you need to create the document (you can find it in the browser URL bar after opening the required folder in the web application)

2. Generate Document Version plug-in

  • Generates a file by a template to the specified context variable (Generated File). On the Settings tab of the activity add a template and a File type variable, to which the generated document will be saved.

 

 3. Internal sub-process "Convert to pdf" (described below)

 4. Add Document Version script

The script text:

var version = InterfaceActivator.Create<DocumentVersion>();
version.Document = context.NewDocument;
context.Temp = InterfaceActivator.Create<BinaryFile>(); 
context.Temp.Name = "Template1.pdf"; 
context.Temp.CreateDate = DateTime.Now; 
context.Temp.InitializeContentFilePath();
System.IO.File.Copy(@"c:\\Template1.pdf", context.Temp.ContentFilePath);
Locator.GetServiceNotNull<IFileManager>().SaveFile(context.Temp);
version.File=context.Temp;
version.Status = DocumentVersionStatus.Current;//set the current status to the version
context.NewDocument.Versions.Add(version);
context.NewDocument.Save();
 
Creating a similar script with PublicAPI
Documentation on PublicAPI is available here
var version = PublicAPI.Docflow.DocumentVersion.Create();
version.Document = context.NewDocument;
context.Temp = InterfaceActivator.Create<BinaryFile>();
context.Temp.Name = "Template1.pdf";
context.Temp.CreateDate = DateTime.Now;
context.Temp.InitializeContentFilePath();
System.IO.File.Copy(@"c:\\Template1.pdf", context.Temp.ContentFilePath);
version.File = context.Temp;
version.Status = DocumentVersionStatus.Current;//set the current status to the version
context.NewDocument.Versions.Add(version);
context.NewDocument.Save();

This script:

  • creates and attaches a version to the created document. The version name is defined in the line context.Temp.Name = "Template1.pdf"; you can change it to any other.
  • in the line System.IO.File.Copy(@"c:\\Template1.pdf", context.Temp.ContentFilePath) the path to a temporary file in the pdf format from the Convert to pdf sub-process is specified.

Namespaces used in the scripts module of the business process:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EleWise.ELMA.Documents.Managers;
using EleWise.ELMA.Documents.Models;
using EleWise.ELMA.Files;
using EleWise.ELMA.Model.Common;
using EleWise.ELMA.Model.Entities;
using EleWise.ELMA.Model.Entities.ProcessContext;
using EleWise.ELMA.Model.Managers;
using EleWise.ELMA.Model.Services;
using EleWise.ELMA.Model.Types.Settings;
using EleWise.ELMA.Runtime.Managers;
using EleWise.ELMA.Services;
using Microsoft.CSharp;

+ add the Microsoft.CSharp global assembly. On the Scripts tab of the process, click Add -> Add a link to assembly. In the opened window select Microsoft.CSharp and click OK.

Internal sub-process Convert to pdf

It includes one activity – Convert to pdf script:

  • It uses temporary service files "Template1.doc" (or "Template1.xls") and "Template1.pdf". You need to know beforehand where they will be stored and change the path to the files in the script. E.g. c:\\Template1.doc (or c:\\Template1.xls ) and c:\\Template1.pdf
  • The first two lines of the script remove the temporary files, in order to modify them later.
  • The third line copies the file generated in the parent process to the temporary file Template1.doc (or Template1.xls)
  • The fourth and the fifth lines convert the file to pdf.

 Consider two cases:

1) Converting an MS Word document to pdf

The script will look like this:

System.IO.File.Delete(@"c:\\Template1.doc");
System.IO.File.Delete(@"c:\\Template1.pdf");
System.IO.File.Copy(context.GeneratedFile.ContentFilePath, @"c:\\Template1.doc");
Document doc = new Document(@"c:\\Template1.doc");
doc.Save(@"c:\\Template1.pdf", Aspose.Words.SaveFormat.Pdf);

2) Converting an Excel table to pdf

The script will look like this:

System.IO.File.Delete(@"c:\\Template1.xls");
System.IO.File.Delete(@"c:\\Template1.pdf");
System.IO.File.Copy(context.InvoiceTemplate.ContentFilePath, @"c:\\Template1.xls");
Workbook workbook = new Workbook(@"c:\\Template1.xls");
workbook.Save(@"c:\\Template1.pdf", Aspose.Cells.SaveFormat.Pdf);

For the script to work correctly, add the following assemblies:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EleWise.ELMA.Model.Common;
using EleWise.ELMA.Model.Entities;
using EleWise.ELMA.Model.Managers;
using EleWise.ELMA.Model.Types.Settings;
using EleWise.ELMA.Model.Entities.ProcessContext;
using System.Xml;
using Aspose.Words; //for converting a doc file
using Aspose.Cells; //for converting an xls file

+ add the System.Xml global assembly. On the Scripts tab of the process, click Add -> Add a link to assembly. In the opened window select System.Xml and click OK.