There have been a lot of questions about using a workflow to export Document(s) to a Windows folder. I will show a script that will handle almost any export need. It has a lot of checks built into functions, so it may look a little complicated, but I find that building in smaller functions and calling into them makes it easier to both develop and trouble shoot.
The Workflow I will show runs a search for documents and then loops through each found Document and exports it. Here is the Flow:
You can see that in the Assign Token activity, I have a token to specify to export image documents as PDF and another token to specify the windows destination folder.
Use a SDK Script activity and set the Script Language to C# .net and the Script's Default Entry to the For Each Entry - Current Entry. Then open the script editor and add a reference to the Laserfiche.DocumentServices and the corresponding "using" statement.
Then here is the whole of the script that I am starting with:
protected override void Execute()
{
// Write your code here. The BoundEntryInfo property will access the entry, RASession will get the Repository Access session
try
{
// Get Setting for Exporting Image Documents
String sImageToPDF = (String)GetTokenValue("ExportImagesAsPDF");
// Get Path to Windows Folder for Exports
String exportFolder = (String)GetTokenValue("DestinationPath");
// Ensure the Destination folder exists
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(exportFolder);
if (!di.Exists)
{
// Create missing folder(s)
di.Create();
}
// Only Export Documents
if (IsEntryDocument(BoundEntryInfo))
{
// Get DocumentInfo object
using (DocumentInfo doc = (DocumentInfo)BoundEntryInfo)
{
if (IsElectronicDocument(doc)) // Check if it is an electronic document
{
// Export the electronic Portion of the document
ExportElectronic(doc, exportFolder);
}
else if (HasImagePages(doc)) // Check for Image pages
{
if (sImageToPDF.ToLower() == "true")
{
// Export images to PDF
ExportImageDocumentToPDF(doc, exportFolder);
}
else
{
// Export images to TIFF
ExportImageDocument(doc, exportFolder);
}
}
else
{
if (HasText(doc)) // if it has Text
{
// Export text file
ExportTextDocument(doc, exportFolder);
}
else
{
ExportEmptyDocument(doc, exportFolder);
}
}
}
}
}
catch (Exception ex)
{
WorkflowApi.TrackError(ex.Message);
}
}
private void ExportEmptyDocument(DocumentInfo LFDoc, String sExportFolder)
{
try
{
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(sExportFolder);
if (!di.Exists)
{
di.Create();
}
String exportPath= System.IO.Path.Combine(sExportFolder, LFDoc.Name);
System.IO.File.Create(exportPath).Dispose();
}
catch (Exception ex)
{
WorkflowApi.TrackError(ex.Message);
}
}
private void ExportTextDocument(DocumentInfo LFDoc, String sExportFolder)
{
try
{
String sTxt = null;
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(sExportFolder);
if (!di.Exists)
{
di.Create();
}
String exportPath= System.IO.Path.Combine(sExportFolder, LFDoc.Name + ".txt");
using (PageInfoReader LF_PageInfos = LFDoc.GetPageInfos())
{
foreach (PageInfo PI in LF_PageInfos)
{
if (PI.HasText)
{
using (System.IO.StreamReader reader = PI.ReadTextPagePart())
{
if (String.IsNullOrEmpty(sTxt))
{
sTxt = reader.ReadToEnd();
}
else
{
sTxt = sTxt + Environment.NewLine + reader.ReadToEnd();
}
}
}
}
}
using (System.IO.StreamWriter file = new System.IO.StreamWriter(exportPath))
{
file.Write(sTxt);
}
}
catch (Exception ex)
{
WorkflowApi.TrackError(ex.Message);
}
}
private void ExportImageDocument(DocumentInfo LFDoc, String sExportFolder)
{
try
{
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(sExportFolder);
if (!di.Exists)
{
di.Create();
}
DocumentExporter docExporter = new DocumentExporter();
docExporter.PageFormat = DocumentPageFormat.Tiff;
String exportPath = System.IO.Path.Combine(sExportFolder, LFDoc.Name + ".tiff");
docExporter.ExportPages(LFDoc, GetImagePageSet(LFDoc), exportPath);
}
catch (Exception ex)
{
WorkflowApi.TrackError(ex.Message);
}
}
private void ExportImageDocumentToPDF(DocumentInfo LFDoc, String sExportFolder)
{
try
{
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(sExportFolder);
if (!di.Exists)
{
di.Create();
}
DocumentExporter docExporter = new DocumentExporter();
String exportPath = System.IO.Path.Combine(sExportFolder, LFDoc.Name + ".pdf");
docExporter.ExportPdf(LFDoc, GetImagePageSet(LFDoc), PdfExportOptions.None, exportPath);
}
catch (Exception ex)
{
WorkflowApi.TrackError(ex.Message);
}
}
private void ExportElectronic(DocumentInfo LFDoc, String sExportFolder)
{
try
{
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(sExportFolder);
if (!di.Exists)
{
di.Create();
}
DocumentExporter docExporter = new DocumentExporter();
String exportPath = System.IO.Path.Combine(sExportFolder, LFDoc.Name + "." + LFDoc.Extension);
docExporter.ExportElecDoc(LFDoc, exportPath);
}
catch (Exception ex)
{
WorkflowApi.TrackError(ex.Message);
}
}
private PageSet GetImagePageSet(DocumentInfo LFDoc)
{
PageSet psReturn = new PageSet();
try
{
using (PageInfoReader LF_PageInfos = (PageInfoReader)LFDoc.GetPageInfos())
{
foreach (PageInfo PI in LF_PageInfos)
{
using (LaserficheReadStream lrs = PI.ReadPagePart(new PagePart()))
{
if (lrs.Length > 0)
{
psReturn.AddPage(PI.PageNumber);
}
}
}
}
}
catch (Exception ex)
{
WorkflowApi.TrackError(ex.Message);
psReturn = new PageSet();
}
return psReturn;
}
private Boolean HasText(DocumentInfo LFDoc)
{
Boolean bReturn = false;
try
{
using (PageInfoReader LF_PageInfos = LFDoc.GetPageInfos())
{
foreach (PageInfo PI in LF_PageInfos)
{
if (PI.HasText)
{
bReturn = true;
break;
}
}
}
}
catch (Exception ex)
{
WorkflowApi.TrackError(ex.Message);
bReturn = false;
}
return bReturn;
}
private Boolean HasImagePages(DocumentInfo LFDoc)
{
Boolean bReturn = false;
try
{
using (PageInfoReader LF_PageInfos = LFDoc.GetPageInfos())
{
foreach (PageInfo PI in LF_PageInfos)
{
using (LaserficheReadStream lrs = PI.ReadPagePart(new PagePart()))
{
if (lrs.Length > 0)
{
bReturn = true;
break;
}
}
}
}
}
catch (Exception ex)
{
WorkflowApi.TrackError(ex.Message);
bReturn = false;
}
return bReturn;
}
private Boolean IsElectronicDocument(DocumentInfo LFDoc)
{
Boolean bReturn = false;
try
{
bReturn = LFDoc.IsElectronicDocument;
}
catch (Exception ex)
{
WorkflowApi.TrackError(ex.Message);
bReturn = false;
}
return bReturn;
}
private Boolean IsEntryDocument(EntryInfo LFEnt)
{
Boolean bReturn = false;
try
{
if (LFEnt.EntryType == EntryType.Document)
{
bReturn = true;
}
else
{
bReturn = false;
}
}
catch (Exception ex)
{
WorkflowApi.TrackError(ex.Message);
bReturn = false;
}
return bReturn;
}
Please feel free to reply with any improvements you might have.