forked from SergiyStoyan/PdfDocumentParser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPage.fields.cs
469 lines (439 loc) · 23.8 KB
/
Page.fields.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
//********************************************************************************************
//Author: Sergiy Stoyan
// s.y.stoyan@gmail.com, sergiy.stoyan@outlook.com, stoyan@cliversoft.com
// http://www.cliversoft.com
//********************************************************************************************
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
namespace Cliver.PdfDocumentParser
{
/// <summary>
/// pdf page parsing API
/// </summary>
public partial class Page
{
/// <summary>
/// It is helpful when a field has more than 1 definition and its image is required.
/// !!!Only must be used for the field returned by GetValue(string fieldName, out Template.Field actualField, Template.Field.Types? type = null)
/// ATTENTION: using Template.Field as a parameter may be deceitful for these reasons:
/// - it may belong to another template than ActiveTemplate;
/// - it implies that a Template.Field object is equivalent to a field while it is just one of its defintions;
/// - it may be the same by logic while being another as an object;
/// </summary>
/// <param name="exactField"></param>
/// <param name="type"></param>
/// <returns></returns>
//public Bitmap GetImage(Template.Field exactField)
//{
// RectangleF? ar = getFieldActualRectangle(exactField);
// FieldActualInfo fai = new FieldActualInfo(this, exactField, ar, exactField.ColumnOfTable != null ? getFoundFieldActualInfo(exactField.ColumnOfTable) : null);
// if (!fai.Found)
// return null;
// return (Bitmap)fai.GetValue(Template.Field.Types.Image);
//}
internal FieldActualInfo GetFieldActualInfo(Template.Field field)
{
List<FieldActualInfo> fais = getFieldActualInfos(field.Name);
//FieldActualInfo fai = fais.Find(a => a.ActualField == field);!!!while the fields can be same by logic, they are different objects!
string fs = Serialization.Json.Serialize(field, false, true);
FieldActualInfo fai = fais.Find(a => Serialization.Json.Serialize(a.ActualField, false, true) == fs);
if (fai == null)
{
RectangleF? ar = getFieldActualRectangle(field);
fai = new FieldActualInfo(this, field, ar, field.ColumnOfTable != null ? getFoundFieldActualInfo(field.ColumnOfTable) : null);
fais.Add(fai);
}
return fai;
}
FieldActualInfo getFoundFieldActualInfo(string fieldName)
{
return getFieldActualInfos(fieldName)[0];
}
List<FieldActualInfo> getFieldActualInfos(string fieldName)
{
if (!fieldNames2fieldActualInfos.TryGetValue(fieldName, out List<FieldActualInfo> fais))
{
fais = new List<FieldActualInfo>();
fieldNames2fieldActualInfos[fieldName] = fais;
if (PageCollection.ActiveTemplate == null)
throw new Exception("ActiveTemplate is not set for the PageCollection.");
foreach (Template.Field f in PageCollection.ActiveTemplate.Fields.Where(x => x.Name == fieldName))
{
RectangleF? ar = getFieldActualRectangle(f);
FieldActualInfo fai = new FieldActualInfo(this, f, ar, f.ColumnOfTable != null ? getFoundFieldActualInfo(f.ColumnOfTable) : null);
if (fai.Found)
{
fais.Insert(0, fai);
break;
}
fais.Add(fai);
}
if (fais.Count < 1)
throw new Exception("Field[name=" + fieldName + "] does not exist.");
}
return fais;
}
HandyDictionary<string, List<FieldActualInfo>> fieldNames2fieldActualInfos = new HandyDictionary<string, List<FieldActualInfo>>(
disposeValue: (List<FieldActualInfo> v) =>
{
if (v == null)
return;
foreach (FieldActualInfo fai in v)
fai?.Dispose();
}
);
internal class FieldActualInfo : IDisposable
{
~FieldActualInfo()
{
Dispose();
}
public void Dispose()
{
types2cachedValue.Dispose();
}
readonly internal Template.Field ActualField;
readonly internal RectangleF? ActualRectangle;
readonly internal FieldActualInfo TableFieldActualInfo = null;
readonly internal bool Found;
internal object GetValue(Template.Field.Types type)
{
if (!page.PageCollection.CacheDisposableFieldValues && (type == Template.Field.Types.Image || type == Template.Field.Types.OcrTextLineImages))
return getValue(type);
if (!types2cachedValue.TryGetValue(type, out object o))
{
o = getValue(type);
types2cachedValue[type] = o;
}
return o;
}
HandyDictionary<Template.Field.Types, object> types2cachedValue;
object getValue(Template.Field.Types type)
{
if (ActualRectangle == null || TableFieldActualInfo?.Found == false)
return null;
switch (type)
{
case Template.Field.Types.PdfText:
{
List<string> ss = getPdfTextLines();
return ss == null ? null : string.Join("\r\n", ss);
}
case Template.Field.Types.PdfTextLines:
return getPdfTextLines();
case Template.Field.Types.PdfCharBoxs:
return getPdfCharBoxs();
case Template.Field.Types.OcrText:
{
List<string> ss = getOcrTextLines();
return ss == null ? null : string.Join("\r\n", ss);
}
case Template.Field.Types.OcrTextLines:
return getOcrTextLines();
case Template.Field.Types.OcrCharBoxs:
return getOcrCharBoxs();
case Template.Field.Types.Image:
return getImage();
case Template.Field.Types.OcrTextLineImages:
return getOcrTextLineImages();
default:
throw new Exception("Unknown option: " + type);
}
}
readonly Page page;
List<string> getPdfTextLines()
{
if (ActualRectangle == null)
return null;
RectangleF ar = (RectangleF)ActualRectangle;
TextAutoInsertSpace textAutoInsertSpace = ActualField.TextAutoInsertSpace != null ? ActualField.TextAutoInsertSpace : page.PageCollection.ActiveTemplate.TextAutoInsertSpace;
if (ActualField.ColumnOfTable == null)
return Pdf.GetTextLinesSurroundedByRectangle(page.PdfCharBoxs, ar, textAutoInsertSpace);
List<string> ls = new List<string>();
List<Pdf.CharBox> cbs = (List<Pdf.CharBox>)TableFieldActualInfo.GetValue(Template.Field.Types.PdfCharBoxs);
foreach (Line<Pdf.CharBox> l in GetLines(cbs, textAutoInsertSpace, null))
{
StringBuilder sb = new StringBuilder();
foreach (Pdf.CharBox cb in l.CharBoxs)
if (cb.R.Left >= ar.Left && cb.R.Right <= ar.Right && cb.R.Top >= ar.Top && cb.R.Bottom <= ar.Bottom)
sb.Append(cb.Char);
ls.Add(sb.ToString());
}
return ls;
}
List<Pdf.CharBox> getPdfCharBoxs()
{
if (ActualRectangle == null)
return null;
RectangleF ar = (RectangleF)ActualRectangle;
if (ActualField.ColumnOfTable == null)
return Pdf.GetCharBoxsSurroundedByRectangle(page.PdfCharBoxs, ar);
if (!TableFieldActualInfo.Found)
return null;
List<Pdf.CharBox> cbs = (List<Pdf.CharBox>)TableFieldActualInfo.GetValue(Template.Field.Types.PdfCharBoxs);
return cbs.Where(a => a.R.Left >= ar.Left && a.R.Right <= ar.Right && a.R.Top >= ar.Top && a.R.Bottom <= ar.Bottom).ToList();
}
List<string> getOcrTextLines()
{
if (ActualRectangle == null)
return null;
RectangleF ar = (RectangleF)ActualRectangle;
Template.Field.Ocr aof = ActualField as Template.Field.Ocr;
TextAutoInsertSpace textAutoInsertSpace = aof?.TextAutoInsertSpace != null ? aof?.TextAutoInsertSpace : page.PageCollection.ActiveTemplate.TextAutoInsertSpace;
if (ActualField.ColumnOfTable == null)
{
if (aof?.SingleFieldFromFieldImage ?? page.PageCollection.ActiveTemplate.SingleFieldFromFieldImage)
{
List<Ocr.CharBox> cs = Ocr.This.GetCharBoxsSurroundedByRectangle(page.ActiveTemplateBitmap, ar, aof?.TesseractPageSegMode ?? page.PageCollection.ActiveTemplate.TesseractPageSegMode);
if (cs == null)
return null;
return GetTextLines(cs, textAutoInsertSpace, ActualField.CharFilter ?? page.PageCollection.ActiveTemplate.CharFilter);
}
else
return Ocr.GetTextLinesSurroundedByRectangle(page.ActiveTemplateOcrCharBoxs, ar, textAutoInsertSpace, ActualField.CharFilter ?? page.PageCollection.ActiveTemplate.CharFilter);
}
if (!TableFieldActualInfo.Found)
return null;
List<Ocr.CharBox> cbs = (List<Ocr.CharBox>)TableFieldActualInfo.GetValue(Template.Field.Types.OcrCharBoxs);
List<string> ls = new List<string>();
if (aof?.ColumnCellFromCellImage ?? page.PageCollection.ActiveTemplate.ColumnCellFromCellImage)
{
List<Line<Ocr.CharBox>> ols = GetLines(cbs, null, ActualField.CharFilter ?? page.PageCollection.ActiveTemplate.CharFilter);
if (aof?.AdjustLineBorders ?? page.PageCollection.ActiveTemplate.AdjustLineBorders)
AdjustBorders(ols, TableFieldActualInfo.ActualRectangle.Value);
else
PadLines(ols, ActualField.LinePaddingY ?? page.PageCollection.ActiveTemplate.LinePaddingY);
foreach (Line<Ocr.CharBox> l in ols)
{
float x = ar.X > TableFieldActualInfo.ActualRectangle.Value.X ? ar.X : TableFieldActualInfo.ActualRectangle.Value.X;
RectangleF r = new RectangleF(
x,
l.Top,
(ar.Right < TableFieldActualInfo.ActualRectangle.Value.Right ? ar.Right : TableFieldActualInfo.ActualRectangle.Value.Right) - x,
l.Bottom - l.Top
);
List<Ocr.CharBox> cs = Ocr.This.GetCharBoxsSurroundedByRectangle(page.ActiveTemplateBitmap, r, aof?.TesseractPageSegMode ?? page.PageCollection.ActiveTemplate.TesseractPageSegMode);
ls.Add(cs != null ? string.Join("", GetTextLines(cs, textAutoInsertSpace, ActualField.CharFilter ?? page.PageCollection.ActiveTemplate.CharFilter)) : "");
}
}
else
{
foreach (Line<Ocr.CharBox> l in GetLines(cbs, textAutoInsertSpace, ActualField.CharFilter ?? page.PageCollection.ActiveTemplate.CharFilter))
{
StringBuilder sb = new StringBuilder();
foreach (Ocr.CharBox cb in l.CharBoxs)
if (cb.R.Left >= ar.Left && cb.R.Right <= ar.Right && cb.R.Top >= ar.Top && cb.R.Bottom <= ar.Bottom)
sb.Append(cb.Char);
ls.Add(sb.ToString());
}
}
return ls;
}
List<Ocr.CharBox> getOcrCharBoxs()
{
if (ActualRectangle == null)
return null;
RectangleF ar = (RectangleF)ActualRectangle;
Template.Field.Ocr aof = ActualField as Template.Field.Ocr;
if (ActualField.ColumnOfTable == null)
{
if (aof?.SingleFieldFromFieldImage ?? page.PageCollection.ActiveTemplate.SingleFieldFromFieldImage)
return Ocr.This.GetCharBoxsSurroundedByRectangle(page.ActiveTemplateBitmap, ar, aof?.TesseractPageSegMode ?? page.PageCollection.ActiveTemplate.TesseractPageSegMode);
else
return Ocr.GetCharBoxsSurroundedByRectangle(page.ActiveTemplateOcrCharBoxs, ar);
}
if (!TableFieldActualInfo.Found)
return null;
if (aof?.ColumnCellFromCellImage ?? page.PageCollection.ActiveTemplate.ColumnCellFromCellImage)
{
float x = ar.X > TableFieldActualInfo.ActualRectangle.Value.X ? ar.X : TableFieldActualInfo.ActualRectangle.Value.X;
float y = ar.Top > TableFieldActualInfo.ActualRectangle.Value.Top ? ar.Top : TableFieldActualInfo.ActualRectangle.Value.Top;
RectangleF r = new RectangleF(
x,
y,
(ar.Right < TableFieldActualInfo.ActualRectangle.Value.Right ? ar.Right : TableFieldActualInfo.ActualRectangle.Value.Right) - x,
(ar.Bottom < TableFieldActualInfo.ActualRectangle.Value.Bottom ? ar.Bottom : TableFieldActualInfo.ActualRectangle.Value.Bottom) - y
);
return Ocr.This.GetCharBoxsSurroundedByRectangle(page.ActiveTemplateBitmap, ar, aof?.TesseractPageSegMode ?? page.PageCollection.ActiveTemplate.TesseractPageSegMode);
}
else
{
List<Ocr.CharBox> cbs = (List<Ocr.CharBox>)TableFieldActualInfo.GetValue(Template.Field.Types.OcrCharBoxs);
return cbs.Where(a => /*!check: ar.Contains(a.R)*/ a.R.Left >= ar.Left && a.R.Right <= ar.Right && a.R.Top >= ar.Top && a.R.Bottom <= ar.Bottom).ToList();
}
}
Bitmap getImage()
{
if (ActualRectangle == null)
return null;
RectangleF ar = ActualRectangle.Value;
RectangleF r;
if (ActualField.ColumnOfTable != null)
{
if (!TableFieldActualInfo.Found)
return null;
float x = ar.X > TableFieldActualInfo.ActualRectangle.Value.X ? ar.X : TableFieldActualInfo.ActualRectangle.Value.X;
float y = ar.Top > TableFieldActualInfo.ActualRectangle.Value.Top ? ar.Top : TableFieldActualInfo.ActualRectangle.Value.Top;
r = new RectangleF(
x,
y,
(ar.Right < TableFieldActualInfo.ActualRectangle.Value.Right ? ar.Right : TableFieldActualInfo.ActualRectangle.Value.Right) - x,
(ar.Bottom < TableFieldActualInfo.ActualRectangle.Value.Bottom ? ar.Bottom : TableFieldActualInfo.ActualRectangle.Value.Bottom) - y
);
}
else
r = ar;
using (Bitmap b = page.GetRectangleFromActiveTemplateBitmap(r.X / Settings.Constants.Pdf2ImageResolutionRatio, r.Y / Settings.Constants.Pdf2ImageResolutionRatio, r.Width / Settings.Constants.Pdf2ImageResolutionRatio, r.Height / Settings.Constants.Pdf2ImageResolutionRatio))
{
if (b == null)
return null;
return GetImageScaled2Pdf(b);
}
}
List<Bitmap> getOcrTextLineImages()
{
if (ActualRectangle == null)
return null;
RectangleF ar = (RectangleF)ActualRectangle;
Template.Field.Ocr aof = ActualField as Template.Field.Ocr;
List<Line<Ocr.CharBox>> ols;
float left, width;
if (ActualField.ColumnOfTable == null)
{
if (aof?.SingleFieldFromFieldImage ?? page.PageCollection.ActiveTemplate.SingleFieldFromFieldImage)
{
List<Ocr.CharBox> cs = Ocr.This.GetCharBoxsSurroundedByRectangle(page.ActiveTemplateBitmap, ar, aof?.TesseractPageSegMode ?? page.PageCollection.ActiveTemplate.TesseractPageSegMode);
if (cs == null)
return null;
ols = GetLines(cs, null, ActualField.CharFilter ?? page.PageCollection.ActiveTemplate.CharFilter);
}
else
ols = Ocr.GetLinesSurroundedByRectangle(page.ActiveTemplateOcrCharBoxs, ar, null, ActualField.CharFilter ?? page.PageCollection.ActiveTemplate.CharFilter);
if (aof?.AdjustLineBorders ?? page.PageCollection.ActiveTemplate.AdjustLineBorders)
AdjustBorders(ols, ar);
else
PadLines(ols, ActualField.LinePaddingY ?? page.PageCollection.ActiveTemplate.LinePaddingY);
left = ar.Left;
width = ar.Width;
}
else
{
if (!TableFieldActualInfo.Found)
return null;
List<Ocr.CharBox> cbs = (List<Ocr.CharBox>)TableFieldActualInfo.GetValue(Template.Field.Types.OcrCharBoxs);
if (aof?.ColumnCellFromCellImage ?? page.PageCollection.ActiveTemplate.ColumnCellFromCellImage)
ols = GetLines(cbs, null, ActualField.CharFilter ?? page.PageCollection.ActiveTemplate.CharFilter);
else
ols = GetLines(cbs, null, ActualField.CharFilter ?? page.PageCollection.ActiveTemplate.CharFilter);
if (aof?.AdjustLineBorders ?? page.PageCollection.ActiveTemplate.AdjustLineBorders)
AdjustBorders(ols, TableFieldActualInfo.ActualRectangle.Value);
else
PadLines(ols, ActualField.LinePaddingY ?? page.PageCollection.ActiveTemplate.LinePaddingY);
left = ar.X > TableFieldActualInfo.ActualRectangle.Value.X ? ar.X : TableFieldActualInfo.ActualRectangle.Value.X;
width = (ar.Right < TableFieldActualInfo.ActualRectangle.Value.Right ? ar.Right : TableFieldActualInfo.ActualRectangle.Value.Right) - left;
}
List<Bitmap> ls = new List<Bitmap>();
foreach (Line<Ocr.CharBox> l in ols)
{
RectangleF r = new RectangleF(left, l.Top, width, l.Bottom - l.Top);
using (Bitmap b = page.GetRectangleFromActiveTemplateBitmap(r.X / Settings.Constants.Pdf2ImageResolutionRatio, r.Y / Settings.Constants.Pdf2ImageResolutionRatio, r.Width / Settings.Constants.Pdf2ImageResolutionRatio, r.Height / Settings.Constants.Pdf2ImageResolutionRatio))
{
ls.Add(b == null ? b : GetImageScaled2Pdf(b));
}
}
return ls;
}
internal FieldActualInfo(Page page, Template.Field actualfield, RectangleF? actualRectangle, FieldActualInfo tableFieldActualInfo)
{
this.page = page;
ActualField = actualfield;
ActualRectangle = actualRectangle;
TableFieldActualInfo = tableFieldActualInfo;
Found = ActualRectangle != null;
types2cachedValue = new HandyDictionary<Template.Field.Types, object>(
disposeValue: (object v) =>
{
if (v == null)
return;
if (v is Bitmap)
((Bitmap)v).Dispose();
else if (v is List<Bitmap>)
foreach (Bitmap b in (List<Bitmap>)v)
b?.Dispose();
}
);
}
}
RectangleF? getFieldActualRectangle(Template.Field field)
{
if (!field.IsSet())
throw new Exception("Field is not set.");
if (field.Rectangle.Width <= Settings.Constants.CoordinateDeviationMargin || field.Rectangle.Height <= Settings.Constants.CoordinateDeviationMargin)
throw new Exception("Rectangle is malformed.");
RectangleF r = field.Rectangle.GetSystemRectangleF();
if (field.LeftAnchor != null)
{
AnchorActualInfo aai = GetAnchorActualInfo(field.LeftAnchor.Id);
if (!aai.Found)
return null;
float right = r.Right;
r.X += aai.Shift.Width - field.LeftAnchor.Shift;
r.Width = right - r.X;
}
if (field.TopAnchor != null)
{
AnchorActualInfo aai = GetAnchorActualInfo(field.TopAnchor.Id);
if (!aai.Found)
return null;
float bottom = r.Bottom;
r.Y += aai.Shift.Height - field.TopAnchor.Shift;
r.Height = bottom - r.Y;
}
if (field.RightAnchor != null)
{
AnchorActualInfo aai = GetAnchorActualInfo(field.RightAnchor.Id);
if (!aai.Found)
return null;
r.Width += aai.Shift.Width - field.RightAnchor.Shift;
}
if (field.BottomAnchor != null)
{
AnchorActualInfo aai = GetAnchorActualInfo(field.BottomAnchor.Id);
if (!aai.Found)
return null;
r.Height += aai.Shift.Height - field.BottomAnchor.Shift;
}
//!!!???when all the anchors found then not null even if it is collapsed
//if (r.Width <= 0 || r.Height <= 0)
// return null;
return r;
}
internal static Bitmap GetImageScaled2Pdf(Image image)
{
int w = (int)Math.Round(image.Width * Settings.Constants.Pdf2ImageResolutionRatio, 0);
int h = (int)Math.Round(image.Height * Settings.Constants.Pdf2ImageResolutionRatio, 0);
if (w == 0)
w = 1;
if (h == 0)
h = 1;
Bitmap b = new Bitmap(w, h, PixelFormat.Format24bppRgb);
using (var g = Graphics.FromImage(b))
{
g.CompositingMode = CompositingMode.SourceCopy;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.SmoothingMode = SmoothingMode.HighQuality;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.CompositingQuality = CompositingQuality.HighQuality;
g.DrawImage(image, 0, 0, b.Width, b.Height);
}
b.SetResolution(image.HorizontalResolution, image.VerticalResolution);
return b;
}
}
}