Feldwerte beim Export abrufen
Section 1
Field1
…
Group1
Field2
…
…
FieldN
Section 2
Field1
…
FieldK
Dim field1Value, field2Value
' Ruft den Wert von Field1 in Section 1 ab
if Not IsNull( me.Field( "Section 1\Field1" ).Value ) then
field1Value = me.Field( "Section 1\Field1" ).Value
end if
' Ruft den Wert von Field2 in Group1 in Section 1 ab
if Not IsNull( me.FIELD( "Section 1\Group1\Field2" ).Value ) then
field2Value = me.Field( "Section 1\Group1\Field2" ).Value
end if
' Ruft den Wert von Field1 in Section 1 ab, wenn die Section mehrere Instanzen aufweist
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
// Ruft den Wert von Field1 in Section 1 ab
if( Field("Section 1\\Field1").Value != null ) {
var field1Value = Field("Section 1\\Field1").Value;
}
// Ruft den Wert von Field2 in Group1 in Section 1 ab
if( Field("Section 1\\Group1\\Field2").Value != null ) {
var field2Value = Field("Section 1\\Group1\\Field2").Value;
}
// Ruft den Wert von Field1 in Section 1 ab, wenn die Section mehrere Instanzen aufweist
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;
}
}
}
}
}
Seitenbilder exportieren
page1.tif, page2.tif usw. Sie können das Schema für die Dateibenennung ändern — zum Beispiel, indem Sie den Wert eines Felds als Grundlage für den Dateinamen verwenden.
Das folgende VBScript-Beispiel exportiert die Seitenbilder:
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
' Legt den gewünschten Namen und die Erweiterung für die Datei je nach ausgewähltem Format fest
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++ ) {
// Legt den gewünschten Namen und die Erweiterung für die Datei je nach ausgewähltem Format fest
var fileName = fso.BuildPath( folderName, "page" + (i+1) + ".tif" );
Pages.Item(i).SaveAs( fileName, imageOptions );
}
Eine Tabelle exportieren
*.txt-Datei.
Das folgende VBScript-Beispiel exportiert die Tabelle:
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();
Exportieren eines Dokuments mit beliebiger Verschachtelungstiefe
Kerncode für den Export
Export.ExportDocument me, FCTools
Export.ExportDocument( this, FCTools );
Funktionen des globalen Export-Moduls
' Die Prozedur führt den Export durch: erstellt einen Exportordner und
' speichert darin Seitenbilddateien, die Textdatei mit den Dokumentfeldern
' sowie Informationen über das Dokument
Sub ExportDocument(ByRef docObj, ByRef FCTools)
Dim folderName
Dim txtFile, fileName
Dim fso
On Error Resume Next
set fso = CreateObject("Scripting.FileSystemObject")
' Exportordner erstellen
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
' Bilder exportieren
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
' Textdatei erstellen
fileName = fso.BuildPath(folderName, "doc.txt")
Set txtFile = fso.CreateTextFile(fileName, True)
' Dokumentinformationen exportieren
ExportDocInfo docObj, txtFile
' Felder exportieren
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
' Bildexportfunktion
Function ExportImages( ByRef docObj, ByRef FCTools, ByVal exportFolder, ByRef fso )
Dim pages, page, imageOptions
Dim fileName, pageNum
' Exporteinstellungen festlegen
Set imageOptions = FCTools.NewImageSavingOptions
imageOptions.Format = "bmp"
imageOptions.ColorType = "FullColor"
imageOptions.Resolution = 100
' Seitenweiser 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
' Die Prozedur exportiert Informationen über das Dokument
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
' Die Prozedur exportiert Felder aus der Felder-Sammlung
Sub ExportFields(ByRef fields, ByRef txtFile, ByVal indent)
Dim curField
For Each curField In fields
ExportField curField, txtFile, indent
Next
End Sub
' Prüft, ob der Wert der Felder null ist
' Wenn der Feldwert ungültig ist, kann jeder Zugriffsversuch auf dieses Feld (auch
' die Prüfung auf null) eine Ausnahme auslösen
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
' Feldexportprozedur
Sub ExportField(ByRef field, ByRef txtFile, ByVal indent)
' Feldname speichern
txtFile.Write (indent & field.Name)
' Feldwert speichern, sofern darauf zugegriffen werden kann
If IsNullFieldValue(field) Then
txtFile.WriteLine
Else
txtFile.WriteLine (" " & field.Text)
End If
If Not field.Children Is Nothing Then
' Untergeordnete Felder exportieren
ExportFields field.Children, txtFile, indent & " "
ElseIf Not field.Items Is Nothing Then
' Feldinstanzen exportieren
ExportFields field.Items, txtFile, indent & " "
End If
End Sub
' Die Funktion erstellt einen Exportordner und gibt den vollständigen Pfad zu diesem Ordner zurück
Function CreateExportFolder(ByVal definitionName, ByRef fso)
Dim docFolder, folderName
' Hauptordner
exportFolder = "d:\ScriptExport"
If fso.FolderExists(exportFolder) = False Then
fso.CreateFolder (exportFolder)
End If
' Der Ordner der angegebenen Document Definition
docFolder = fso.BuildPath(exportFolder, definitionName)
If fso.FolderExists(docFolder) = False Then
fso.CreateFolder (docFolder)
End If
' Der Ordner des exportierten Dokuments
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
// Die Funktion führt den Export durch: erstellt einen Exportordner und
// speichert darin Seitenbilddateien, die Textdatei mit den Dokumentfeldern
// sowie Informationen über das Dokument
function ExportDocument(docObj, exportImageTools)
{
var folderName
var txtFile, fileName
var fso
fso = new ActiveXObject("Scripting.FileSystemObject");
// Exportordner erstellen
try {
folderName = CreateExportFolder(docObj.DefinitionName, fso);
} catch( e ) {
docObj.Action.Succeeded = false;
docObj.Action.ErrorMessage = "Error during create export folder: " + e.description;
return;n;
}
// Bilder exportieren
try {
ExportImages(docObj, exportImageTools, folderName, fso);
} catch( e ) {
docObj.Action.Succeeded = false;
docObj.Action.ErrorMessage = "Error during export images: " + e.description;
return;
}
// Textdatei erstellen
fileName = fso.BuildPath(folderName, "doc.txt");
txtFile = fso.CreateTextFile(fileName, true);
// Informationen über das Dokument exportieren
ExportDocInfo( docObj, txtFile );
// Felder exportieren
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;
}
// Bildexportfunktion. Tritt beim Exportieren von Bildern ein Fehler auf,
// wird eine Fehlermeldung zurückgegeben; andernfalls wird eine leere Zeichenkette zurückgegeben
function ExportImages( docObj, exportImageTools, exportFolder, fso )
{
// Exporteinstellungen festlegen
var imageOptions = exportImageTools.NewImageSavingOptions();
imageOptions.Format = "bmp";
imageOptions.ColorType = "FullColor";
imageOptions.Resolution = 100;
// Seitenweiser 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 );
}
}
// Prozedur zum Exportieren von Informationen über das Dokument
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();
}
// Prozedur zum Exportieren von Feldern aus der Felder-Sammlung
function ExportFields(fields, txtFile, indent)
{
var i
for( i = 0; i < fields.Count; i++ ) {
ExportField( fields.Item(i), txtFile, indent );
}
}
// Prüft, ob der Feldwert null ist
// Ist der Feldwert ungültig, kann jeder Zugriffsversuch auf dieses Feld (selbst
// die Prüfung auf null) eine Ausnahme auslösen
function IsNullFieldValue( field )
{
try {
return ( field.Value == null );
} catch( e ) {
return true;
}
}
// Feldexportprozedur
function ExportField(field, txtFile, indent)
{
// Dateinamen speichern
txtFile.Write(indent + field.Name);
// Feldwert speichern, sofern er zugänglich ist
if( IsNullFieldValue( field ) ) {
txtFile.WriteLine();
} else {
txtFile.WriteLine(" " + field.Text);
} }
if( field.Children != null ) {
// Untergeordnete Felder exportieren
ExportFields( field.Children, txtFile, indent + " " );
} else if( field.Items != null ) {
// Feldinstanzen exportieren
ExportFields( field.Items, txtFile, indent + " " );
}
}
// Die Funktion erstellt einen Exportordner und gibt den vollständigen Pfad zu diesem Ordner zurück
function CreateExportFolder(definitionName, fso)
{
var docFolder, folderName
// Hauptordner
var exportFolder = "d:\\ScriptExport";
if( !fso.FolderExists(exportFolder) ) {
fso.CreateFolder (exportFolder);
} }
// Der Ordner der angegebenen Document Definition
docFolder = fso.BuildPath(exportFolder, definitionName);
if( !fso.FolderExists(docFolder) ) {
fso.CreateFolder(docFolder);
}
// Der Ordner des exportierten Dokuments
var i = 1;
folderName = fso.BuildPath(docFolder, i);
while( fso.FolderExists(folderName) ) {
i++;
folderName = fso.BuildPath(docFolder, i);
}
fso.CreateFolder(folderName);
return folderName;
}
Verwendung einer externen COM-Komponente
dim autoExport
set autoExport = CreateObject( "AutomationExport.Exporter" )
autoExport.Export me, FCTools
var autoExport = new ActiveXObject("AutomationExport.Exporter");
autoExport.Export( this, FCTools );
Code der Exporter-Klasse in Visual Basic
Exporter-Klasse aus dem AutomationExport-Projekt, der in den vorherigen Skripten verwendet wurde.
Option Explicit
Dim mFso As New Scripting.FileSystemObject
' Die Prozedur führt den Dokumentexport durch: erstellt einen Exportordner und
' speichert darin Seitenbilddateien, die Textdatei mit Dokumentfeldern
' sowie Informationen über das Dokument
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
' Exportordner erstellen
folderName = createExportFolder(docObj.definitionName)
If folderName = "" Then
docObj.Action.Succeeded = False
docObj.Action.ErrorMessage = "Cannot create export folder"
Exit Sub
End If
' Bilder exportieren
imageExportResult = exportImages(docObj, FCTools, folderName)
' Textdatei erstellen
fileName = mFso.BuildPath(folderName, "doc.txt")
Set txtFile = mFso.CreateTextFile(fileName, True)
' Informationen zu Bildexportproblemen speichern
If imageExportResult <> "" Then
txtFile.WriteLine imageExportResult
errMessage = errMessage & imageExportResult
End If
' Informationen über das Dokument exportieren
exportDocInfo docObj, txtFile
' Felder exportieren
txtFile.WriteLine "Fields:"
If Not exportFields(docObj.Children, txtFile, "") Then
errMessage = errMessage & " Error during export data"
End If
txtFile.Close
' Wenn beim Export Fehler auftreten, wird das
' Erfolgs-Flag auf False zurückgesetzt
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
' Bildexportfunktion. Wenn beim Exportieren von Bildern ein Fehler auftritt,
' wird eine Fehlermeldung zurückgegeben, andernfalls eine leere Zeichenkette
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 = ""
' Exporteinstellungen festlegen
Set imageOptions = FCTools.NewImageSavingOptions
imageOptions.Format = "png"
imageOptions.ColorType = "GrayScale"
imageOptions.Resolution = 300
' Seitenweiser Export
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
' Die Prozedur exportiert Informationen über das Dokument
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
' Die Prozedur exportiert Felder aus der Felder-Sammlung
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
' Prüft, ob der Feldwert null ist
' Wenn der Wert ungültig ist, kann jeder Zugriffsversuch auf dieses Feld (selbst
' die Prüfung auf null) eine Ausnahme auslösen
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
' Feldexportfunktion
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
' Feldnamen speichern
txtFile.Write (indent & field.Name)
' Feldwert speichern, sofern darauf zugegriffen werden kann
If Not IsNullFieldValue(field) Then
txtFile.WriteLine (" " & field.Value)
Else
txtFile.WriteLine
End If
If Not field.Children Is Nothing Then
' Untergeordnete Felder exportieren
result = result And exportFields(field.Children, txtFile, indent & " ")
ElseIf Not field.Items Is Nothing Then
' Feldinstanzen exportieren
result = result And exportFields(field.Items, txtFile, indent & " ")
End If
exportField = result
Exit Function
err_h:
txtFile.WriteLine Err.Description
exportField = False
End Function
' Die Funktion erstellt einen Exportordner und gibt den vollständigen Pfad zu diesem Ordner zurück
Private Function createExportFolder(ByVal definitionName As String) As String
On Error GoTo err_h
Dim docFolder As String, folderName As String
' Hauptordner
Const exportFolder = "d:\AutomationExport"
If mFso.FolderExists(exportFolder) = False Then
mFso.CreateFolder (exportFolder)
End If
' Der Ordner der angegebenen Document Definition
docFolder = mFso.BuildPath(exportFolder, definitionName)
If mFso.FolderExists(docFolder) = False Then
mFso.CreateFolder (docFolder)
End If
' Der Ordner des exportierten Dokuments
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
Verwenden einer in C# geschriebenen .NET-Komponente
1
Ein ClassLibrary-Projekt erstellen
Erstellen Sie ein Projekt vom Typ
ClassLibrary.2
Den Verweis auf ControllerInterop.dll hinzufügen
Fügen Sie
ControllerInterop.dll zu den Verweisen des Projekts hinzu. ABBYY.FlexiCapture wird in der Liste „Verweise“ angezeigt, und alle Schnittstellen der Skriptobjekte werden im Projekt verfügbar.3
Die dispinterface, die Klasse und die ProgId definieren
Definieren Sie die dispinterface und die Klasse, die diese Schnittstelle implementiert, sowie die
ProgId. Dadurch können Sie mit der .NET-Komponente aus Skriptcode genauso wie mit ActiveX arbeiten.4
Die generierte Typbibliothek registrieren
Registrieren Sie nach dem Build des Projekts die generierte Typbibliothek. Um dies automatisch zu tun, aktivieren Sie die entsprechende Option in den Projekteigenschaften.
5
Die Komponente aus dem Exportcode aufrufen
Rufen Sie im Exportcode die Methode der Komponente wie gewohnt auf, wie im folgenden Beispiel gezeigt.
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
{
// Die Schnittstelle der Exportkomponente, auf die über das Skript zugegriffen werden kann
// Beim Erstellen einer neuen Komponente eine neue GUID generieren
[Guid("32B10C3B-EEA3-4194-B0A0-E358C310225A")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface _cExport
{
[DispId(1)]
void ExportDocument(ref IExportDocument DocRef, ref FCTools Tools);
}
// Die Klasse, die die Funktionalität der Exportkomponente implementiert
// Beim Erstellen einer neuen Komponente eine neue GUID generieren
// und die eigene ProgId festlegen
[Guid("3BA19BD7-C6DC-4f63-BC08-0D95254DADC3")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("ExportLibrary1.Export")]
[ComVisible(true)]
public class Export : _cExport
{
public Export()
{
}
// Die Funktion führt den Dokumentexport durch: erstellt einen Exportordner und
// speichert darin Seitenbilddateien,
// die Textdatei mit Dokumentfeldern sowie Informationen über das Dokument
public void ExportDocument( ref IExportDocument docRef, ref FCTools FCTools )
{
try
{
string exportFolder = createExportFolder( docRef.DefinitionName );
exportImages( docRef, FCTools, exportFolder );
// Textdatei erstellen
string fileName = exportFolder + "//" + "doc.txt";
StreamWriter sw = File.CreateText( fileName );
// Dokumentinformationen exportieren
exportDocInfo( docRef, sw );
// Felder exportieren
sw.WriteLine( "Fields:" );
exportFields( docRef.Children, sw, "" );
sw.Close();
}
catch( Exception e )
{
docRef.Action.Succeeded = false;
docRef.Action.ErrorMessage = e.ToString();
}
}
// Die Funktion erstellt einen Exportordner und gibt den vollständigen Pfad zu diesem Ordner zurück
private string createExportFolder(string definitionName )
{
string docFolder, folderName;
// Hauptordner
string exportFolder = "d:\\DotNetExport";
if( !Directory.Exists(exportFolder) )
{
Directory.CreateDirectory(exportFolder);
}
// Der Ordner der angegebenen Document Definition
docFolder = exportFolder + "\\" + definitionName;
if( !Directory.Exists(docFolder) )
{
Directory.CreateDirectory( docFolder );
}
// Der Ordner des exportierten Dokuments
int i = 1;
folderName = docFolder + "\\" + i;
while( Directory.Exists(folderName) )
{
i++;
folderName = docFolder + "\\" + i;
}
Directory.CreateDirectory(folderName);
return folderName;
}
// Bildexportfunktion
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++;
}
}
// Dokumentinformationen exportieren
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();
}
// Feldsammlung exportieren
private void exportFields( IExportFields fields, StreamWriter sw, string indent )
{
foreach( IExportField curField in fields )
{
exportField( curField, sw, indent );
}
}
// Prüft, ob der Feldwert null ist
// Wenn der Feldwert ungültig ist, kann jeder Zugriffsversuch auf dieses Feld (selbst
// die Prüfung auf null) eine Ausnahme auslösen
private bool IsNullFieldValue( IExportField field )
{
try
{
return ( field.Value == null );
}
catch( Exception e )
{
return true;
}
}
// Das angegebene Feld exportieren
private void exportField( IExportField field, StreamWriter sw, string indent )
{
// Feldnamen speichern
sw.Write( indent + field.Name );
// Feldwert speichern, sofern er zugänglich ist
if( IsNullFieldValue( field ) )
{
sw.WriteLine();
}
else
{
sw.WriteLine( " " + field.Text );
}
if( field.Children != null )
{
// Untergeordnete Felder exportieren
exportFields( field.Children, sw, indent + " " );
}
else if( field.Items != null )
{
// Feldinstanzen exportieren
exportFields( field.Items, sw, indent + " " );
}
}
}
}
Einen Export-Handler schreiben
dim curDoc
dim fso, fileErrorName, txtErrorFile, fileSucceedName, txtSucceedFile
set fso = CreateObject("Scripting.FileSystemObject")
' Erstellen der Datei mit fehlgeschlagenen Dokumenten
fileErrorName = "d:\ExportResults\ErrorsDocuments.txt"
set txtErrorFile = fso.CreateTextFile( fileErrorName, true )
' Erstellen der Datei mit erfolgreich exportierten Dokumenten
fileSucceedName = "d:\ExportResults\SucceedDocuments.txt"
set txtSucceedFile = fso.CreateTextFile( fileSucceedName, true )
dim i, exprortResult, docInfo
' Durchlaufen der Sammlung exportierter Dokumente
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");
// Erstellen der Datei mit fehlgeschlagenen Dokumenten
var fileErrorName = "d:\\ExportResults\\ErrorsDocuments.txt";
var txtErrorFile = fso.CreateTextFile( fileErrorName, true );
// Erstellen der Datei mit erfolgreich exportierten Dokumenten
var fileSucceedName = "d:\\ExportResults\\SucceedDocuments.txt"
var txtSucceedFile = fso.CreateTextFile( fileSucceedName, true );
var i
// Durchlaufen der Sammlung exportierter Dokumente
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();
Zugriff auf Quelldateien in gängigen Office-Formaten
using System;
using System.IO;
using System.Collections.Generic;
// Seiten durchlaufen.
for( int i = 0; i<Document.Pages.Count; i++ ) {
IPage page = Document.Pages[i];
// Prüfen, ob eine Quelldatei vorhanden ist.
if( page.SourceFileGUID == "" ) {
continue;
}
// Der ursprüngliche Name der Datei.
string sourceFileName = @"";
// Den ursprünglichen Dateinamen angeben.
if( page.ImageSourceType == @"File" ) {
sourceFileName = Path.GetFileName( page.ImageSource );
} else if( page.ImageSourceType == @"Scanner" || page.ImageSourceType == @"Custom" ) {
sourceFileName = Path.GetFileName( page.ImageSourceFileSubPath );
}
// Die Dateiendung aus dem Dateinamen entfernen.
if( sourceFileName != @"" ) {
sourceFileName = Path.GetFileNameWithoutExtension( sourceFileName ) + @".";
}
// Eindeutiger Name der Quelldatei (ursprünglicher Name + GUID).
string sourceFileUniqueName = sourceFileName + page.SourceFileGUID;
// Der Pfad zum Ordner, in dem die Quelldatei gespeichert werden soll.
string sourceFilePath = Document.Batch.Project.ExportRootPath + @"\" + sourceFileUniqueName;
// Sicherstellen, dass die Datei noch nicht vorhanden ist.
if( File.Exists( sourceFilePath ) ) {
continue;
}
// Die Datei speichern.
Processing.ReportMessage( "Saving source file: " + sourceFileUniqueName );
page.SaveSourceFile( sourceFilePath );
}
