Sample scripts describing export
Getting field values during export
This sample code shows how you can get the value of a field during export. You may use this value to name the files into which images or date are exported. If there is no such field in the document, an error will occur.
The structure of the document to be exported:
Section 1
Field1
…
Group1
Field2
…
…
FieldN
Section 2
Field1
…
FieldK
[VBScript]
Dim field1Value, field2Value
' Gets the value of Field1 in Section 1
if Not IsNull( me.Field( "Section 1\Field1" ).Value ) then
field1Value = me.Field( "Section 1\Field1" ).Value
end if
' Gets the value of Field2 in Group1 in Section 1
if Not IsNull( me.FIELD( "Section 1\Group1\Field2" ).Value ) then
field2Value = me.Field( "Section 1\Group1\Field2" ).Value
end if
' Gets the value of Field1 in Section 1 if the section has multiple instances
if Not me.Field( "Section 1" ).Items Is Nothing then
dim curPage
for each curPage in me.Field( "Section 1" ).Items
if Not curPage.Children Is Nothing then
dim curField
for each curField in curPage.Children
if curField.Name = "Field1" And Not IsNull( curField.Value ) then
field1Value = curField.Value
exit for
end if
next
end if
next
end if
[JScript]
// Gets the value of Field1 in Section 1
if( Field("Section 1\\Field1").Value != null ) {
var field1Value = Field("Section 1\\Field1").Value;
}
// Gets the value of Field2 in Group1 in Section 1
if( Field("Section 1\\Group1\\Field2").Value != null ) {
var field2Value = Field("Section 1\\Group1\\Field2").Value;
}
// Gets the value of Field1 in Section 1 if the section has multiple instances
if( Field( "Section 1" ).Items != null ) {
var i, j;
for( i = 0; i < Field( "Section 1" ).Items.Count; i++ ) {
var curPage = Field( "Section 1" ).Items.Item( i );
if( curPage.Children != null ) {
for( j = 0; j < curPage.Children.Count; j++ ) {
var curField = curPage.Children.Item( j );
if( curField.Name == "Field1" && curField.Value != null ) {
var field1Value = curField.Value;
break;
}
}
}
}
}
Exporting page images
The code in this example shows how to export images of all document pages using the required file format, color scheme and resolution. Images are saved by the page, i.e. every page in a separate file.
The files in this example are named "page1.tif," "page2.tif," etc. You can change the file naming mechanism if required. For example, you can create file names based on the value of a field.
[VBScript]
dim fso, folderName
set fso = CreateObject("Scripting.FileSystemObject")
folderName = "d:\ExportImages"
if Not fso.FolderExists( folderName ) then
fso.CreateFolder folderName
end if
dim imageOptions
set imageOptions = FCTools.NewImageSavingOptions
imageOptions.Format = "tif"
imageOptions.ColorType = "BlackAndWhite"
imageOptions.Resolution = 600
dim i
for i = 0 to me.Pages.Count - 1
dim fileName
' Sets the desired name and extension for the file depending on the selected format
fileName = fso.BuildPath( folderName, "page" & (i+1) & ".tif" )
me.Pages.Item(i).SaveAs fileName, imageOptions
next
[JScript]
var fso = new ActiveXObject("Scripting.FileSystemObject");
var folderName = "d:\\ExportImages";
if( !fso.FolderExists( folderName ) ) {
fso.CreateFolder( folderName );
}
var imageOptions = FCTools.NewImageSavingOptions();
imageOptions.Format = "tif";
imageOptions.ColorType = "BlackAndWhite";
imageOptions.Resolution = 600;
var i;
for( i = 0; i < Pages.Count; i++ ) {
// Sets the desired name and extension for the file depending on the selected format
var fileName = fso.BuildPath( folderName, "page" + (i+1) + ".tif" );
Pages.Item(i).SaveAs( fileName, imageOptions );
}
Exporting a table
This example shows how to export a table to a *.txt file.
[VBScript]
dim fso, txtFile, fileName
set fso = CreateObject("Scripting.FileSystemObject")
fileName = "d:\TestExportTable.txt"
set txtFile = fso.CreateTextFile( fileName, true )
txtFile.WriteLine "Table"
dim table, row, cell, rowNumber
set table = me.Field("Page 1\Table")
rowNumber = 1
for each row in table.Items
txtFile.WriteLine "RowNumber = " & rowNumber
for each cell in row.Children
txtFile.Write cell.Value & " "
next
txtFile.WriteBlankLines 1
rowNumber = rowNumber + 1
next
txtFile.Close
set txtFile = nothing
set fso = nothing
[JScript]
var fso, txtFile, fileName;
fso = new ActiveXObject("Scripting.FileSystemObject");
fileName = "d:\\TestExportTable1.txt";
txtFile = fso.CreateTextFile( fileName, true );
txtFile.WriteLine( "Table" );
var table, i, j;
table = Field("Page 1\\Table");
var rows = table.Items;
for( i = 0; i < rows.Count; i++ ) {
txtFile.WriteLine( "RowNumber = " + ( i+1 ) );
var cells = rows.Item(i).Children;
for( j = 0; j < cells.Count; j++ ) {
txtFile.Write( cells.Item(j).Value + " " );
}
txtFile.WriteBlankLines(1);
}
txtFile.Close();
Exporting a document of any nesting level
This example shows how to export fields of a document of any nesting level using custom-made functions from the global export module. If you need to export document fields of variable nesting, it is recommended to use an external COM component (see next example).
Core export code
[VBScript]
Export.ExportDocument me, FCTools
[JScript]
Export.ExportDocument( this, FCTools );
The code of the functions of the global export module
[VBScript]
' The procedure carries out the export: creates an export folder and
' saves in it page image files, the text file with the document fields,
' and information about the document
Sub ExportDocument(ByRef docObj, ByRef FCTools)
Dim folderName
Dim txtFile, fileName
Dim fso
On Error Resume Next
set fso = CreateObject("Scripting.FileSystemObject")
' creating the export folder
folderName = CreateExportFolder(docObj.DefinitionName, fso)
if Err.Number <> 0 then
docObj.Action.Succeeded = false
docObj.Action.ErrorMessage = "Error during create export folder: " + Err.Description
Err.Clear
Exit Subub
End if
' exporting images
ExportImages docObj, FCTools, folderName, fso
if Err.Number <> 0 then
docObj.Action.Succeeded = false
docObj.Action.ErrorMessage = "Error during export images: " + Err.Description
Err.Clear
Exit Sub
End if
' creating the text file
fileName = fso.BuildPath(folderName, "doc.txt")
Set txtFile = fso.CreateTextFile(fileName, True)
' exporting info about the document
ExportDocInfo docObj, txtFile
' exporting fields
txtFile.WriteLine "Fields:"
ExportFields docObj.Children, txtFile, ""
txtFile.Close
if Err.Number <> 0 then
docObj.Action.Succeeded = false
docObj.Action.ErrorMessage = "Error during export data: " + Err.Description
Err.Clear
Exit Sub
End if
Set txtFile = Nothing
Set fso = Nothing
End Sub
' Image export function
Function ExportImages( ByRef docObj, ByRef FCTools, ByVal exportFolder, ByRef fso )
Dim pages, page, imageOptions
Dim fileName, pageNum
' specifying export settings
Set imageOptions = FCTools.NewImageSavingOptions
imageOptions.Format = "bmp"
imageOptions.ColorType = "FullColor"
imageOptions.Resolution = 100
' page-by-page export
Set pages = docObj.Pages
pageNum = 1
For Each page In pageses
fileName = fso.BuildPath(exportFolder, "page" & pageNum & ".bmp")
page.SaveAs fileName, imageOptions
pageNum = pageNum + 1
Next
End Function
' The procedure that exports info about the document
Sub ExportDocInfo(ByRef docObj, ByRef txtFile)
txtFile.WriteLine "Doc info:"
txtFile.WriteLine ("DocumentId " & docObj.Id)
txtFile.WriteLine ("IsAssembled " & docObj.IsAssembled)
txtFile.WriteLine ("IsVerified " & docObj.IsVerified)
txtFile.WriteLine ("IsExported " & docObj.IsExported)
txtFile.WriteLine ("ProcessingErrors " & docObj.ProcessingErrors)
txtFile.WriteLine ("ProcessingWarnings " & docObj.ProcessingWarnings)
txtFile.WriteLine ("TotalSymbolsCount " & docObj.TotalSymbolsCount)
txtFile.WriteLine ("RecognizedSymbolsCount " & docObj.RecognizedSymbolsCount)
txtFile.WriteLine ("UncertainSymbolsCount " & docObj.UncertainSymbolsCount)
txtFile.WriteLine
End Sub
' The procedure that exports fields from the fields collection
Sub ExportFields(ByRef fields, ByRef txtFile, ByVal indent)
Dim curField
For Each curField In fields
ExportField curField, txtFile, indent
Next
End Sub
' Checks if the value of the fields is null
' If the field value is invalid, any attempt to access this field (even
' to check if it is null) may cause an exception
Function IsNullFieldValue( ByRef field )
on error resume next
IsNullFieldValue = IsNull( field.Value )
if Err.Number <> 0 then
IsNullFieldValue = True
Err.Clear
End if
End Function
' Field export procedure
Sub ExportField(ByRef field, ByRef txtFile, ByVal indent)
' saving field name
txtFile.Write (indent & field.Name)
' saving field value if it can be accessed
If IsNullFieldValue(field) Then
txtFile.WriteLine
Else
txtFile.WriteLine (" " & field.Text)
End If
If Not field.Children Is Nothing Then
' exporting child fields
ExportFields field.Children, txtFile, indent & " "
ElseIf Not field.Items Is Nothing Then
' exporting field instances
ExportFields field.Items, txtFile, indent & " "
End If
End Sub
' The function creates an export folder and returns a full path to this folder
Function CreateExportFolder(ByVal definitionName, ByRef fso)
Dim docFolder, folderName
' main folder
exportFolder = "d:\ScriptExport"
If fso.FolderExists(exportFolder) = False Then
fso.CreateFolder (exportFolder)
End If
' the folder of specified Document Definition
docFolder = fso.BuildPath(exportFolder, definitionName)
If fso.FolderExists(docFolder) = False Then
fso.CreateFolder (docFolder)
End If
' the folder of the exported document
Dim i
i = 1
folderName = fso.BuildPath(docFolder, i)
While fso.FolderExists(folderName)
i = i + 1
folderName = fso.BuildPath(docFolder, i)
Wend
fso.CreateFolder (folderName)
CreateExportFolder = folderName
End Function
[JScript]
// The function carries out the export: creates an export folder and
// saves in it page image files, the text file with the document fields,
// and information about the document
function ExportDocument(docObj, exportImageTools)
{
var folderName
var txtFile, fileName
var fso
fso = new ActiveXObject("Scripting.FileSystemObject");
// creating export folders
try {
folderName = CreateExportFolder(docObj.DefinitionName, fso);
} catch( e ) {
docObj.Action.Succeeded = false;
docObj.Action.ErrorMessage = "Error during create export folder: " + e.description;
return;n;
}
// exporting images
try {
ExportImages(docObj, exportImageTools, folderName, fso);
} catch( e ) {
docObj.Action.Succeeded = false;
docObj.Action.ErrorMessage = "Error during export images: " + e.description;
return;
}
// creating the text file
fileName = fso.BuildPath(folderName, "doc.txt");
txtFile = fso.CreateTextFile(fileName, true);
// exporting info about the document
ExportDocInfo( docObj, txtFile );
// exporting fields
txtFile.WriteLine( "Fields:" );
try {
ExportFields( docObj.Children, txtFile, "" );
} catch( e ) { {
docObj.Action.Succeeded = false;
docObj.Action.ErrorMessage = "Error during export data: " + e.description;
txtFile.Close();
return;
}
txtFile.Close();
txtFile = 0;
fso = 0;
}
// Image export function. If an error occurs when exporting images,
// it returns a message about the error; otherwise, it returns an empty string
function ExportImages( docObj, exportImageTools, exportFolder, fso )
{
// specifying export settings
var imageOptions = exportImageTools.NewImageSavingOptions();
imageOptions.Format = "bmp";
imageOptions.ColorType = "FullColor";
imageOptions.Resolution = 100;
// page-by-page export
var pages = docObj.Pages;
var i
for( i = 0; i < pages.Count; i++ ) {
var fileName = fso.BuildPath( exportFolder, "page" + (i+1) + ".bmp" );
pages.Item(i).SaveAs( fileName, imageOptions );
}
}
// The procedure that exports info about the document
function ExportDocInfo(docObj, txtFile)
{
txtFile.WriteLine( "Doc info:" );
txtFile.WriteLine("IsAssembled " + docObj.IsAssembled);
txtFile.WriteLine("IsVerified " + docObj.IsVerified);
txtFile.WriteLine("IsExported " + docObj.IsExported);
txtFile.WriteLine("ProcessingErrors " + docObj.ProcessingErrors);
txtFile.WriteLine("ProcessingWarnings " + docObj.ProcessingWarnings);
txtFile.WriteLine("TotalSymbolsCount " + docObj.TotalSymbolsCount);
txtFile.WriteLine("RecognizedSymbolsCount " + docObj.RecognizedSymbolsCount);
txtFile.WriteLine("UncertainSymbolsCount " + docObj.UncertainSymbolsCount);
txtFile.WriteLine();
}
// The procedure that exports fields from the fields collection
function ExportFields(fields, txtFile, indent)
{
var i
for( i = 0; i < fields.Count; i++ ) {
ExportField( fields.Item(i), txtFile, indent );
}
}
// Checks if the field value is null
// If the field value is invalid, any attempt to access this field (even
// to check if it is null) may cause an exception
function IsNullFieldValue( field )
{
try {
return ( field.Value == null );
} catch( e ) {
return true;
}
}
// Field export procedure
function ExportField(field, txtFile, indent)
{
// saving file name
txtFile.Write(indent + field.Name);
// saving field value, if it can be accessed
if( IsNullFieldValue( field ) ) {
txtFile.WriteLine();
} else {
txtFile.WriteLine(" " + field.Text);
} }
if( field.Children != null ) {
// exporting child fields
ExportFields( field.Children, txtFile, indent + " " );
} else if( field.Items != null ) {
// exporting field instances
ExportFields( field.Items, txtFile, indent + " " );
}
}
// The function creates an export folder and returns a full path to this folder
function CreateExportFolder(definitionName, fso)
{
var docFolder, folderName
// main folder
var exportFolder = "d:\\ScriptExport";
if( !fso.FolderExists(exportFolder) ) {
fso.CreateFolder (exportFolder);
} }
// the folder of the specified Document Definition
docFolder = fso.BuildPath(exportFolder, definitionName);
if( !fso.FolderExists(docFolder) ) {
fso.CreateFolder(docFolder);
}
// the folder of the exported document
var i = 1;
folderName = fso.BuildPath(docFolder, i);
while( fso.FolderExists(folderName) ) {
i++;
folderName = fso.BuildPath(docFolder, i);
}
fso.CreateFolder(folderName);
return folderName;
}
Using an external COM component
[VBScript]
dim autoExport
set autoExport = CreateObject( "AutomationExport.Exporter" )
autoExport.Export me, FCTools
[JScript]
var autoExport = new ActiveXObject("AutomationExport.Exporter");
autoExport.Export( this, FCTools );
Code of an ActiveX component in VisualBasic
Provided below is the Exporter class code from the AutomationExport project, which is used in the above mentioned scripts.
OptionExplicit
Dim mFso AsNew Scripting.FileSystemObject
' The procedure carries out document export: created an export folder and
' saves in it page image files, the text file with document fields,
' and information about the document
PublicSub Export(ByRef docObj As Object, ByRef FCTools As Object)
OnErrorGoTo err_h
Dim folderName AsString
Dim txtFile As TextStream, fileName AsString
Dim imageExportResult AsString, errMessage AsString
' creating export folder
folderName = createExportFolder(docObj.definitionName)
If folderName = "" Then
docObj.Action.Succeeded = False
docObj.Action.ErrorMessage = "Cannot create export folder"
ExitSub
EndIf
' exporting images
imageExportResult = exportImages(docObj, FCTools, folderName)
' creating the text file
fileName = mFso.BuildPath(folderName, "doc.txt")
Set txtFile = mFso.CreateTextFile(fileName, True)
' saving info about image export problems
If imageExportResult <> "" Then
txtFile.WriteLine imageExportResult
errMessage = errMessage & imageExportResult
EndIf
' exporting info about document
exportDocInfo docObj, txtFile
' exporting fields
txtFile.WriteLine "Fields:"
IfNot exportFields(docObj.Children, txtFile, "") Then
errMessage = errMessage & " Error during export data"
EndIf
txtFile.Close
' if errors occur during export, reset the
' export success flag to False
If errMessage <> "" Then
docObj.Action.Succeeded = False
docObj.Action.ErrorMessage = errMessage
EndIf
Set txtFile = Nothing
Set mFso = Nothing
ExitSub
err_h:
docObj.Action.Succeeded = False
docObj.Action.ErrorMessage = Err.Description
txtFile.Close
Set mFso = Nothing
EndSub
' Image export function. If an error occurs when exporting images,
' it returns a message about the error, otherwise it returns an empty string
PrivateFunction exportImages(ByRef docObj As Object, ByRef FCTools As Object, _
ByVal exportFolder AsString) AsString
OnErrorGoTo err_h
Dim pages As Object, page As Object, imageOptions As Object
Dim fileName AsString, pageNum AsLong
exportImages = ""
' specifying export settings
Set imageOptions = FCTools.NewImageSavingOptions
imageOptions.Format = "png"
imageOptions.ColorType = "GrayScale"
imageOptions.Resolution = 300
' page-by-page export
Set pages = docObj.pages
pageNum = 1
ForEach page In pages
fileName = mFso.BuildPath(exportFolder, page.definitionName + "_page" & pageNum & "." & imageOptions.Format)
page.SaveAs fileName, imageOptions
pageNum = pageNum + 1
Next page
ExitFunction
err_h:
exportImages = Err.Description
EndFunction
' The procedure that exports info about the document
PrivateSub exportDocInfo(ByRef docObj As Object, ByRef txtFile As TextStream)
OnErrorGoTo err_h
txtFile.WriteLine "Doc info:"
txtFile.WriteLine ("DocumentId " & docObj.Id)
txtFile.WriteLine ("IsAssembled " & docObj.IsAssembled)
txtFile.WriteLine ("IsVerified " & docObj.IsVerified)
txtFile.WriteLine ("IsExported " & docObj.IsExported)
txtFile.WriteLine ("ProcessingErrors " & docObj.ProcessingErrors)
txtFile.WriteLine ("ProcessingWarnings " & docObj.ProcessingWarnings)
txtFile.WriteLine ("TotalSymbolsCount " & docObj.TotalSymbolsCount)
txtFile.WriteLine ("RecognizedSymbolsCount " & docObj.RecognizedSymbolsCount)
txtFile.WriteLine ("UncertainSymbolsCount " & docObj.UncertainSymbolsCount)
txtFile.WriteLine
ExitSub
err_h:
txtFile.WriteLine Err.Description
EndSub
' The procedure that exports fields from the fields collection
PrivateFunction exportFields(ByRef fields As Object, ByRef txtFile As TextStream, ByVal indent As String) AsBoolean
OnErrorGoTo err_h
Dim curField As Object
exportFields = True
ForEach curField In fields
IfNot exportField(curField, txtFile, indent) Then
exportFields = False
EndIf
Next curField
ExitFunction
err_h:
txtFile.WriteLine Err.Description
exportFields = False
EndFunction
' Checks if the field value is null
' If the value is invalid, any attempt to access this field (even
' to check if it is null) may cause an exception
Function IsNullFieldValue(ByRef field As Object) AsBoolean
OnErrorGoTo err_h
IsNullFieldValue = IsNull(field.Value)
ExitFunction
err_h:
IsNullFieldValue = True
EndFunction
' Field export function
PrivateFunction exportField(ByRef field As Object, ByRef txtFile As TextStream, _
ByVal indent AsString) AsBoolean
OnErrorGoTo err_h
Dim result AsBoolean
result = True
' saving field name
txtFile.Write (indent & field.Name)
' saving field value if it can be accessed
IfNot IsNullFieldValue(field) Then
txtFile.WriteLine (" " & field.Value)
Else
txtFile.WriteLine
EndIf
IfNot field.Children Is NothingThen
' exporting child fields
result = result And exportFields(field.Children, txtFile, indent & " ")
ElseIfNot field.Items IsNothingThen
' exporting field instances
result = result And exportFields(field.Items, txtFile, indent & " ")
EndIf
exportField = result
ExitFunction
err_h:
txtFile.WriteLine Err.Description
exportField = False
EndFunction
' The function creates an export folder and returns a full path to this folder
PrivateFunction createExportFolder(ByVal definitionName AsString) AsString
OnErrorGoTo err_h
Dim docFolder AsString, folderName AsString
' main folder
Const exportFolder = "d:\AutomationExport"
If mFso.FolderExists(exportFolder) = FalseThen
mFso.CreateFolder (exportFolder)
EndIf
' the folder of the specified Document Definition
docFolder = mFso.BuildPath(exportFolder, definitionName)
If mFso.FolderExists(docFolder) = FalseThen
mFso.CreateFolder (docFolder)
EndIf
' the folder of the exported document
Dim i AsLong
i = 1
folderName = mFso.BuildPath(docFolder, i)
While mFso.FolderExists(folderName)
i = i + 1
folderName = mFso.BuildPath(docFolder, i)
Wend
mFso.CreateFolder (folderName)
createExportFolder = folderName
ExitFunction
err_h:
createExportFolder = ""
EndFunction
Using an external component written in .Net( C# )
To use an external component written in .Net, do the following:
- Create a project of type ClassLibrary
- Add ControllerInterop.dll to the References of the project ABBYY.FlexiCapture will appear in the References list and all the interfaces of the script objects will become available in the project.
- Define the disp interface and the class that implements this interface, as well as ProgId (this will make it possible to work with the .Net component just like with ActiveX from the code of the script).
- After building the project, register the generated types library (this can be done automatically by enabling the corresponding option in the project properties).
- From the export code, call the method of the component in the usual manner:
[VBScript]
dim autoExport
set autoExport = CreateObject( "ExportLibrary1.Export" )
autoExport.ExportDocument me, FCTools
[JScript]
var autoExport = new ActiveXObject("ExportLibrary1.Export"); autoExport.ExportDocument( this, FCTools );
Below follows the code of such a component:
using System;
using System.Runtime.InteropServices;
using System.IO;
using ABBYY.FlexiCapture;
namespace ExportLibrary1
{
// The interface of the export component which can be accessed from the script
// When creating a new component, generate a new GUID
[Guid("32B10C3B-EEA3-4194-B0A0-E358C310225A")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface _cExport
{
[DispId(1)]
void ExportDocument(ref IExportDocument DocRef, ref FCTools Tools);
}
// The class that implements export component functionality
// When creating a new component, generate a new GUID
// and set your ProgId
[Guid("3BA19BD7-C6DC-4f63-BC08-0D95254DADC3")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("ExportLibrary1.Export")]
[ComVisible(true)]
public class Export : _cExport
{
public Export()
{
}
// The function carries out document export: creates an export folder and
// saves in it page image files,
// the text file with document fields, and information about the document
public void ExportDocument( ref IExportDocument docRef, ref FCTools FCTools )
{
try
{
string exportFolder = createExportFolder( docRef.DefinitionName );
exportImages( docRef, FCTools, exportFolder );
// creating the text file
string fileName = exportFolder + "//" + "doc.txt";
StreamWriter sw = File.CreateText( fileName );
// exporting info about document
exportDocInfo( docRef, sw );
// exporting fields
sw.WriteLine( "Fields:" );
exportFields( docRef.Children, sw, "" );
sw.Close();
}
catch( Exception e )
{
docRef.Action.Succeeded = false;
docRef.Action.ErrorMessage = e.ToString();
}
}
// The function creates an export folder and returns a full path to this folder
privatestring createExportFolder(string definitionName )
{
string docFolder, folderName;
// main folder
string exportFolder = "d:\\DotNetExport";
if( !Directory.Exists(exportFolder) )
{
Directory.CreateDirectory(exportFolder);
}
// the folder of the specified Document Definition
docFolder = exportFolder + "\\" + definitionName;
if( !Directory.Exists(docFolder) )
{
Directory.CreateDirectory( docFolder );
}
// the folder of the exported document
int i = 1;
folderName = docFolder + "\\" + i;
while( Directory.Exists(folderName) )
{
i++;
folderName = docFolder + "\\" + i;
}
Directory.CreateDirectory(folderName);
return folderName;
}
// Image export function
private void exportImages( IExportDocument docRef, FCTools FCTools, string exportFolder )
{
string baseFileName = exportFolder + "\\page_";
IExportImageSavingOptions imageOptions = FCTools.NewImageSavingOptions();
imageOptions.Format = "bmp";
imageOptions.ColorType = "FullColor";
imageOptions.Resolution = 100;
int i = 1;
foreach( IExportPage curPage in docRef.Pages )
{
string fileName = baseFileName + i + ".bmp";
curPage.SaveAs( fileName, imageOptions );
i++;
}
}
// Exporting info about document
private void exportDocInfo( IExportDocument docRef, StreamWriter sw )
{
sw.WriteLine( "Doc info:" );
sw.WriteLine("DocumentId " + docRef.Id );
sw.WriteLine("IsAssembled " + docRef.IsAssembled);
sw.WriteLine("IsVerified " + docRef.IsVerified);
sw.WriteLine("IsExported " + docRef.IsExported);
sw.WriteLine("ProcessingErrors " + docRef.ProcessingErrors);
sw.WriteLine("ProcessingWarnings " + docRef.ProcessingWarnings);
sw.WriteLine("TotalSymbolsCount " + docRef.TotalSymbolsCount);
sw.WriteLine("RecognizedSymbolsCount " + docRef.RecognizedSymbolsCount);
sw.WriteLine("UncertainSymbolsCount " + docRef.UncertainSymbolsCount);
sw.WriteLine();
}
// Exporting field collection
private void exportFields( IExportFields fields, StreamWriter sw, string indent )
{
foreach( IExportField curField in fields )
{
exportField( curField, sw, indent );
}
}
// Checks if the field value is null
// If the field value is invalid, any attempt to access this field (even
// to check if it is null) may cause an exception
privatebool IsNullFieldValue( IExportField field )
{
try
{
return ( field.Value == null );
}
catch( Exception e )
{
returntrue;
}
}
// Exporting the specified field
private void exportField( IExportField field, StreamWriter sw, string indent )
{
// saving the field name
sw.Write( indent + field.Name );
// saving the field value if it can be accessed
if( IsNullFieldValue( field ) )
{
sw.WriteLine();
}
else
{
sw.WriteLine( " " + field.Text );
}
if( field.Children != null )
{
// exporting child fields
exportFields( field.Children, sw, indent + " " );
}
else if( field.Items != null )
{
// exporting field instances
exportFields( field.Items, sw, indent + " " );
}
}
}
}
Export handler
This sample code demonstrates a simple export handler which saves information about exported documents and export results to text files. Two text files are used in this example: one containing the list of successfully exported documents and one containing the list of documents that returned errors during export.
[VBScript]
dim curDoc
dim fso, fileErrorName, txtErrorFile, fileSucceedName, txtSucceedFile
set fso = CreateObject("Scripting.FileSystemObject")
' creating the file with failed documents
fileErrorName = "d:\ExportResults\ErrorsDocuments.txt"
set txtErrorFile = fso.CreateTextFile( fileErrorName, true )
' creating the file with successfully exported documents
fileSucceedName = "d:\ExportResults\SucceedDocuments.txt"
set txtSucceedFile = fso.CreateTextFile( fileSucceedName, true )
dim i, exprortResult, docInfo
' iterating the collection of exported documents
for i = 0 Tome.Count - 1
set exportResult = me.Item(i)
docInfo = "DocumentId:" & exportResult.Document.Id
if exportResult.Succeeded then
docInfo = docInfo & " - Exported successfully."
txtSucceedFile.WriteLine docInfo
else
docInfo = docInfo & " - Export error: " & exportResult.ErrorMessage
txtErrorFile.WriteLine docInfo
endif
next
txtErrorFile.Close
txtSucceedFile.Close
[JScript]
var fso = new ActiveXObject("Scripting.FileSystemObject");
// creating the file with failed documents
var fileErrorName = "d:\\ExportResults\\ErrorsDocuments.txt";
var txtErrorFile = fso.CreateTextFile( fileErrorName, true );
// creating the file with successfully exported documents
var fileSucceedName = "d:\\ExportResults\\SucceedDocuments.txt"
var txtSucceedFile = fso.CreateTextFile( fileSucceedName, true );
var i
// iterating the collection of exported documents
for( i = 0; i < Documents.Count; i++ ) {
var curDoc = Documents.Item( i );
var docInfo = "DocumentId: " + curDoc.Id;
if( curDoc.Action.Succeeded ) {
docInfo = docInfo + ". Exported successfully. Result:" + curDoc.Action.Result;
txtSucceedFile.WriteLine( docInfo );
} else {
docInfo = docInfo + ". Export error: " + curDoc.Action.ErrorMessage + " Result:" + curDoc.Action.Result;
txtErrorFile.WriteLine( docInfo );
}
}
txtErrorFile.Close();
txtSucceedFile.Close();
Accessing source files in common office formats
This sample script shows how you can save the source files when exporting documents. The source files must be in any of the supported common office formats and must be given unique names in order to avoid naming conflicts.
using System;
using System.IO;
using System.Collections.Generic;
// Iterate through the pages.
for( int i = 0; i<Document.Pages.Count; i++ ) {
IPage page = Document.Pages[i];
// Check whether there is a source file.
if( page.SourceFileGUID == "" ) {
continue;
}
// The original name of the file.
string sourceFileName = @"";
// Specify the original file name.
if( page.ImageSourceType == @"File" ) {
sourceFileName = Path.GetFileName( page.ImageSource );
} else if( page.ImageSourceType == @"Scanner" || page.ImageSourceType == @"Custom" ) {
sourceFileName = Path.GetFileName( page.ImageSourceFileSubPath );
}
// Remove the file extension from the file name.
if( sourceFileName != @"" ) {
sourceFileName = Path.GetFileNameWithoutExtension( sourceFileName ) + @".";
}
// Unique source file name (original name + guid).
string sourceFileUniqueName = sourceFileName + page.SourceFileGUID;
// The path to the folder where the source file must be saved.
string sourceFilePath = Document.Batch.Project.ExportRootPath + @"\" + sourceFileUniqueName;
// Make sure that the file does not exist.
if( File.Exists( sourceFilePath ) ) {
continue;
}
// Save the file.
Processing.ReportMessage( "Saving source file: " + sourceFileUniqueName );
page.SaveSourceFile( sourceFilePath );
}
12.04.2024 18:16:02