エクスポート時にfield値を取得する
エクスポート時にfield値を取得する
このコード例では、エクスポート時にfieldの値を取得する方法を示します。この値は、画像または日付のエクスポート先ファイル名として使用できます。ドキュメント内に該当するfieldが存在しない場合は、エラーが発生します。エクスポートするドキュメントの構造:
Section 1
Field1
…
Group1
Field2
…
…
FieldN
Section 2
Field1
…
FieldK
[VBScript]
Dim field1Value, field2Value
' Section 1 の Field1 の値を取得します
if Not IsNull( me.Field( "Section 1\Field1" ).Value ) then
field1Value = me.Field( "Section 1\Field1" ).Value
end if
' Section 1 の Group1 にある Field2 の値を取得します
if Not IsNull( me.FIELD( "Section 1\Group1\Field2" ).Value ) then
field2Value = me.Field( "Section 1\Group1\Field2" ).Value
end if
' セクションに複数のインスタンスがある場合は、Section 1 の Field1 の値を取得します
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]
// Section 1 の Field1 の値を取得します
if( Field("Section 1\\Field1").Value != null ) {
var field1Value = Field("Section 1\\Field1").Value;
}
// Section 1 の Group1 にある Field2 の値を取得します
if( Field("Section 1\\Group1\\Field2").Value != null ) {
var field2Value = Field("Section 1\\Group1\\Field2").Value;
}
// セクションに複数のインスタンスがある場合は、Section 1 の Field1 の値を取得します
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;
}
}
}
}
}
ページ画像のエクスポート
ページ画像のエクスポート
このコード例では、必要なファイル形式、カラースキーム、解像度を使用して、すべての文書ページの画像をエクスポートする方法を示します。画像はページごとに保存され、各ページが個別のファイルになります。この例では、ファイル名は “page1.tif”、“page2.tif” などになります。必要に応じて、ファイル名の付け方を変更できます。たとえば、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
' 選択した形式に応じて、ファイル名と拡張子を設定します
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++ ) {
// 選択した形式に応じて、ファイル名と拡張子を設定します
var fileName = fso.BuildPath( folderName, "page" + (i+1) + ".tif" );
Pages.Item(i).SaveAs( fileName, imageOptions );
}
テーブルのエクスポート
テーブルのエクスポート
このコード例では、テーブルを *.txt ファイルにエクスポートする方法を示します。
[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();
任意のネストレベルのドキュメントのエクスポート
任意のネストレベルのドキュメントのエクスポート
この例では、グローバルエクスポートモジュールのカスタム関数を使用して、任意のネストレベルのドキュメントのfieldsをエクスポートする方法を示します。ネストレベルが可変のドキュメントのfieldsをエクスポートする必要がある場合は、外部COMコンポーネントの使用をお勧めします (次の例を参照) 。
コアエクスポートコード
[VBScript]
Export.ExportDocument me, FCTools
[JScript]
Export.ExportDocument( this, FCTools );
グローバルExportモジュールの関数のコード
[VBScript]
' このプロシージャはエクスポートを実行します。エクスポートフォルダを作成し、
' ページの画像ファイル、documentのfieldsを含むテキストファイル、
' およびdocumentに関する情報を保存します。
Sub ExportDocument(ByRef docObj, ByRef FCTools)
Dim folderName
Dim txtFile, fileName
Dim fso
On Error Resume Next
set fso = CreateObject("Scripting.FileSystemObject")
' エクスポートフォルダの作成
folderName = CreateExportFolder(docObj.DefinitionName, fso)
if Err.Number <> 0 then
docObj.Action.Succeeded = false
docObj.Action.ErrorMessage = "エクスポートフォルダの作成中にエラーが発生しました: " + Err.Description
Err.Clear
Exit Subub
End if
' 画像のエクスポート
ExportImages docObj, FCTools, folderName, fso
if Err.Number <> 0 then
docObj.Action.Succeeded = false
docObj.Action.ErrorMessage = "画像のエクスポート中にエラーが発生しました: " + Err.Description
Err.Clear
Exit Sub
End if
' テキストファイルの作成
fileName = fso.BuildPath(folderName, "doc.txt")
Set txtFile = fso.CreateTextFile(fileName, True)
' ドキュメントに関する情報をエクスポートする
ExportDocInfo docObj, txtFile
' fieldsのエクスポート
txtFile.WriteLine "Fields:"
ExportFields docObj.Children, txtFile, ""
txtFile.Close
if Err.Number <> 0 then
docObj.Action.Succeeded = false
docObj.Action.ErrorMessage = "エクスポートデータ中にエラーが発生しました: " + Err.Description
Err.Clear
Exit Sub
End if
Set txtFile = Nothing
Set fso = Nothing
End Sub
' 画像エクスポート関数
Function ExportImages( ByRef docObj, ByRef FCTools, ByVal exportFolder, ByRef fso )
Dim pages, page, imageOptions
Dim fileName, pageNum
' エクスポート設定の指定
Set imageOptions = FCTools.NewImageSavingOptions
imageOptions.Format = "bmp"
imageOptions.ColorType = "FullColor"
imageOptions.Resolution = 100
' ページごとのエクスポート
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
' ドキュメントに関する情報をエクスポートするプロシージャ
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
' fieldsコレクションからfieldsをエクスポートするプロシージャ
Sub ExportFields(ByRef fields, ByRef txtFile, ByVal indent)
Dim curField
For Each curField In fields
ExportField curField, txtFile, indent
Next
End Sub
' fieldsの値がnullかどうかを確認します
' field値が無効な場合、このfieldへのアクセス(nullかどうかの確認を含む)は
' 例外が発生する可能性があります
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エクスポート手順
Sub ExportField(ByRef field, ByRef txtFile, ByVal indent)
' フィールド名を保存する
txtFile.Write (indent & field.Name)
' アクセス可能な場合はfieldの値を保存する
If IsNullFieldValue(field) Then
txtFile.WriteLine
Else
txtFile.WriteLine (" " & field.Text)
End If
If Not field.Children Is Nothing Then
' 子fieldのエクスポート
ExportFields field.Children, txtFile, indent & " "
ElseIf Not field.Items Is Nothing Then
' fieldインスタンスのエクスポート
ExportFields field.Items, txtFile, indent & " "
End If
End Sub
' この関数はエクスポートフォルダを作成し、そのフォルダへの完全なパスを返します
Function CreateExportFolder(ByVal definitionName, ByRef fso)
Dim docFolder, folderName
' メインフォルダー
exportFolder = "d:\ScriptExport"
If fso.FolderExists(exportFolder) = False Then
fso.CreateFolder (exportFolder)
End If
' 指定されたDocument Definitionのフォルダー
docFolder = fso.BuildPath(exportFolder, definitionName)
If fso.FolderExists(docFolder) = False Then
fso.CreateFolder (docFolder)
End If
' エクスポートされたドキュメントのフォルダー
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]
// この関数はエクスポートを実行します。エクスポートフォルダーを作成し、
// そこにページの画像ファイル、documentのfieldsを含むテキストファイル、
// およびdocumentに関する情報を保存します。
function ExportDocument(docObj, exportImageTools)
{
var folderName
var txtFile, fileName
var fso
fso = new ActiveXObject("Scripting.FileSystemObject");
// エクスポートフォルダの作成
try {
folderName = CreateExportFolder(docObj.DefinitionName, fso);
} catch( e ) {
docObj.Action.Succeeded = false;
docObj.Action.ErrorMessage = "エクスポートフォルダの作成中にエラーが発生しました: " + e.description;
return;n;
}
// 画像のエクスポート
try {
ExportImages(docObj, exportImageTools, folderName, fso);
} catch( e ) {
docObj.Action.Succeeded = false;
docObj.Action.ErrorMessage = "画像のエクスポート中にエラーが発生しました: " + e.description;
return;
}
// テキストファイルの作成
fileName = fso.BuildPath(folderName, "doc.txt");
txtFile = fso.CreateTextFile(fileName, true);
// documentの情報をエクスポートする
ExportDocInfo( docObj, txtFile );
// fieldsのエクスポート
txtFile.WriteLine( "Fields:" );
try {
ExportFields( docObj.Children, txtFile, "" );
} catch( e ) { {
docObj.Action.Succeeded = false;
docObj.Action.ErrorMessage = "エクスポートデータ中にエラーが発生しました: " + e.description;
txtFile.Close();
return;
}
txtFile.Close();
txtFile = 0;
fso = 0;
}
// 画像エクスポート関数。画像のエクスポート中にエラーが発生した場合は
// エラーに関するメッセージを返し、それ以外の場合は空の文字列を返す
function ExportImages( docObj, exportImageTools, exportFolder, fso )
{
// エクスポート設定の指定
var imageOptions = exportImageTools.NewImageSavingOptions();
imageOptions.Format = "bmp";
imageOptions.ColorType = "FullColor";
imageOptions.Resolution = 100;
// ページごとのエクスポート
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 );
}
}
// Documentの情報をエクスポートするプロシージャ
function ExportDocInfo(docObj, txtFile)
{
txtFile.WriteLine( "ドキュメント情報:" );
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();
}
// fieldsコレクションからfieldsをエクスポートするプロシージャ
function ExportFields(fields, txtFile, indent)
{
var i
for( i = 0; i < fields.Count; i++ ) {
ExportField( fields.Item(i), txtFile, indent );
}
}
// field の値が null かどうかを確認する
// field の値が無効な場合、このフィールドへのアクセス(null かどうかの確認を含む)は
// 例外が発生する可能性がある
function IsNullFieldValue( field )
{
try {
return ( field.Value == null );
} catch( e ) {
return true;
}
}
// fieldのエクスポート処理
function ExportField(field, txtFile, indent)
{
// ファイル名を保存
txtFile.Write(indent + field.Name);
// fieldの値を保存(アクセス可能な場合)
if( IsNullFieldValue( field ) ) {
txtFile.WriteLine();
} else {
txtFile.WriteLine(" " + field.Text);
} }
if( field.Children != null ) {
// 子fieldをエクスポート
ExportFields( field.Children, txtFile, indent + " " );
} else if( field.Items != null ) {
// fieldインスタンスのエクスポート
ExportFields( field.Items, txtFile, indent + " " );
}
}
// この関数はエクスポートフォルダを作成し、そのフォルダへの完全パスを返します
function CreateExportFolder(definitionName, fso)
{
var docFolder, folderName
// メインフォルダー
var exportFolder = "d:\\ScriptExport";
if( !fso.FolderExists(exportFolder) ) {
fso.CreateFolder (exportFolder);
} }
// 指定されたDocument Definitionのフォルダー
docFolder = fso.BuildPath(exportFolder, definitionName);
if( !fso.FolderExists(docFolder) ) {
fso.CreateFolder(docFolder);
}
// エクスポートされたドキュメントのフォルダー
var i = 1;
folderName = fso.BuildPath(docFolder, i);
while( fso.FolderExists(folderName) ) {
i++;
folderName = fso.BuildPath(docFolder, i);
}
fso.CreateFolder(folderName);
return folderName;
}
外部COMコンポーネントの使用
外部COMコンポーネントの使用
[VBScript]
dim autoExport
set autoExport = CreateObject( "AutomationExport.Exporter" )
autoExport.Export me, FCTools
[JScript]
var autoExport = new ActiveXObject("AutomationExport.Exporter");
autoExport.Export( this, FCTools );
VisualBasicにおけるActiveXコンポーネントのコード
以下に、上記のscriptsで使用されているAutomationExportプロジェクトのExporterクラスのコードを示します。OptionExplicit
Dim mFso AsNew Scripting.FileSystemObject
' このプロシージャはドキュメントのエクスポートを実行します。エクスポートフォルダを作成し、
' ページの画像ファイルと、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
' エクスポートフォルダの作成
folderName = createExportFolder(docObj.definitionName)
If folderName = "" Then
docObj.Action.Succeeded = False
docObj.Action.ErrorMessage = "Cannot create export folder"
ExitSub
EndIf
' 画像のエクスポート
imageExportResult = exportImages(docObj, FCTools, folderName)
' テキストファイルの作成
fileName = mFso.BuildPath(folderName, "doc.txt")
Set txtFile = mFso.CreateTextFile(fileName, True)
' 画像エクスポートの問題に関する情報を保存する
If imageExportResult <> "" Then
txtFile.WriteLine imageExportResult
errMessage = errMessage & imageExportResult
EndIf
' Documentに関する情報をエクスポートする
exportDocInfo docObj, txtFile
' fieldsのエクスポート
txtFile.WriteLine "Fields:"
IfNot exportFields(docObj.Children, txtFile, "") Then
errMessage = errMessage & " Error during export data"
EndIf
txtFile.Close
' エクスポート中にエラーが発生した場合、リセットする
' エクスポート成功フラグを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 エクスポート関数。画像のエクスポート中にエラーが発生した場合、
' エラーに関するメッセージを返します。それ以外の場合は空の文字列を返します
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 = ""
' エクスポート設定の指定
Set imageOptions = FCTools.NewImageSavingOptions
imageOptions.Format = "png"
imageOptions.ColorType = "GrayScale"
imageOptions.Resolution = 300
' ページごとのエクスポート
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
次のページ
ExitFunction
err_h:
exportImages = Err.Description
EndFunction
' 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
' fieldsコレクションからfieldsをエクスポートするプロシージャ
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
' fieldの値がnullかどうかを確認します
' 値が無効な場合、このfieldへのアクセスを試みると(たとえ
' nullかどうかを確認する) は例外を引き起こす可能性があります
Function IsNullFieldValue(ByRef field As Object) AsBoolean
OnErrorGoTo err_h
IsNullFieldValue = IsNull(field.Value)
ExitFunction
err_h:
IsNullFieldValue = True
EndFunction
' fieldのエクスポート関数
PrivateFunction exportField(ByRef field As Object, ByRef txtFile As TextStream, _
ByVal indent AsString) AsBoolean
OnErrorGoTo err_h
Dim result AsBoolean
result = True
' fieldの名前を保存中
txtFile.Write (indent & field.Name)
' アクセス可能な場合、field の値を保存する
IfNot IsNullFieldValue(field) Then
txtFile.WriteLine (" " & field.Value)
Else
txtFile.WriteLine
EndIf
IfNot field.Children Is NothingThen
' 子fieldsのエクスポート
result = result And exportFields(field.Children, txtFile, indent & " ")
ElseIfNot field.Items IsNothingThen
' fieldインスタンスのエクスポート
result = result And exportFields(field.Items, txtFile, indent & " ")
EndIf
exportField = result
ExitFunction
err_h:
txtFile.WriteLine Err.Description
exportField = False
EndFunction
' この関数はエクスポートフォルダを作成し、そのフォルダへの完全なパスを返します
PrivateFunction createExportFolder(ByVal definitionName AsString) AsString
OnErrorGoTo err_h
Dim docFolder AsString, folderName AsString
' メインフォルダ
Const exportFolder = "d:\AutomationExport"
If mFso.FolderExists(exportFolder) = FalseThen
mFso.CreateFolder (exportFolder)
EndIf
' 指定されたDocument Definitionのフォルダー
docFolder = mFso.BuildPath(exportFolder, definitionName)
If mFso.FolderExists(docFolder) = FalseThen
mFso.CreateFolder (docFolder)
EndIf
' エクスポートされた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
.Net (C#) で記述された外部コンポーネントの使用
.Netで記述された外部コンポーネントを使用するには、以下の手順を実行します。- ClassLibrary 型のプロジェクトを作成します
- ControllerInterop.dll をプロジェクトの参照に追加すると、参照リストに ABBYY.FlexiCapture が表示され、スクリプト オブジェクトのすべてのインターフェイスをプロジェクトで利用できるようになります。
- disp インターフェイス、このインターフェイスを実装するクラス、および ProgId を定義します (これにより、スクリプトコードから .Net コンポーネントを ActiveX と同様に扱えるようになります) 。
- プロジェクトをビルドした後、生成された型ライブラリを登録します (これは、プロジェクトのプロパティで該当するオプションを有効にすると自動的に実行できます) 。
- エクスポートコードでは、通常どおりコンポーネントのメソッドを呼び出します:
[VBScript]
dim autoExport
set autoExport = CreateObject( "ExportLibrary1.Export" )
autoExport.ExportDocument me, FCTools
[JScript]
var autoExport = new ActiveXObject("ExportLibrary1.Export"); autoExport.ExportDocument( this, FCTools );
using System;
using System.Runtime.InteropServices;
using System.IO;
using ABBYY.FlexiCapture;
namespace ExportLibrary1
{
// スクリプトからアクセスできるエクスポートコンポーネントのインターフェイス
// 新しいコンポーネントを作成する際は、新しいGUIDを生成してください
[Guid("32B10C3B-EEA3-4194-B0A0-E358C310225A")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface _cExport
{
[DispId(1)]
void ExportDocument(ref IExportDocument DocRef, ref FCTools Tools);
}
// エクスポートコンポーネント機能を実装するクラス
// 新しいコンポーネントを作成するときは、新しいGUIDを生成して
// ProgIdを設定してください
[Guid("3BA19BD7-C6DC-4f63-BC08-0D95254DADC3")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("ExportLibrary1.Export")]
[ComVisible(true)]
public class Export : _cExport
{
public Export()
{
}
// この関数はドキュメントのエクスポートを実行します。エクスポートフォルダーを作成し、
// その中にページの画像ファイルを保存します。
// documentのfieldとdocumentに関する情報を含むテキストファイル
public void ExportDocument( ref IExportDocument docRef, ref FCTools FCTools )
{
try
{
string exportFolder = createExportFolder( docRef.DefinitionName );
exportImages( docRef, FCTools, exportFolder );
// テキストファイルの作成
string fileName = exportFolder + "//" + "doc.txt";
StreamWriter sw = File.CreateText( fileName );
// documentの情報をエクスポート
exportDocInfo( docRef, sw );
// fieldsのエクスポート
sw.WriteLine( "Fields:" );
exportFields( docRef.Children, sw, "" );
sw.Close();
}
catch( Exception e )
{
docRef.Action.Succeeded = false;
docRef.Action.ErrorMessage = e.ToString();
}
}
// この関数はエクスポートフォルダを作成し、そのフォルダへの完全なパスを返します
privatestring createExportFolder(string definitionName )
{
string docFolder, folderName;
// メインフォルダ
string exportFolder = "d:\\DotNetExport";
if( !Directory.Exists(exportFolder) )
{
Directory.CreateDirectory(exportFolder);
}
// 指定されたDocument Definitionのフォルダー
docFolder = exportFolder + "\\" + definitionName;
if( !Directory.Exists(docFolder) )
{
Directory.CreateDirectory( docFolder );
}
// エクスポートされたドキュメントのフォルダー
int i = 1;
folderName = docFolder + "\\" + i;
while( Directory.Exists(folderName) )
{
i++;
folderName = docFolder + "\\" + i;
}
Directory.CreateDirectory(folderName);
return folderName;
}
// 画像エクスポート関数
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++;
}
}
// 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();
}
// fieldコレクションのエクスポート
private void exportFields( IExportFields fields, StreamWriter sw, string indent )
{
foreach( IExportField curField in fields )
{
exportField( curField, sw, indent );
}
}
// fieldの値がnullかどうかを確認する
// fieldの値が無効な場合、このfieldへのアクセス(nullチェックを含む)は
// 例外が発生する可能性がある
privatebool IsNullFieldValue( IExportField field )
{
try
{
return ( field.Value == null );
}
catch( Exception e )
{
returntrue;
}
}
// 指定されたfieldをExportingする
private void exportField( IExportField field, StreamWriter sw, string indent )
{
// field名を保存する
sw.Write( indent + field.Name );
// アクセス可能な場合はfieldの値を保存する
if( IsNullFieldValue( field ) )
{
sw.WriteLine();
}
else
{
sw.WriteLine( " " + field.Text );
}
if( field.Children != null )
{
// 子fieldをエクスポート
exportFields( field.Children, sw, indent + " " );
}
else if( field.Items != null )
{
// fieldインスタンスのエクスポート
exportFields( field.Items, sw, indent + " " );
}
}
}
}
エクスポートハンドラー
エクスポートハンドラー
このコード例は、エクスポートされたドキュメントとエクスポート結果に関する情報をテキストファイルに保存するシンプルなエクスポートハンドラーを示しています。この例では 2 つのテキストファイルを使用します。1 つは正常にエクスポートされたドキュメントの一覧を格納し、もう 1 つはエクスポート時にエラーが発生したドキュメントの一覧を格納します。
[VBScript]
dim curDoc
dim fso, fileErrorName, txtErrorFile, fileSucceedName, txtSucceedFile
set fso = CreateObject("Scripting.FileSystemObject")
' エクスポートに失敗したドキュメントを記録するファイルを作成
fileErrorName = "d:\ExportResults\ErrorsDocuments.txt"
set txtErrorFile = fso.CreateTextFile( fileErrorName, true )
' 正常にエクスポートされたドキュメントを記録するファイルを作成
fileSucceedName = "d:\ExportResults\SucceedDocuments.txt"
set txtSucceedFile = fso.CreateTextFile( fileSucceedName, true )
dim i, exprortResult, docInfo
' エクスポートされたドキュメントのコレクションを反復処理
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");
// エクスポートに失敗したドキュメントを記録するファイルを作成
var fileErrorName = "d:\\ExportResults\\ErrorsDocuments.txt";
var txtErrorFile = fso.CreateTextFile( fileErrorName, true );
// 正常にエクスポートされたドキュメントを記録するファイルを作成
var fileSucceedName = "d:\\ExportResults\\SucceedDocuments.txt"
var txtSucceedFile = fso.CreateTextFile( fileSucceedName, true );
var i
// エクスポートされたドキュメントのコレクションを反復処理
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();
一般的なオフィス形式のソースファイルへのアクセス
一般的なオフィス形式のソースファイルへのアクセス
このサンプルスクリプトでは、ドキュメントのエクスポート時にソースファイルを保存する方法を示します。ソースファイルは、サポートされている一般的なオフィス形式 のいずれかである必要があり、名前の競合を避けるために一意の名前を付ける必要があります。
using System;
using System.IO;
using System.Collections.Generic;
// ページを順に処理します。
for( int i = 0; i<Document.Pages.Count; i++ ) {
IPage page = Document.Pages[i];
// ソースファイルがあるかどうかを確認します。
if( page.SourceFileGUID == "" ) {
continue;
}
// 元のファイル名。
string sourceFileName = @"";
// 元のファイル名を取得します。
if( page.ImageSourceType == @"File" ) {
sourceFileName = Path.GetFileName( page.ImageSource );
} else if( page.ImageSourceType == @"Scanner" || page.ImageSourceType == @"Custom" ) {
sourceFileName = Path.GetFileName( page.ImageSourceFileSubPath );
}
// ファイル名から拡張子を削除します。
if( sourceFileName != @"" ) {
sourceFileName = Path.GetFileNameWithoutExtension( sourceFileName ) + @".";
}
// 一意のソースファイル名(元の名前 + guid)。
string sourceFileUniqueName = sourceFileName + page.SourceFileGUID;
// ソースファイルの保存先フォルダのパス。
string sourceFilePath = Document.Batch.Project.ExportRootPath + @"\" + sourceFileUniqueName;
// ファイルが存在しないことを確認します。
if( File.Exists( sourceFilePath ) ) {
continue;
}
// ファイルを保存します。
Processing.ReportMessage( "Saving source file: " + sourceFileUniqueName );
page.SaveSourceFile( sourceFilePath );
}
