-
Notifications
You must be signed in to change notification settings - Fork 0
/
DocuwareExport.cs
490 lines (421 loc) · 18.6 KB
/
DocuwareExport.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;
using FlexiCaptureScriptingObjects;
using DocuWare.ToolKit;
using System.Xml;
using System.Xml.XPath;
using System.Windows.Forms;
using System.Security.Principal;
using System.Management;
using System.Security.Permissions;
using System.Security;
namespace DocuWareExport
{
// The interface of the export component which can be accessed from the script
// When creating a new component, generate a new GUID
[Guid("48b33872-7e3a-47cc-8bc1-647fe3d08039")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface _cExport
{
[DispId(1)]
void ExportDocument(ref IExportDocument DocRef, ref IExportTools Tools, string XMLfolder);
}
// The class that implements export component functionality
// When creating a new component, generate a new GUID
[Guid("33a6a485-f3d0-438e-931d-8efaab5353cb")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("BanDocuWareExport.Export")]
[ComVisible(true)]
public class Export : _cExport
{
DocuwareFileCabinet fileCabinetForm;
Session session;
Basket basket;
ActiveBasket abasket;
FileCabinet filecabinet;
string xmlfilecabinetpath = "";
string xmlfieldname = "";
string xmlfieldindex = "";
List<string> fieldnamelist = new List<string>();
List<uint> fieldindexlist = new List<uint>();
Dictionary<int, string> openWith = new Dictionary<int, string>();
Document docum;
string fieldValue;
StreamWriter sw;
public Export()
{
}
public void UpdateLogFile(string s)
{
string logfolder = @"c:\BanLog\";
string logfile = "log.txt";
if (!Directory.Exists(logfolder))
{
try
{
Directory.CreateDirectory(logfolder);
}
catch (Exception e)
{
throw new ArgumentException("CAN NOT create a log folder - " + e.Message.ToString() + " - from UpdateLogFile function " + ". Please make sure you have permission to read/write.");
}
}
else if (Directory.Exists(logfolder))
{
if (!File.Exists(logfolder+logfile))
{
try
{
sw = File.CreateText(logfolder+logfile);
sw.WriteLine(DateTime.Now + " -> " + s.ToString());
sw.Close();
}
catch (Exception e)
{
throw new ArgumentException("CAN NOT create and write to logfile - " + e.Message.ToString() + " " +" - from UpdateLogFile function " + ". Please make sure you have permission to read/write.");
}
}
else if(File.Exists(logfolder+logfile))
{
try
{
sw = File.AppendText(logfolder+logfile);
sw.WriteLine(DateTime.Now + " --> " + s.ToString());
sw.Close();
}
catch (Exception e)
{
throw new ArgumentException("CAN NOT append to logfile - " + e.Message.ToString() + " " + " - from UpdateLogFile function " + ". Please make sure you have permission to read/write.");
}
}
}
}
private StreamWriter StreamWriter(string p, bool p_2)
{
throw new NotImplementedException();
}
// 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
[FileIOPermission(SecurityAction.Assert, Read = "C:/")]
public void ExportDocument(ref IExportDocument docRef, ref IExportTools exportTools, string XMLfolder)
{
try
{
session = new Session();
string templateFolder = createTemplateFolder( XMLfolder);
UpdateLogFile(templateFolder);
string templateXMLfile = createTemplateXML(docRef, exportTools, templateFolder, docRef.TemplateName);
}
catch (Exception e)
{
docRef.Action.Succeeded = false;
docRef.Action.ErrorMessage = e.ToString();
UpdateLogFile(e.Message);
//throw new NoDescException("Interface not implemented for " + objName);
}
}
//the function create folder which will contain XML templateName
private string createTemplateFolder(string XMLfolder)
{
string templateFolder = XMLfolder;
try
{
FileIOPermission fileIOPermission = new FileIOPermission(FileIOPermissionAccess.AllAccess, templateFolder);
fileIOPermission.Demand();
}
catch (SecurityException ex)
{
UpdateLogFile("Error: " + ex.Message + " PermissionType: "+ex.PermissionType);
}
if (!Directory.Exists(templateFolder))
{
try
{
Directory.CreateDirectory(templateFolder);
}
catch (Exception e)
{
throw new ArgumentException("CAN NOT create a folder - " +e.Message.ToString() + " - to store Docuware template XML files. Please make sure you have permission to read/write.");
}
}
return templateFolder;
}
private string createTemplateXML(IExportDocument docRef, IExportTools exportTools, string tempfolder, string tempName)
{
string templateFolder = tempfolder;
string templateName = tempName.ToString() + ".xml";
string temp = templateFolder + "\\" + templateName;
NTAccount ntAccount = new NTAccount("Everyone");
SecurityIdentifier sid = (SecurityIdentifier)ntAccount.Translate(typeof(SecurityIdentifier));
byte[] sidArray = new byte[sid.BinaryLength];
sid.GetBinaryForm(sidArray, 0);
if (!File.Exists(temp))
{
fileCabinetForm = new DocuwareFileCabinet(docRef, exportTools, temp);
fileCabinetForm.ShowDialog();
}
if (File.Exists(temp))
{
// create the XmlReader object
XmlReaderSettings settings = new XmlReaderSettings();
XmlReader reader = XmlReader.Create(temp.ToString(), settings);
int depth = -1; // tree depth is -1, no indentation
while (reader.Read()) // display each node's content
{
switch (reader.NodeType)
{
case XmlNodeType.Element: // XML Element, display its name
depth++; // increase tab depth
TabOutput(depth); // insert tabs
if (reader.Name == "FileCabinetName")
{
depth++;
reader.Read();
xmlfilecabinetpath = reader.Value;
depth--;
reader.Read();
}
if (reader.Name == "FieldName")
{
depth++;
reader.Read();
xmlfieldname = reader.Value;
fieldnamelist.Add(reader.Value);
getFields(docRef.Children, docRef, xmlfieldname);
depth--;
reader.Read();
}
if (reader.Name == "FieldIndex")
{
depth++;
reader.Read();
xmlfieldindex = reader.Value;
fieldindexlist.Add(Convert.ToUInt32(reader.Value));
depth--;
reader.Read();
openWith.Add(Convert.ToInt32(xmlfieldindex), fieldValue);
}
// if empty element, decrease depth
if (reader.IsEmptyElement)
depth--;
break;
case XmlNodeType.Comment: // XML Comment, display it
TabOutput(depth); // insert tabs
break;
case XmlNodeType.Text: // XML Text, display it
TabOutput(depth); // insert tabs
break;
// XML XMLDeclaration, display it
case XmlNodeType.XmlDeclaration:
TabOutput(depth); // insert tabs
break;
case XmlNodeType.EndElement: // XML EndElement, display it
TabOutput(depth); // insert tabs
depth--; // decrement depth
break;
} // end switch
} // end while
//------------------- DocuWare -------------------
string fullimportimagepath = exportImages(docRef, exportTools, templateFolder);
UpdateLogFile("Full path of current working image: "+fullimportimagepath);
//select filecabinet
filecabinet = new FileCabinet(session, xmlfilecabinetpath);
UpdateLogFile(filecabinet.FileCabinetPath);
filecabinet.Open();
abasket = new ActiveBasket(session);
// open basket
basket = session.GetActiveBasket();
basket = new Basket(session, abasket.BasketPath);
UpdateLogFile("Active Basket Path: " + abasket.BasketPath);
basket = new ActiveBasket(session);
basket.Open();
basket.SetAsActive();
UpdateLogFile("Basket: "+basket.BasketPath);
//sending bantran.tif to current basket.
docum = basket.ImportFile(fullimportimagepath);
UpdateLogFile("FileName: "+docum.FileName);
if (File.Exists(fullimportimagepath))
{
File.Delete(fullimportimagepath);
}
int numberofdocuments = basket.GetNumberOfDocuments();
docum = new Document(basket, docum.FileName);
UpdateLogFile("Name: " + docum.Name);
UpdateLogFile("FileName: " + docum.FileName);
if (!File.Exists(docum.FileName))
{
UpdateLogFile(docum.FileName + " path does not exist after import into basket.");
}
try
{
filecabinet.Store(docum, false, true, openWith);
}
catch (Exception e)
{
UpdateLogFile(filecabinet.LastException.Message+ " | " + e.Message);
}
}
return templateName;
}
// Image export function.
private string exportImages(IExportDocument docRef, IExportTools exportTools, string exportFolder)
{
string baseFileName = exportFolder + "\\" + "bantran";
string baseFileNameFullpath = "";
IExportImageSavingOptions imageOptions = exportTools.NewImageSavingOptions();
imageOptions.Format = "tif";
imageOptions.ColorType = "BlackAndWhite";
imageOptions.Resolution = 300;
imageOptions.ShouldOverwrite = true;
docRef.SaveAs(baseFileName + ".tif", imageOptions);
baseFileNameFullpath = baseFileName + ".tif";
return baseFileNameFullpath;
}
// insert tabs
private void TabOutput(int number)
{
for (int i = 0; i < number; i++)
{
//OutputTextBox.Text += "\t";
}
} // end method TabOutput
// The function creates an export folder and returns a full path to this folder
private string createExportFolder(string templateName)
{
string docFolder, folderName;
// main folder
string exportFolder = "c:\\DotNetExport";
if (!Directory.Exists(exportFolder))
{
Directory.CreateDirectory(exportFolder);
}
// the folder of the specified Document Definition
docFolder = exportFolder + "\\" + templateName;
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;
}
// 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();
}
// Getting field collection
private void getFields(IExportFields fields, IExportDocument docRef, string fieldName) //string indent
{
foreach (IExportField curField in fields)
{
getField(curField, docRef, fieldName);
}
}
// Getting the specified field
private void getField(IExportField field, IExportDocument docRef, string fieldName)
{
// saving the field name
if (field.Children != null)
{
getFields(field.Children, docRef, fieldName);
}
else if (field.Items != null)
{
getFields(field.Items, docRef, fieldName);
}
else if (field.Value != null)
{
if (field.IsExportable == true && field.Name == fieldName)
{
fieldValue = field.Value.ToString();
//MessageBox.Show(field.Value.ToString(), fieldValue.ToString());
}
}
}
// Exporting field collection
private void exportFields(IExportFields fields, IExportDocument docRef, StreamWriter sw) //string indent
{
foreach (IExportField curField in fields)
{
//exportField(curField, docRef, indent);
exportField(curField, docRef, sw);
}
}
// Exporting the specified field
private void exportField(IExportField field, IExportDocument docRef, StreamWriter sw)
{
// saving the field value it can be accessed
if (IsNullFieldValue(field))
{
sw.WriteLine();
//fieldvalue = "";
}
else
{
//sw.WriteLine(" " + field.Text);
//fieldvalue = field.Text;
}
if (field.Children != null)
{
// exporting child fields
//exportFields(field.Children, docRef, indent + " ");
exportFields(field.Children, docRef, sw);
}
else
{
if (field.Items != null)
{
// exporting field instances
//exportFields(field.Items, docRef, indent + " ");
exportFields(field.Items, docRef, sw);
}
else
{
if (field.IsExportable == true)
{
// fLayer.DataSource.Add(field);
sw.Write(" " + field.Name);
sw.WriteLine(" " + field.Text);
}
}
}
}
// 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
private bool IsNullFieldValue(IExportField field)
{
try
{
return (field.Value == null);
}
catch (Exception e)
{
return true;
}
}
}
}