annotation.dart
15.8 KB
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
/*
* Copyright (C) 2017, David PHAM-VAN <dev.nfet.net@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
part of pdf;
class PdfAnnot extends PdfObject {
PdfAnnot(this.pdfPage, this.annot)
: assert(annot != null),
super(pdfPage.pdfDocument, '/Annot') {
pdfPage.annotations.add(this);
}
/// The annotation content
final PdfAnnotBase annot;
/// The page where the annotation will display
final PdfPage pdfPage;
/// Output the annotation
@override
void _prepare() {
super._prepare();
annot.build(pdfPage, this, params);
}
}
enum PdfAnnotFlags {
/// 1
invisible,
/// 2
hidden,
/// 3
print,
/// 4
noZoom,
/// 5
noRotate,
/// 6
noView,
/// 7
readOnly,
/// 8
locked,
/// 9
toggleNoView,
/// 10
lockedContent,
}
enum PdfAnnotApparence {
normal,
rollover,
down,
}
abstract class PdfAnnotBase {
PdfAnnotBase({
@required this.subtype,
@required this.rect,
this.border,
this.content,
this.name,
this.flags,
this.date,
this.color,
}) : assert(subtype != null),
assert(rect != null);
/// The subtype of the outline, ie text, note, etc
final String subtype;
final PdfRect rect;
/// the border for this annotation
final PdfBorder border;
/// The text of a text annotation
final String content;
/// The internal name for a link
final String name;
/// Flags specifying various characteristics of the annotation
final Set<PdfAnnotFlags> flags;
/// Last modification date
final DateTime date;
/// Color
final PdfColor color;
final Map<String, PdfDataType> _appearances = <String, PdfDataType>{};
int get flagValue {
if (flags == null || flags.isEmpty) {
return 0;
}
return flags
.map<int>((PdfAnnotFlags e) => 1 << e.index)
.reduce((int a, int b) => a | b);
}
PdfGraphics appearance(
PdfDocument pdfDocument,
PdfAnnotApparence type, {
String name,
Matrix4 matrix,
PdfRect boundingBox,
}) {
final s = PdfGraphicXObject(pdfDocument, '/Form');
String n;
switch (type) {
case PdfAnnotApparence.normal:
n = '/N';
break;
case PdfAnnotApparence.rollover:
n = '/R';
break;
case PdfAnnotApparence.down:
n = '/D';
break;
}
if (name == null) {
_appearances[n] = s.ref();
} else {
if (_appearances[n] is! PdfDict) {
_appearances[n] = PdfDict();
}
final PdfDict d = _appearances[n];
d[name] = s.ref();
}
if (matrix != null) {
s.params['/Matrix'] = PdfArray.fromNum(<double>[
matrix[0],
matrix[1],
matrix[4],
matrix[5],
matrix[12],
matrix[13]
]);
}
final bbox = boundingBox ?? PdfRect.fromPoints(PdfPoint.zero, rect.size);
s.params['/BBox'] =
PdfArray.fromNum(<double>[bbox.x, bbox.y, bbox.width, bbox.height]);
final g = PdfGraphics(s, s.buf);
return g;
}
@protected
@mustCallSuper
void build(PdfPage page, PdfObject object, PdfDict params) {
params['/Subtype'] = PdfName(subtype);
params['/Rect'] = PdfArray.fromNum(
<double>[rect.left, rect.bottom, rect.right, rect.top]);
params['/P'] = page.ref();
// handle the border
if (border == null) {
params['/Border'] = PdfArray.fromNum(const <int>[0, 0, 0]);
} else {
params['/BS'] = border.ref();
}
if (content != null) {
params['/Contents'] = PdfSecString.fromString(object, content);
}
if (name != null) {
params['/NM'] = PdfSecString.fromString(object, name);
}
if (flags != null && flags.isNotEmpty) {
params['/F'] = PdfNum(flagValue);
}
if (date != null) {
params['/M'] = PdfSecString.fromDate(object, date);
}
if (color != null) {
params['/C'] = PdfColorType(color);
}
if (_appearances.isNotEmpty) {
params['/AP'] = PdfDict(_appearances);
if (_appearances['/N'] is PdfDict) {
final PdfDict n = _appearances['/N'];
params['/AS'] = PdfName(n.values.keys.first);
}
}
}
}
class PdfAnnotText extends PdfAnnotBase {
/// Create a text annotation
PdfAnnotText({
@required PdfRect rect,
@required String content,
PdfBorder border,
String name,
Set<PdfAnnotFlags> flags,
DateTime date,
PdfColor color,
}) : super(
subtype: '/Text',
rect: rect,
border: border,
content: content,
name: name,
flags: flags,
date: date,
color: color,
);
}
class PdfAnnotNamedLink extends PdfAnnotBase {
/// Create a named link annotation
PdfAnnotNamedLink({
@required PdfRect rect,
@required this.dest,
PdfBorder border,
Set<PdfAnnotFlags> flags,
DateTime date,
PdfColor color,
}) : super(
subtype: '/Link',
rect: rect,
border: border,
flags: flags,
date: date,
color: color,
);
final String dest;
@override
void build(PdfPage page, PdfObject object, PdfDict params) {
super.build(page, object, params);
params['/A'] = PdfDict(
<String, PdfDataType>{
'/S': const PdfName('/GoTo'),
'/D': PdfSecString.fromString(object, dest),
},
);
}
}
class PdfAnnotUrlLink extends PdfAnnotBase {
/// Create an url link annotation
PdfAnnotUrlLink({
@required PdfRect rect,
@required this.url,
PdfBorder border,
Set<PdfAnnotFlags> flags,
DateTime date,
PdfColor color,
}) : super(
subtype: '/Link',
rect: rect,
border: border,
flags: flags,
date: date,
color: color,
);
final String url;
@override
void build(PdfPage page, PdfObject object, PdfDict params) {
super.build(page, object, params);
params['/A'] = PdfDict(
<String, PdfDataType>{
'/S': const PdfName('/URI'),
'/URI': PdfSecString.fromString(object, url),
},
);
}
}
enum PdfAnnotHighlighting { none, invert, outline, push, toggle }
abstract class PdfAnnotWidget extends PdfAnnotBase {
/// Create a widget annotation
PdfAnnotWidget({
@required PdfRect rect,
@required this.fieldType,
this.fieldName,
PdfBorder border,
Set<PdfAnnotFlags> flags,
DateTime date,
PdfColor color,
this.backgroundColor,
this.highlighting,
}) : super(
subtype: '/Widget',
rect: rect,
border: border,
flags: flags,
date: date,
color: color,
);
final String fieldType;
final String fieldName;
final PdfAnnotHighlighting highlighting;
final PdfColor backgroundColor;
@override
void build(PdfPage page, PdfObject object, PdfDict params) {
super.build(page, object, params);
params['/FT'] = PdfName(fieldType);
if (fieldName != null) {
params['/T'] = PdfSecString.fromString(object, fieldName);
}
final mk = PdfDict();
if (color != null) {
mk.values['/BC'] = PdfColorType(color);
}
if (backgroundColor != null) {
mk.values['/BG'] = PdfColorType(backgroundColor);
}
if (mk.values.isNotEmpty) {
params['/MK'] = mk;
}
if (highlighting != null) {
switch (highlighting) {
case PdfAnnotHighlighting.none:
params['/H'] = const PdfName('/N');
break;
case PdfAnnotHighlighting.invert:
params['/H'] = const PdfName('/I');
break;
case PdfAnnotHighlighting.outline:
params['/H'] = const PdfName('/O');
break;
case PdfAnnotHighlighting.push:
params['/H'] = const PdfName('/P');
break;
case PdfAnnotHighlighting.toggle:
params['/H'] = const PdfName('/T');
break;
}
}
}
}
class PdfAnnotSign extends PdfAnnotWidget {
PdfAnnotSign({
@required PdfRect rect,
String fieldName,
PdfBorder border,
Set<PdfAnnotFlags> flags,
DateTime date,
PdfColor color,
PdfAnnotHighlighting highlighting,
}) : super(
rect: rect,
fieldType: '/Sig',
fieldName: fieldName,
border: border,
flags: flags,
date: date,
color: color,
highlighting: highlighting,
);
@override
void build(PdfPage page, PdfObject object, PdfDict params) {
super.build(page, object, params);
assert(page.pdfDocument.sign != null);
params['/V'] = page.pdfDocument.sign.ref();
}
}
enum PdfFieldFlags {
/// 1 - If set, the user may not change the value of the field.
readOnly,
/// 2 - If set, the field shall have a value at the time it is exported by
/// a submit-form action.
mandatory,
/// 3 - If set, the field shall not be exported by a submit-form action.
noExport,
/// 4
reserved4,
/// 5
reserved5,
/// 6
reserved6,
/// 7
reserved7,
/// 8
reserved8,
/// 9
reserved9,
/// 10
reserved10,
/// 11
reserved11,
/// 12
reserved12,
/// 13 - If set, the field may contain multiple lines of text; if clear,
/// the field’s text shall be restricted to a single line.
multiline,
/// 14 - If set, the field is intended for entering a secure password that
/// should not be echoed visibly to the screen. Characters typed from
/// the keyboard shall instead be echoed in some unreadable form, such
/// as asterisks or bullet characters.
password,
/// 15 - If set, exactly one radio button shall be selected at all times.
noToggleToOff,
/// 16 - If set, the field is a set of radio buttons; if clear,
/// the field is a check box.
radio,
/// 17 - If set, the field is a pushbutton that does not retain
/// a permanent value.
pushButton,
/// 18 - If set, the field is a combo box; if clear, the field is a list box.
combo,
/// 19 - If set, the combo box shall include an editable text box as well
/// as a drop-down list
edit,
/// 20 - If set, the field’s option items shall be sorted alphabetically.
sort,
/// 21 - If set, the text entered in the field represents the pathname
/// of a file whose contents shall be submitted as the value of the field.
fileSelect,
/// 22 - If set, more than one of the field’s option items may be selected
/// simultaneously
multiSelect,
/// 23 - If set, text entered in the field shall not be spell-checked.
doNotSpellCheck,
/// 24 - If set, the field shall not scroll to accommodate more text
/// than fits within its annotation rectangle.
doNotScroll,
/// 25 - If set, the field shall be automatically divided into as many
/// equally spaced positions, or combs, as the value of MaxLen,
/// and the text is laid out into those combs.
comb,
/// 26 - If set, a group of radio buttons within a radio button field
/// that use the same value for the on state will turn on and off in unison.
radiosInUnison,
/// 27 - If set, the new value shall be committed as soon as a selection
/// is made.
commitOnSelChange,
}
class PdfFormField extends PdfAnnotWidget {
PdfFormField({
@required String fieldType,
@required PdfRect rect,
String fieldName,
this.alternateName,
this.mappingName,
PdfBorder border,
Set<PdfAnnotFlags> flags,
DateTime date,
PdfColor color,
PdfColor backgroundColor,
PdfAnnotHighlighting highlighting,
this.fieldFlags,
}) : super(
rect: rect,
fieldType: fieldType,
fieldName: fieldName,
border: border,
flags: flags,
date: date,
backgroundColor: backgroundColor,
color: color,
highlighting: highlighting,
);
final String alternateName;
final String mappingName;
final Set<PdfFieldFlags> fieldFlags;
int get fieldFlagsValue {
if (fieldFlags == null || fieldFlags.isEmpty) {
return 0;
}
return fieldFlags
.map<int>((PdfFieldFlags e) => 1 << e.index)
.reduce((int a, int b) => a | b);
}
@override
void build(PdfPage page, PdfObject object, PdfDict params) {
super.build(page, object, params);
if (alternateName != null) {
params['/TU'] = PdfSecString.fromString(object, alternateName);
}
if (mappingName != null) {
params['/TM'] = PdfSecString.fromString(object, mappingName);
}
params['/Ff'] = PdfNum(fieldFlagsValue);
}
}
enum PdfTextFieldAlign { left, center, right }
class PdfTextField extends PdfFormField {
PdfTextField({
@required PdfRect rect,
String fieldName,
String alternateName,
String mappingName,
PdfBorder border,
Set<PdfAnnotFlags> flags,
DateTime date,
PdfColor color,
PdfColor backgroundColor,
PdfAnnotHighlighting highlighting,
Set<PdfFieldFlags> fieldFlags,
this.value,
this.defaultValue,
this.maxLength,
@required this.font,
@required this.fontSize,
@required this.textColor,
this.textAlign,
}) : assert(fontSize != null),
assert(textColor != null),
assert(font != null),
super(
rect: rect,
fieldType: '/Tx',
fieldName: fieldName,
border: border,
flags: flags,
date: date,
color: color,
backgroundColor: backgroundColor,
highlighting: highlighting,
alternateName: alternateName,
mappingName: mappingName,
fieldFlags: fieldFlags,
);
final int maxLength;
final String value;
final String defaultValue;
final PdfFont font;
final double fontSize;
final PdfColor textColor;
final PdfTextFieldAlign textAlign;
@override
void build(PdfPage page, PdfObject object, PdfDict params) {
super.build(page, object, params);
if (maxLength != null) {
params['/MaxLen'] = PdfNum(maxLength);
}
final buf = PdfStream();
final g = PdfGraphics(page, buf);
g.setFillColor(textColor);
g.setFont(font, fontSize);
params['/DA'] = PdfSecString.fromStream(object, buf);
if (value != null) {
params['/V'] = PdfSecString.fromString(object, value);
}
if (defaultValue != null) {
params['/DV'] = PdfSecString.fromString(object, defaultValue);
}
if (textAlign != null) {
params['/Q'] = PdfNum(textAlign.index);
}
}
}
class PdfButtonField extends PdfFormField {
PdfButtonField({
@required PdfRect rect,
String fieldName,
String alternateName,
String mappingName,
PdfBorder border,
Set<PdfAnnotFlags> flags,
DateTime date,
PdfColor color,
PdfColor backgroundColor,
PdfAnnotHighlighting highlighting,
Set<PdfFieldFlags> fieldFlags,
this.value,
this.defaultValue,
}) : super(
rect: rect,
fieldType: '/Btn',
fieldName: fieldName,
border: border,
flags: flags,
date: date,
color: color,
backgroundColor: backgroundColor,
highlighting: highlighting,
alternateName: alternateName,
mappingName: mappingName,
fieldFlags: fieldFlags,
);
final bool value;
final bool defaultValue;
@override
void build(PdfPage page, PdfObject object, PdfDict params) {
super.build(page, object, params);
if (value != null) {
params['/V'] = value ? const PdfName('/Yes') : const PdfName('/Off');
}
if (defaultValue != null) {
params['/DV'] =
defaultValue ? const PdfName('/Yes') : const PdfName('/Off');
}
}
}