David PHAM-VAN

Add support for existing reference objects

@@ -3,6 +3,7 @@ @@ -3,6 +3,7 @@
3 ## 3.10.8 3 ## 3.10.8
4 4
5 - Add Flutter's Logical Pixel constant 5 - Add Flutter's Logical Pixel constant
  6 +- Add support for existing reference objects
6 7
7 ## 3.10.7 8 ## 3.10.7
8 9
@@ -104,8 +104,10 @@ class PdfChoiceField extends PdfAnnotWidget { @@ -104,8 +104,10 @@ class PdfChoiceField extends PdfAnnotWidget {
104 } 104 }
105 105
106 class PdfAnnot extends PdfObject<PdfDict> { 106 class PdfAnnot extends PdfObject<PdfDict> {
107 - PdfAnnot(this.pdfPage, this.annot) 107 + PdfAnnot(this.pdfPage, this.annot, {int? objser, int objgen = 0})
108 : super(pdfPage.pdfDocument, 108 : super(pdfPage.pdfDocument,
  109 + objser: objser,
  110 + objgen: objgen,
109 params: PdfDict.values({ 111 params: PdfDict.values({
110 '/Type': const PdfName('/Annot'), 112 '/Type': const PdfName('/Annot'),
111 })) { 113 })) {
@@ -142,7 +142,10 @@ class PdfCatalog extends PdfObject<PdfDict> { @@ -142,7 +142,10 @@ class PdfCatalog extends PdfObject<PdfDict> {
142 (acroForm['/SigFlags'] as PdfNum? ?? const PdfNum(0)); 142 (acroForm['/SigFlags'] as PdfNum? ?? const PdfNum(0));
143 final fields = (acroForm['/Fields'] ??= PdfArray()) as PdfArray; 143 final fields = (acroForm['/Fields'] ??= PdfArray()) as PdfArray;
144 for (final w in widgets) { 144 for (final w in widgets) {
145 - fields.add(w.ref()); 145 + final ref = w.ref();
  146 + if (!fields.values.contains(ref)) {
  147 + fields.add(ref);
  148 + }
146 } 149 }
147 } 150 }
148 } 151 }
@@ -19,6 +19,7 @@ import 'dart:math'; @@ -19,6 +19,7 @@ import 'dart:math';
19 import 'package:vector_math/vector_math_64.dart'; 19 import 'package:vector_math/vector_math_64.dart';
20 20
21 import '../../pdf.dart'; 21 import '../../pdf.dart';
  22 +import '../pdf/format/indirect.dart';
22 import 'geometry.dart'; 23 import 'geometry.dart';
23 import 'shape.dart'; 24 import 'shape.dart';
24 import 'text_style.dart'; 25 import 'text_style.dart';
@@ -32,6 +33,7 @@ class Anchor extends SingleChildWidget { @@ -32,6 +33,7 @@ class Anchor extends SingleChildWidget {
32 this.description, 33 this.description,
33 this.zoom, 34 this.zoom,
34 this.setX = false, 35 this.setX = false,
  36 + this.replaces,
35 }) : super(child: child); 37 }) : super(child: child);
36 38
37 final String name; 39 final String name;
@@ -42,6 +44,8 @@ class Anchor extends SingleChildWidget { @@ -42,6 +44,8 @@ class Anchor extends SingleChildWidget {
42 44
43 final bool setX; 45 final bool setX;
44 46
  47 + final PdfIndirect? replaces;
  48 +
45 @override 49 @override
46 void paint(Context context) { 50 void paint(Context context) {
47 super.paint(context); 51 super.paint(context);
@@ -60,7 +64,12 @@ class Anchor extends SingleChildWidget { @@ -60,7 +64,12 @@ class Anchor extends SingleChildWidget {
60 if (description != null) { 64 if (description != null) {
61 final rb = mat.transform3(Vector3(box!.right, box!.top, 0)); 65 final rb = mat.transform3(Vector3(box!.right, box!.top, 0));
62 final iBox = PdfRect.fromLTRB(lt.x, lt.y, rb.x, rb.y); 66 final iBox = PdfRect.fromLTRB(lt.x, lt.y, rb.x, rb.y);
63 - PdfAnnot(context.page, PdfAnnotText(rect: iBox, content: description!)); 67 + PdfAnnot(
  68 + context.page,
  69 + PdfAnnotText(rect: iBox, content: description!),
  70 + objser: replaces?.ser,
  71 + objgen: replaces?.gen ?? 0,
  72 + );
64 } 73 }
65 } 74 }
66 } 75 }
@@ -70,10 +79,12 @@ abstract class AnnotationBuilder { @@ -70,10 +79,12 @@ abstract class AnnotationBuilder {
70 } 79 }
71 80
72 class AnnotationLink extends AnnotationBuilder { 81 class AnnotationLink extends AnnotationBuilder {
73 - AnnotationLink(this.destination); 82 + AnnotationLink(this.destination, {this.replaces});
74 83
75 final String destination; 84 final String destination;
76 85
  86 + final PdfIndirect? replaces;
  87 +
77 @override 88 @override
78 PdfAnnot build(Context context, PdfRect? box) { 89 PdfAnnot build(Context context, PdfRect? box) {
79 return PdfAnnot( 90 return PdfAnnot(
@@ -82,6 +93,8 @@ class AnnotationLink extends AnnotationBuilder { @@ -82,6 +93,8 @@ class AnnotationLink extends AnnotationBuilder {
82 rect: context.localToGlobal(box!), 93 rect: context.localToGlobal(box!),
83 dest: destination, 94 dest: destination,
84 ), 95 ),
  96 + objser: replaces?.ser,
  97 + objgen: replaces?.gen ?? 0,
85 ); 98 );
86 } 99 }
87 } 100 }
@@ -92,6 +105,7 @@ class AnnotationUrl extends AnnotationBuilder { @@ -92,6 +105,7 @@ class AnnotationUrl extends AnnotationBuilder {
92 this.date, 105 this.date,
93 this.subject, 106 this.subject,
94 this.author, 107 this.author,
  108 + this.replaces,
95 }); 109 });
96 110
97 final String destination; 111 final String destination;
@@ -102,6 +116,8 @@ class AnnotationUrl extends AnnotationBuilder { @@ -102,6 +116,8 @@ class AnnotationUrl extends AnnotationBuilder {
102 116
103 final String? subject; 117 final String? subject;
104 118
  119 + final PdfIndirect? replaces;
  120 +
105 @override 121 @override
106 PdfAnnot build(Context context, PdfRect? box) { 122 PdfAnnot build(Context context, PdfRect? box) {
107 return PdfAnnot( 123 return PdfAnnot(
@@ -113,6 +129,8 @@ class AnnotationUrl extends AnnotationBuilder { @@ -113,6 +129,8 @@ class AnnotationUrl extends AnnotationBuilder {
113 author: author, 129 author: author,
114 subject: subject, 130 subject: subject,
115 ), 131 ),
  132 + objser: replaces?.ser,
  133 + objgen: replaces?.gen ?? 0,
116 ); 134 );
117 } 135 }
118 } 136 }
@@ -126,6 +144,7 @@ class AnnotationSquare extends AnnotationBuilder { @@ -126,6 +144,7 @@ class AnnotationSquare extends AnnotationBuilder {
126 this.date, 144 this.date,
127 this.subject, 145 this.subject,
128 this.content, 146 this.content,
  147 + this.replaces,
129 }); 148 });
130 149
131 final PdfColor? color; 150 final PdfColor? color;
@@ -142,6 +161,8 @@ class AnnotationSquare extends AnnotationBuilder { @@ -142,6 +161,8 @@ class AnnotationSquare extends AnnotationBuilder {
142 161
143 final String? content; 162 final String? content;
144 163
  164 + final PdfIndirect? replaces;
  165 +
145 @override 166 @override
146 PdfAnnot build(Context context, PdfRect? box) { 167 PdfAnnot build(Context context, PdfRect? box) {
147 return PdfAnnot( 168 return PdfAnnot(
@@ -155,6 +176,8 @@ class AnnotationSquare extends AnnotationBuilder { @@ -155,6 +176,8 @@ class AnnotationSquare extends AnnotationBuilder {
155 author: author, 176 author: author,
156 subject: subject, 177 subject: subject,
157 ), 178 ),
  179 + objser: replaces?.ser,
  180 + objgen: replaces?.gen ?? 0,
158 ); 181 );
159 } 182 }
160 } 183 }
@@ -168,6 +191,7 @@ class AnnotationCircle extends AnnotationBuilder { @@ -168,6 +191,7 @@ class AnnotationCircle extends AnnotationBuilder {
168 this.date, 191 this.date,
169 this.subject, 192 this.subject,
170 this.content, 193 this.content,
  194 + this.replaces,
171 }); 195 });
172 196
173 final PdfColor? color; 197 final PdfColor? color;
@@ -184,6 +208,8 @@ class AnnotationCircle extends AnnotationBuilder { @@ -184,6 +208,8 @@ class AnnotationCircle extends AnnotationBuilder {
184 208
185 final String? content; 209 final String? content;
186 210
  211 + final PdfIndirect? replaces;
  212 +
187 @override 213 @override
188 PdfAnnot build(Context context, PdfRect? box) { 214 PdfAnnot build(Context context, PdfRect? box) {
189 return PdfAnnot( 215 return PdfAnnot(
@@ -197,6 +223,8 @@ class AnnotationCircle extends AnnotationBuilder { @@ -197,6 +223,8 @@ class AnnotationCircle extends AnnotationBuilder {
197 author: author, 223 author: author,
198 subject: subject, 224 subject: subject,
199 ), 225 ),
  226 + objser: replaces?.ser,
  227 + objgen: replaces?.gen ?? 0,
200 ); 228 );
201 } 229 }
202 } 230 }
@@ -211,6 +239,7 @@ class AnnotationPolygon extends AnnotationBuilder { @@ -211,6 +239,7 @@ class AnnotationPolygon extends AnnotationBuilder {
211 this.date, 239 this.date,
212 this.subject, 240 this.subject,
213 this.content, 241 this.content,
  242 + this.replaces,
214 }); 243 });
215 244
216 final List<PdfPoint> points; 245 final List<PdfPoint> points;
@@ -229,6 +258,8 @@ class AnnotationPolygon extends AnnotationBuilder { @@ -229,6 +258,8 @@ class AnnotationPolygon extends AnnotationBuilder {
229 258
230 final String? content; 259 final String? content;
231 260
  261 + final PdfIndirect? replaces;
  262 +
232 @override 263 @override
233 PdfAnnot build(Context context, PdfRect? box) { 264 PdfAnnot build(Context context, PdfRect? box) {
234 final globalPoints = 265 final globalPoints =
@@ -254,7 +285,12 @@ class AnnotationPolygon extends AnnotationBuilder { @@ -254,7 +285,12 @@ class AnnotationPolygon extends AnnotationBuilder {
254 subject: subject, 285 subject: subject,
255 ); 286 );
256 287
257 - return PdfAnnot(context.page, pdfAnnotPolygon); 288 + return PdfAnnot(
  289 + context.page,
  290 + pdfAnnotPolygon,
  291 + objser: replaces?.ser,
  292 + objgen: replaces?.gen ?? 0,
  293 + );
258 } 294 }
259 } 295 }
260 296
@@ -267,6 +303,7 @@ class AnnotationInk extends AnnotationBuilder { @@ -267,6 +303,7 @@ class AnnotationInk extends AnnotationBuilder {
267 this.date, 303 this.date,
268 this.subject, 304 this.subject,
269 this.content, 305 this.content,
  306 + this.replaces,
270 }); 307 });
271 308
272 final List<List<PdfPoint>> points; 309 final List<List<PdfPoint>> points;
@@ -283,6 +320,8 @@ class AnnotationInk extends AnnotationBuilder { @@ -283,6 +320,8 @@ class AnnotationInk extends AnnotationBuilder {
283 320
284 final String? content; 321 final String? content;
285 322
  323 + final PdfIndirect? replaces;
  324 +
286 @override 325 @override
287 PdfAnnot build(Context context, PdfRect? box) { 326 PdfAnnot build(Context context, PdfRect? box) {
288 final globalPoints = points 327 final globalPoints = points
@@ -313,7 +352,12 @@ class AnnotationInk extends AnnotationBuilder { @@ -313,7 +352,12 @@ class AnnotationInk extends AnnotationBuilder {
313 content: content, 352 content: content,
314 ); 353 );
315 354
316 - return PdfAnnot(context.page, pdfAnnotInk); 355 + return PdfAnnot(
  356 + context.page,
  357 + pdfAnnotInk,
  358 + objser: replaces?.ser,
  359 + objgen: replaces?.gen ?? 0,
  360 + );
317 } 361 }
318 } 362 }
319 363
@@ -335,6 +379,7 @@ class AnnotationTextField extends AnnotationBuilder { @@ -335,6 +379,7 @@ class AnnotationTextField extends AnnotationBuilder {
335 this.value, 379 this.value,
336 this.defaultValue, 380 this.defaultValue,
337 this.textStyle, 381 this.textStyle,
  382 + this.replaces,
338 }); 383 });
339 384
340 final String? name; 385 final String? name;
@@ -369,6 +414,8 @@ class AnnotationTextField extends AnnotationBuilder { @@ -369,6 +414,8 @@ class AnnotationTextField extends AnnotationBuilder {
369 414
370 final String? subject; 415 final String? subject;
371 416
  417 + final PdfIndirect? replaces;
  418 +
372 @override 419 @override
373 PdfAnnot build(Context context, PdfRect? box) { 420 PdfAnnot build(Context context, PdfRect? box) {
374 final _textStyle = Theme.of(context).defaultTextStyle.merge(textStyle); 421 final _textStyle = Theme.of(context).defaultTextStyle.merge(textStyle);
@@ -396,6 +443,8 @@ class AnnotationTextField extends AnnotationBuilder { @@ -396,6 +443,8 @@ class AnnotationTextField extends AnnotationBuilder {
396 fontSize: _textStyle.fontSize!, 443 fontSize: _textStyle.fontSize!,
397 textColor: _textStyle.color!, 444 textColor: _textStyle.color!,
398 ), 445 ),
  446 + objser: replaces?.ser,
  447 + objgen: replaces?.gen ?? 0,
399 ); 448 );
400 } 449 }
401 } 450 }
@@ -20,6 +20,7 @@ import 'dart:typed_data'; @@ -20,6 +20,7 @@ import 'dart:typed_data';
20 import 'package:vector_math/vector_math_64.dart'; 20 import 'package:vector_math/vector_math_64.dart';
21 21
22 import '../../pdf.dart'; 22 import '../../pdf.dart';
  23 +import '../pdf/format/indirect.dart';
