エクスポート時に field の値を取得する
Section 1
Field1
…
Group1
Field2
…
…
FieldN
Section 2
Field1
…
FieldK
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
// 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
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
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();
任意のネストレベルのドキュメントのエクスポート
エクスポートの基本コード
Export.ExportDocument me, FCTools
Export.ExportDocument( this, FCTools );
グローバルExport モジュールの関数
' このプロシージャはエクスポートを実行します。エクスポート フォルダーを作成し、
' ページの画像ファイル、文書のfieldを含むテキストファイル、
' および文書に関する情報を保存します。
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 = "Error during create export folder: " + 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 = "Error during export images: " + 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 = "Error during export data: " + 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)
' field名の保存
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
' 子fieldsのエクスポート
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
// この関数はエクスポートを実行します。エクスポート フォルダーを作成し、
// ページの画像ファイル、文書のfieldを含むテキストファイル、
// および文書に関する情報をそのフォルダーに保存します。
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 = "Error during create export folder: " + e.description;
return;n;
}
// 画像のエクスポート
try {
ExportImages(docObj, exportImageTools, folderName, fso);
} catch( e ) {
docObj.Action.Succeeded = false;
docObj.Action.ErrorMessage = "Error during export images: " + e.description;
return;
}
// テキストファイルの作成
fileName = fso.BuildPath(folderName, "doc.txt");
txtFile = fso.CreateTextFile(fileName, true);
// 文書に関する情報のエクスポート
ExportDocInfo( docObj, txtFile );
// 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;
}
// 画像エクスポート関数。画像のエクスポート中にエラーが発生した場合は
// エラーメッセージを返し、それ以外の場合は空の文字列を返します。
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 );
}
}
// 文書に関する情報をエクスポートするプロシージャ
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();
}
// 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の値が無効な場合、この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 ) {
// 子fieldsのエクスポート
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コンポーネントの使用
dim autoExport
set autoExport = CreateObject( "AutomationExport.Exporter" )
autoExport.Export me, FCTools
var autoExport = new ActiveXObject("AutomationExport.Exporter");
autoExport.Export( this, FCTools );
Visual Basic の Exporter クラスのコード
AutomationExport プロジェクトの Exporter クラスのコードです。
Option Explicit
Dim mFso As New Scripting.FileSystemObject
' この手順はドキュメントのエクスポートを実行します。エクスポート フォルダーを作成し、
' ページの画像ファイル、文書のfieldを含むテキストファイル、
' およびドキュメントに関する情報を保存します。
Public Sub Export(ByRef docObj As Object, ByRef FCTools As Object)
On Error GoTo err_h
Dim folderName As String
Dim txtFile As TextStream, fileName As String
Dim imageExportResult As String, errMessage As String
' エクスポート フォルダーの作成
folderName = createExportFolder(docObj.definitionName)
If folderName = "" Then
docObj.Action.Succeeded = False
docObj.Action.ErrorMessage = "Cannot create export folder"
Exit Sub
End If
' 画像のエクスポート
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
End If
' ドキュメントに関する情報のエクスポート
exportDocInfo docObj, txtFile
' fieldsのエクスポート
txtFile.WriteLine "Fields:"
If Not exportFields(docObj.Children, txtFile, "") Then
errMessage = errMessage & " Error during export data"
End If
txtFile.Close
' エクスポート中にエラーが発生した場合、
' エクスポート成功フラグをFalseにリセットする
If errMessage <> "" Then
docObj.Action.Succeeded = False
docObj.Action.ErrorMessage = errMessage
End If
Set txtFile = Nothing
Set mFso = Nothing
Exit Sub
err_h:
docObj.Action.Succeeded = False
docObj.Action.ErrorMessage = Err.Description
txtFile.Close
Set mFso = Nothing
End Sub
' 画像エクスポート関数。画像のエクスポート時にエラーが発生した場合は
' エラーメッセージを返し、それ以外の場合は空の文字列を返します。
Private Function exportImages(ByRef docObj As Object, ByRef FCTools As Object, _
ByVal exportFolder As String) As String
On Error GoTo err_h
Dim pages As Object, page As Object, imageOptions As Object
Dim fileName As String, pageNum As Long
exportImages = ""
' エクスポート設定の指定
Set imageOptions = FCTools.NewImageSavingOptions
imageOptions.Format = "png"
imageOptions.ColorType = "GrayScale"
imageOptions.Resolution = 300
' ページごとのエクスポート
Set pages = docObj.pages
pageNum = 1
For Each page In pages
fileName = mFso.BuildPath(exportFolder, page.definitionName + "_page" & pageNum & "." & imageOptions.Format)
page.SaveAs fileName, imageOptions
pageNum = pageNum + 1
Next page
Exit Function
err_h:
exportImages = Err.Description
End Function
' ドキュメントに関する情報をエクスポートする手順
Private Sub exportDocInfo(ByRef docObj As Object, ByRef txtFile As TextStream)
On Error GoTo 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
Exit Sub
err_h:
txtFile.WriteLine Err.Description
End Sub
' fieldsコレクションからfieldsをエクスポートする手順
Private Function exportFields(ByRef fields As Object, ByRef txtFile As TextStream, ByVal indent As String) As Boolean
On Error GoTo err_h
Dim curField As Object
exportFields = True
For Each curField In fields
If Not exportField(curField, txtFile, indent) Then
exportFields = False
End If
Next curField
Exit Function
err_h:
txtFile.WriteLine Err.Description
exportFields = False
End Function
' fieldの値がnullかどうかを確認します。
' 値が無効な場合、このfieldへのアクセス(nullかどうかの
' 確認を含む)は例外を引き起こす可能性があります。
Function IsNullFieldValue(ByRef field As Object) As Boolean
On Error GoTo err_h
IsNullFieldValue = IsNull(field.Value)
Exit Function
err_h:
IsNullFieldValue = True
End Function
' fieldエクスポート関数
Private Function exportField(ByRef field As Object, ByRef txtFile As TextStream, _
ByVal indent As String) As Boolean
On Error GoTo err_h
Dim result As Boolean
result = True
' field名の保存
txtFile.Write (indent & field.Name)
' アクセス可能な場合にfieldの値を保存する
If Not IsNullFieldValue(field) Then
txtFile.WriteLine (" " & field.Value)
Else
txtFile.WriteLine
End If
If Not field.Children Is Nothing Then
' 子fieldsのエクスポート
result = result And exportFields(field.Children, txtFile, indent & " ")
ElseIf Not field.Items Is Nothing Then
' fieldインスタンスのエクスポート
result = result And exportFields(field.Items, txtFile, indent & " ")
End If
exportField = result
Exit Function
err_h:
txtFile.WriteLine Err.Description
exportField = False
End Function
' この関数はエクスポート フォルダーを作成し、そのフォルダーへの完全なパスを返します。
Private Function createExportFolder(ByVal definitionName As String) As String
On Error GoTo err_h
Dim docFolder As String, folderName As String
' メインフォルダー
Const exportFolder = "d:\AutomationExport"
If mFso.FolderExists(exportFolder) = False Then
mFso.CreateFolder (exportFolder)
End If
' 指定されたDocument Definitionのフォルダー
docFolder = mFso.BuildPath(exportFolder, definitionName)
If mFso.FolderExists(docFolder) = False Then
mFso.CreateFolder (docFolder)
End If
' エクスポートされたドキュメントのフォルダー
Dim i As Long
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
Exit Function
err_h:
createExportFolder = ""
End Function
C# で記述された .NET コンポーネントを使用する
1
ClassLibrary Project を作成する
ClassLibrary 型のプロジェクトを作成します。2
ControllerInterop.dll の参照を追加する
ControllerInterop.dll をプロジェクトの References に追加します。References に ABBYY.FlexiCapture が表示され、スクリプト オブジェクトのすべてのインターフェイスをプロジェクト内で使用できるようになります。3
dispinterface、クラス、および ProgId を定義する
dispinterface、それを実装するクラス、および
ProgId を定義します。これにより、ActiveX と同様に、スクリプト コードから .NET コンポーネントを扱えるようになります。4
生成された型ライブラリを登録する
プロジェクトをビルドした後、生成された型ライブラリを登録します。これを自動的に行うには、プロジェクトのプロパティで該当するオプションを有効にします。
5
エクスポート コードからコンポーネントを呼び出す
エクスポート コードから、次の例に示すように通常の方法でコンポーネントのメソッドを呼び出します。
dim autoExport
set autoExport = CreateObject( "ExportLibrary1.Export" )
autoExport.ExportDocument me, FCTools
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()
{
}
// ドキュメントのエクスポートを実行する関数。エクスポート フォルダーを作成し、
// ページの画像ファイル、
// 文書のfieldを含むテキストファイル、およびドキュメントに関する情報を保存します
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 );
// ドキュメント情報のエクスポート
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();
}
}
// エクスポート フォルダーを作成し、そのフォルダーへのフルパスを返す関数
private string 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++;
}
}
// ドキュメント情報のエクスポート
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();
}
// fieldコレクションのエクスポート
private void exportFields( IExportFields fields, StreamWriter sw, string indent )
{
foreach( IExportField curField in fields )
{
exportField( curField, sw, indent );
}
}
// fieldの値がnullかどうかを確認します
// fieldの値が無効な場合、このfieldへのアクセス(nullチェックを含む)で
// 例外が発生する可能性があります
private bool IsNullFieldValue( IExportField field )
{
try
{
return ( field.Value == null );
}
catch( Exception e )
{
return true;
}
}
// 指定されたfieldのエクスポート
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 )
{
// 子fieldsのエクスポート
exportFields( field.Children, sw, indent + " " );
}
else if( field.Items != null )
{
// fieldインスタンスのエクスポート
exportFields( field.Items, sw, indent + " " );
}
}
}
}
エクスポートハンドラーを作成する
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 To me.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
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 );
}
