David PHAM-VAN

Implement To be signed flieds

1 # Changelog 1 # Changelog
2 2
  3 +## 3.3.0
  4 +
  5 +- Implement To be signed flieds
  6 +
3 ## 3.2.0 7 ## 3.2.0
4 8
5 - Fix documentation 9 - Fix documentation
@@ -417,9 +417,10 @@ class PdfAnnotSign extends PdfAnnotWidget { @@ -417,9 +417,10 @@ class PdfAnnotSign extends PdfAnnotWidget {
417 @override 417 @override
418 void build(PdfPage page, PdfObject object, PdfDict params) { 418 void build(PdfPage page, PdfObject object, PdfDict params) {
419 super.build(page, object, params); 419 super.build(page, object, params);
420 - assert(page.pdfDocument.sign != null); 420 + if (page.pdfDocument.sign != null) {
421 params['/V'] = page.pdfDocument.sign!.ref(); 421 params['/V'] = page.pdfDocument.sign!.ref();
422 } 422 }
  423 + }
423 } 424 }
424 425
425 enum PdfFieldFlags { 426 enum PdfFieldFlags {
@@ -25,16 +25,17 @@ enum PdfSigFlags { signaturesExist, appendOnly } @@ -25,16 +25,17 @@ enum PdfSigFlags { signaturesExist, appendOnly }
25 class PdfSignature extends PdfObjectDict { 25 class PdfSignature extends PdfObjectDict {
26 PdfSignature( 26 PdfSignature(
27 PdfDocument pdfDocument, { 27 PdfDocument pdfDocument, {
28 - required this.crypto,  
29 - Set<PdfSigFlags>? flags,  
30 - }) : flags = flags ?? const <PdfSigFlags>{PdfSigFlags.signaturesExist},  
31 - super(pdfDocument, type: '/Sig'); 28 + required this.value,
  29 + required this.flags,
  30 + }) : super(pdfDocument, type: '/Sig');
32 31
33 final Set<PdfSigFlags> flags; 32 final Set<PdfSigFlags> flags;
34 33
35 - final PdfSignatureBase crypto; 34 + final PdfSignatureBase value;
36 35
37 - int get flagsValue => flags 36 + int get flagsValue => flags.isEmpty
  37 + ? 0
  38 + : flags
38 .map<int>((PdfSigFlags e) => 1 >> e.index) 39 .map<int>((PdfSigFlags e) => 1 >> e.index)
39 .reduce((int a, int b) => a | b); 40 .reduce((int a, int b) => a | b);
40 41
@@ -43,7 +44,7 @@ class PdfSignature extends PdfObjectDict { @@ -43,7 +44,7 @@ class PdfSignature extends PdfObjectDict {
43 44
44 @override 45 @override
45 void write(PdfStream os) { 46 void write(PdfStream os) {
46 - crypto.preSign(this, params); 47 + value.preSign(this, params);
47 48
48 _offsetStart = os.offset + '$objser $objgen obj\n'.length; 49 _offsetStart = os.offset + '$objser $objgen obj\n'.length;
49 super.write(os); 50 super.write(os);
@@ -54,7 +55,7 @@ class PdfSignature extends PdfObjectDict { @@ -54,7 +55,7 @@ class PdfSignature extends PdfObjectDict {
54 assert(_offsetStart != null && _offsetEnd != null, 55 assert(_offsetStart != null && _offsetEnd != null,
55 'Must reserve the object space before signing the document'); 56 'Must reserve the object space before signing the document');
56 57
57 - await crypto.sign(this, os, params, _offsetStart, _offsetEnd); 58 + await value.sign(this, os, params, _offsetStart, _offsetEnd);
58 } 59 }
59 } 60 }
60 61
@@ -100,57 +100,6 @@ class AnnotationUrl extends AnnotationBuilder { @@ -100,57 +100,6 @@ class AnnotationUrl extends AnnotationBuilder {
100 } 100 }
101 } 101 }
102 102
103 -class AnnotationSignature extends AnnotationBuilder {  
104 - AnnotationSignature(  
105 - this.crypto, {  
106 - this.name,  
107 - this.signFlags,  
108 - this.border,  
109 - this.flags,  
110 - this.date,  
111 - this.color,  
112 - this.highlighting,  
113 - });  
114 -  
115 - final Set<PdfSigFlags>? signFlags;  
116 -  
117 - final PdfSignatureBase crypto;  
118 -  
119 - final String? name;  
120 -  
121 - final PdfBorder? border;  
122 -  
123 - final Set<PdfAnnotFlags>? flags;  
124 -  
125 - final DateTime? date;  
126 -  
127 - final PdfColor? color;  
128 -  
129 - final PdfAnnotHighlighting? highlighting;  
130 -  
131 - @override  
132 - void build(Context context, PdfRect? box) {  
133 - context.document.sign ??= PdfSignature(  
134 - context.document,  
135 - crypto: crypto,  
136 - flags: signFlags,  
137 - );  
138 -  
139 - PdfAnnot(  
140 - context.page,  
141 - PdfAnnotSign(  
142 - rect: context.localToGlobal(box!),  
143 - fieldName: name,  
144 - border: border,  
145 - flags: flags,  
146 - date: date,  
147 - color: color,  
148 - highlighting: highlighting,  
149 - ),  
150 - );  
151 - }  
152 -}  
153 -  
154 class AnnotationTextField extends AnnotationBuilder { 103 class AnnotationTextField extends AnnotationBuilder {
155 AnnotationTextField({ 104 AnnotationTextField({
156 this.name, 105 this.name,
@@ -259,31 +208,6 @@ class UrlLink extends Annotation { @@ -259,31 +208,6 @@ class UrlLink extends Annotation {
259 }) : super(child: child, builder: AnnotationUrl(destination)); 208 }) : super(child: child, builder: AnnotationUrl(destination));
260 } 209 }
261 210
262 -class Signature extends Annotation {  
263 - Signature({  
264 - required Widget child,  
265 - required PdfSignatureBase crypto,  
266 - required String name,  
267 - Set<PdfSigFlags>? signFlags,  
268 - PdfBorder? border,  
269 - Set<PdfAnnotFlags>? flags,  
270 - DateTime? date,  
271 - PdfColor? color,  
272 - PdfAnnotHighlighting? highlighting,  
273 - }) : super(  
274 - child: child,  
275 - builder: AnnotationSignature(  
276 - crypto,  
277 - signFlags: signFlags,  
278 - name: name,  
279 - border: border,  
280 - flags: flags,  
281 - date: date,  
282 - color: color,  
283 - highlighting: highlighting,  
284 - ));  
285 -}  
286 -  
287 class Outline extends Anchor { 211 class Outline extends Anchor {
288 Outline({ 212 Outline({
289 Widget? child, 213 Widget? child,
@@ -307,3 +307,94 @@ class TextField extends StatelessWidget { @@ -307,3 +307,94 @@ class TextField extends StatelessWidget {
307 PdfAnnot(context.page, tf); 307 PdfAnnot(context.page, tf);
308 } 308 }
309 } 309 }
  310 +
  311 +class Signature extends SingleChildWidget {
  312 + Signature({
  313 + Widget? child,
  314 + @Deprecated('Use value instead') PdfSignatureBase? crypto,
  315 + PdfSignatureBase? value,
  316 + required this.name,
  317 + this.appendOnly = false,
  318 + this.border,
  319 + this.flags,
  320 + this.date,
  321 + this.color,
  322 + this.highlighting,
  323 + }) : value = value ?? crypto,
  324 + super(child: child);
  325 +
  326 + /// Field name
  327 + final String name;
  328 +
  329 + /// Digital signature
  330 + final PdfSignatureBase? value;
  331 +
  332 + /// Append
  333 + final bool appendOnly;
  334 +
  335 + final PdfBorder? border;
  336 +
  337 + /// Flags for this field
  338 + final Set<PdfAnnotFlags>? flags;
  339 +
  340 + /// Date metadata
  341 + final DateTime? date;
  342 +
  343 + /// Field color
  344 + final PdfColor? color;
  345 +
  346 + /// Field highlighting
  347 + final PdfAnnotHighlighting? highlighting;
  348 +
  349 + @override
  350 + void paint(Context context) {
  351 + super.paint(context);
  352 +
  353 + if (value != null) {
  354 + context.document.sign ??= PdfSignature(
  355 + context.document,
  356 + value: value!,
  357 + flags: {
  358 + PdfSigFlags.signaturesExist,
  359 + if (appendOnly) PdfSigFlags.appendOnly,
  360 + },
  361 + );
  362 + } else {
  363 + paintChild(context);
  364 + }
  365 +
  366 + final bf = PdfAnnotSign(
  367 + rect: context.localToGlobal(box!),
  368 + fieldName: name,
  369 + border: border,
  370 + flags: flags,
  371 + date: date,
  372 + color: color,
  373 + highlighting: highlighting,
  374 + );
  375 +
  376 + if (child != null && value != null) {
  377 + final mat = context.canvas.getTransform();
  378 + final translation = Vector3(0, 0, 0);
  379 + final rotation = Quaternion(0, 0, 0, 0);
  380 + final scale = Vector3(0, 0, 0);
  381 + mat
  382 + ..decompose(translation, rotation, scale)
  383 + ..leftTranslate(-translation.x, -translation.y)
  384 + ..translate(box!.x, box!.y);
  385 +
  386 + final canvas = bf.appearance(context.document, PdfAnnotAppearance.normal,
  387 + matrix: mat);
  388 + Widget.draw(
  389 + child!,
  390 + offset: PdfPoint.zero,
  391 + canvas: canvas,
  392 + page: context.page,
  393 + constraints:
  394 + BoxConstraints.tightFor(width: box!.width, height: box!.height),
  395 + );
  396 + }
  397 +
  398 + PdfAnnot(context.page, bf);
  399 + }
  400 +}
@@ -4,7 +4,7 @@ description: A pdf producer for Dart. It can create pdf files for both web or fl @@ -4,7 +4,7 @@ description: A pdf producer for Dart. It can create pdf files for both web or fl
4 homepage: https://github.com/DavBfr/dart_pdf/tree/master/pdf 4 homepage: https://github.com/DavBfr/dart_pdf/tree/master/pdf
5 repository: https://github.com/DavBfr/dart_pdf 5 repository: https://github.com/DavBfr/dart_pdf
6 issue_tracker: https://github.com/DavBfr/dart_pdf/issues 6 issue_tracker: https://github.com/DavBfr/dart_pdf/issues
7 -version: 3.2.0 7 +version: 3.3.0
8 8
9 environment: 9 environment:
10 sdk: ">=2.12.0-0 <3.0.0" 10 sdk: ">=2.12.0-0 <3.0.0"