23 import 'basic.dart'; 24 import 'basic.dart';
24 import 'border_radius.dart'; 25 import 'border_radius.dart';
25 import 'box_border.dart'; 26 import 'box_border.dart';
@@ -89,6 +90,7 @@ class ChoiceField extends StatelessWidget with AnnotationAppearance { @@ -89,6 +90,7 @@ class ChoiceField extends StatelessWidget with AnnotationAppearance {
89 required this.name, 90 required this.name,
90 required this.items, 91 required this.items,
91 this.value, 92 this.value,
  93 + this.replaces,
92 }); 94 });
93 final String name; 95 final String name;
94 final TextStyle? textStyle; 96 final TextStyle? textStyle;
@@ -96,6 +98,7 @@ class ChoiceField extends StatelessWidget with AnnotationAppearance { @@ -96,6 +98,7 @@ class ChoiceField extends StatelessWidget with AnnotationAppearance {
96 final double height; 98 final double height;
97 final List<String> items; 99 final List<String> items;
98 final String? value; 100 final String? value;
  101 + final PdfIndirect? replaces;
99 102
100 @override 103 @override
101 void paint(Context context) { 104 void paint(Context context) {
@@ -125,7 +128,12 @@ class ChoiceField extends StatelessWidget with AnnotationAppearance { @@ -125,7 +128,12 @@ class ChoiceField extends StatelessWidget with AnnotationAppearance {
125 ); 128 );
126 } 129 }
127 130
128 - PdfAnnot(context.page, bf); 131 + PdfAnnot(
  132 + context.page,
  133 + bf,
  134 + objser: replaces?.ser,
  135 + objgen: replaces?.gen ?? 0,
  136 + );
129 } 137 }
130 138
131 @override 139 @override
@@ -144,6 +152,7 @@ class Checkbox extends SingleChildWidget with AnnotationAppearance { @@ -144,6 +152,7 @@ class Checkbox extends SingleChildWidget with AnnotationAppearance {
144 double width = 13, 152 double width = 13,
145 double height = 13, 153 double height = 13,
146 BoxDecoration? decoration, 154 BoxDecoration? decoration,
  155 + this.replaces,
147 }) : radius = decoration?.shape == BoxShape.circle 156 }) : radius = decoration?.shape == BoxShape.circle
148 ? Radius.circular(math.max(height, width) / 2) 157 ? Radius.circular(math.max(height, width) / 2)
149 : decoration?.borderRadius?.uniform ?? Radius.zero, 158 : decoration?.borderRadius?.uniform ?? Radius.zero,
@@ -171,6 +180,8 @@ class Checkbox extends SingleChildWidget with AnnotationAppearance { @@ -171,6 +180,8 @@ class Checkbox extends SingleChildWidget with AnnotationAppearance {
171 180
172 final Radius radius; 181 final Radius radius;
173 182
  183 + final PdfIndirect? replaces;
  184 +
174 @override 185 @override
175 void paint(Context context) { 186 void paint(Context context) {
176 super.paint(context); 187 super.paint(context);
@@ -217,7 +228,12 @@ class Checkbox extends SingleChildWidget with AnnotationAppearance { @@ -217,7 +228,12 @@ class Checkbox extends SingleChildWidget with AnnotationAppearance {
217 child!, 228 child!,
218 ); 229 );
219 230
220 - PdfAnnot(context.page, bf); 231 + PdfAnnot(
  232 + context.page,
  233 + bf,
  234 + objser: replaces?.ser,
  235 + objgen: replaces?.gen ?? 0,
  236 + );
221 } 237 }
222 } 238 }
223 239
@@ -232,6 +248,7 @@ class FlatButton extends SingleChildWidget with AnnotationAppearance { @@ -232,6 +248,7 @@ class FlatButton extends SingleChildWidget with AnnotationAppearance {
232 this.flags, 248 this.flags,
233 required Widget child, 249 required Widget child,
234 required this.name, 250 required this.name,
  251 + this.replaces,
235 }) : _childDown = Container( 252 }) : _childDown = Container(
236 child: DefaultTextStyle( 253 child: DefaultTextStyle(
237 style: TextStyle(color: textColor), 254 style: TextStyle(color: textColor),
@@ -282,6 +299,8 @@ class FlatButton extends SingleChildWidget with AnnotationAppearance { @@ -282,6 +299,8 @@ class FlatButton extends SingleChildWidget with AnnotationAppearance {
282 299
283 final Set<PdfAnnotFlags>? flags; 300 final Set<PdfAnnotFlags>? flags;
284 301
  302 + final PdfIndirect? replaces;
  303 +
285 @override 304 @override
286 void paint(Context context) { 305 void paint(Context context) {
287 super.paint(context); 306 super.paint(context);
@@ -300,7 +319,12 @@ class FlatButton extends SingleChildWidget with AnnotationAppearance { @@ -300,7 +319,12 @@ class FlatButton extends SingleChildWidget with AnnotationAppearance {
300 drawAppearance(context, bf, mat, _childRollover, 319 drawAppearance(context, bf, mat, _childRollover,
301 type: PdfAnnotAppearance.rollover); 320 type: PdfAnnotAppearance.rollover);
302 321
303 - PdfAnnot(context.page, bf); 322 + PdfAnnot(
  323 + context.page,
  324 + bf,
  325 + objser: replaces?.ser,
  326 + objgen: replaces?.gen ?? 0,
  327 + );
304 } 328 }
305 } 329 }
306 330
@@ -323,6 +347,7 @@ class TextField extends StatelessWidget with AnnotationAppearance { @@ -323,6 +347,7 @@ class TextField extends StatelessWidget with AnnotationAppearance {
323 this.value, 347 this.value,
324 this.defaultValue, 348 this.defaultValue,
325 this.textStyle, 349 this.textStyle,
  350 + this.replaces,
326 }); 351 });
327 352
328 final Widget? child; 353 final Widget? child;
@@ -342,6 +367,7 @@ class TextField extends StatelessWidget with AnnotationAppearance { @@ -342,6 +367,7 @@ class TextField extends StatelessWidget with AnnotationAppearance {
342 final String? value; 367 final String? value;
343 final String? defaultValue; 368 final String? defaultValue;
344 final TextStyle? textStyle; 369 final TextStyle? textStyle;
  370 + final PdfIndirect? replaces;
345 371
346 @override 372 @override
347 Widget build(Context context) { 373 Widget build(Context context) {
@@ -386,7 +412,12 @@ class TextField extends StatelessWidget with AnnotationAppearance { @@ -386,7 +412,12 @@ class TextField extends StatelessWidget with AnnotationAppearance {
386 ); 412 );
387 } 413 }
388 414
389 - PdfAnnot(context.page, tf); 415 + PdfAnnot(
  416 + context.page,
  417 + tf,
  418 + objser: replaces?.ser,
  419 + objgen: replaces?.gen ?? 0,
  420 + );
390 } 421 }
391 } 422 }
392 423
@@ -405,6 +436,7 @@ class Signature extends SingleChildWidget with AnnotationAppearance { @@ -405,6 +436,7 @@ class Signature extends SingleChildWidget with AnnotationAppearance {
405 this.crl, 436 this.crl,
406 this.cert, 437 this.cert,
407 this.ocsp, 438 this.ocsp,
  439 + this.replaces,
408 }) : value = value ?? crypto, 440 }) : value = value ?? crypto,
409 super(child: child); 441 super(child: child);
410 442
@@ -440,6 +472,8 @@ class Signature extends SingleChildWidget with AnnotationAppearance { @@ -440,6 +472,8 @@ class Signature extends SingleChildWidget with AnnotationAppearance {
440 /// Online Certificate Status Protocol 472 /// Online Certificate Status Protocol
441 final List<Uint8List>? ocsp; 473 final List<Uint8List>? ocsp;
442 474
  475 + final PdfIndirect? replaces;
  476 +
443 @override 477 @override
444 void paint(Context context) { 478 void paint(Context context) {
445 super.paint(context); 479 super.paint(context);
@@ -475,6 +509,11 @@ class Signature extends SingleChildWidget with AnnotationAppearance { @@ -475,6 +509,11 @@ class Signature extends SingleChildWidget with AnnotationAppearance {
475 drawAppearance(context, bf, mat, child!); 509 drawAppearance(context, bf, mat, child!);
476 } 510 }
477 511
478 - PdfAnnot(context.page, bf); 512 + PdfAnnot(
  513 + context.page,
  514 + bf,
  515 + objser: replaces?.ser,
  516 + objgen: replaces?.gen ?? 0,
  517 + );
479 } 518 }
480 } 519 }