David PHAM-VAN

Fix annotations

@@ -26,7 +26,7 @@ class PdfAnnot extends PdfObject { @@ -26,7 +26,7 @@ class PdfAnnot extends PdfObject {
26 final PdfRect srcRect; 26 final PdfRect srcRect;
27 27
28 /// The text of a text annotation 28 /// The text of a text annotation
29 - final String s; 29 + final String content;
30 30
31 /// Link to the Destination page 31 /// Link to the Destination page
32 final PdfObject dest; 32 final PdfObject dest;
@@ -36,58 +36,46 @@ class PdfAnnot extends PdfObject { @@ -36,58 +36,46 @@ class PdfAnnot extends PdfObject {
36 final PdfRect destRect; 36 final PdfRect destRect;
37 37
38 /// the border for this annotation 38 /// the border for this annotation
39 - PdfBorder border; 39 + final PdfBorder border;
40 40
41 - PdfAnnot(PdfPage pdfPage, 41 + PdfAnnot._create(PdfPage pdfPage,
42 {String type, 42 {String type,
43 - this.s, 43 + this.content,
44 this.srcRect, 44 this.srcRect,
45 - this.subtype, 45 + @required this.subtype,
46 this.dest, 46 this.dest,
47 - this.destRect})  
48 - : super(pdfPage.pdfDocument, type) { 47 + this.destRect,
  48 + this.border})
  49 + : super(pdfPage.pdfDocument, type ?? '/Annot') {
49 pdfPage.annotations.add(this); 50 pdfPage.annotations.add(this);
50 } 51 }
51 52
52 - /// This is used to create an annotation.  
53 - /// @param s Subtype for this annotation  
54 - /// @param rect coordinates  
55 - factory PdfAnnot.annotation(PdfPage pdfPage, String s, PdfRect rect) =>  
56 - PdfAnnot(pdfPage, type: "/Annot", s: s, srcRect: rect);  
57 -  
58 /// Creates a text annotation 53 /// Creates a text annotation
59 /// @param rect coordinates 54 /// @param rect coordinates
60 /// @param s Text for this annotation 55 /// @param s Text for this annotation
61 - factory PdfAnnot.text(PdfPage pdfPage, PdfRect rect, String s) =>  
62 - PdfAnnot(pdfPage, type: "/Text", srcRect: rect, s: s); 56 + factory PdfAnnot.text(PdfPage pdfPage,
  57 + {@required PdfRect rect,
  58 + @required String content,
  59 + PdfBorder border}) =>
  60 + PdfAnnot._create(pdfPage,
  61 + subtype: "/Text", srcRect: rect, content: content, border: border);
63 62
64 /// Creates a link annotation 63 /// Creates a link annotation
65 /// @param srcRect coordinates 64 /// @param srcRect coordinates
66 /// @param dest Destination for this link. The page will fit the display. 65 /// @param dest Destination for this link. The page will fit the display.
67 /// @param destRect Rectangle describing what part of the page to be displayed 66 /// @param destRect Rectangle describing what part of the page to be displayed
68 /// (must be in User Coordinates) 67 /// (must be in User Coordinates)
69 - factory PdfAnnot.link(PdfPage pdfPage, PdfRect srcRect, PdfObject dest,  
70 - [PdfRect destRect]) =>  
71 - PdfAnnot(pdfPage,  
72 - type: "/Link", srcRect: srcRect, dest: dest, destRect: destRect);  
73 -  
74 - /// Sets the border for the annotation. By default, no border is defined.  
75 - ///  
76 - /// If the style is dashed, then this method uses Pdf's default dash  
77 - /// scheme {3}  
78 - ///  
79 - /// Important: the annotation must have been added to the document before  
80 - /// this is used. If the annotation was created using the methods in  
81 - /// [PdfPage], then the annotation is already in the document.  
82 - ///  
83 - /// @param style Border style solid, dashed, beveled, inset or underlined.  
84 - /// @param width Width of the border  
85 - /// @param dash Array of lengths, used for drawing the dashes. If this  
86 - /// is null, then the default of {3} is used.  
87 - void setBorder(double width,  
88 - {PdfBorderStyle style = PdfBorderStyle.solid, List<double> dash}) {  
89 - border = PdfBorder(pdfDocument, width, style: style, dash: dash);  
90 - } 68 + factory PdfAnnot.link(PdfPage pdfPage,
  69 + {@required PdfRect srcRect,
  70 + @required PdfPage dest,
  71 + PdfRect destRect,
  72 + PdfBorder border}) =>
  73 + PdfAnnot._create(pdfPage,
  74 + subtype: "/Link",
  75 + srcRect: srcRect,
  76 + dest: dest,
  77 + destRect: destRect,
  78 + border: border);
91 79
92 /// Output the annotation 80 /// Output the annotation
93 /// 81 ///
@@ -109,7 +97,7 @@ class PdfAnnot extends PdfObject { @@ -109,7 +97,7 @@ class PdfAnnot extends PdfObject {
109 97
110 // Now the annotation subtypes 98 // Now the annotation subtypes
111 if (subtype == "/Text") { 99 if (subtype == "/Text") {
112 - params["/Contents"] = PdfStream.string(s); 100 + params["/Contents"] = PdfStream.text(content);
113 } else if (subtype == "/Link") { 101 } else if (subtype == "/Link") {
114 var dests = List<PdfStream>(); 102 var dests = List<PdfStream>();
115 dests.add(dest.ref()); 103 dests.add(dest.ref());
@@ -40,9 +40,9 @@ class PDFAnnot extends PdfAnnot { @@ -40,9 +40,9 @@ class PDFAnnot extends PdfAnnot {
40 double fb, 40 double fb,
41 double fr, 41 double fr,
42 double ft}) 42 double ft})
43 - : super(pdfPage, 43 + : super._create(pdfPage,
44 type: type, 44 type: type,
45 - s: s, 45 + content: s,
46 srcRect: PdfRect.fromLTRB(l, t, r, b), 46 srcRect: PdfRect.fromLTRB(l, t, r, b),
47 subtype: subtype, 47 subtype: subtype,
48 dest: dest, 48 dest: dest,
@@ -262,8 +262,8 @@ class PDFPage extends PdfPage { @@ -262,8 +262,8 @@ class PDFPage extends PdfPage {
262 PdfAnnot addNote(String note, double x, y, w, h) { 262 PdfAnnot addNote(String note, double x, y, w, h) {
263 var xy1 = cxy(x, y + h); 263 var xy1 = cxy(x, y + h);
264 var xy2 = cxy(x + w, y); 264 var xy2 = cxy(x + w, y);
265 - PdfAnnot ob =  
266 - PdfAnnot.text(this, PdfRect.fromLTRB(xy1.x, xy1.y, xy2.x, xy2.y), note); 265 + PdfAnnot ob = PdfAnnot.text(this,
  266 + rect: PdfRect.fromLTRB(xy1.x, xy1.y, xy2.x, xy2.y), content: note);
267 return ob; 267 return ob;
268 } 268 }
269 269
@@ -289,11 +289,10 @@ class PDFPage extends PdfPage { @@ -289,11 +289,10 @@ class PDFPage extends PdfPage {
289 var xy2 = cxy(x + w, y); 289 var xy2 = cxy(x + w, y);
290 var xy3 = cxy(vx, vy + vh); 290 var xy3 = cxy(vx, vy + vh);
291 var xy4 = cxy(vx + vw, vy); 291 var xy4 = cxy(vx + vw, vy);
292 - PdfAnnot ob = PdfAnnot.link(  
293 - this,  
294 - PdfRect.fromLTRB(xy1.x, xy1.y, xy2.x, xy2.y),  
295 - dest,  
296 - PdfRect.fromLTRB(xy3.x, xy3.y, xy4.x, xy4.y)); 292 + PdfAnnot ob = PdfAnnot.link(this,
  293 + srcRect: PdfRect.fromLTRB(xy1.x, xy1.y, xy2.x, xy2.y),
  294 + dest: dest,
  295 + destRect: PdfRect.fromLTRB(xy3.x, xy3.y, xy4.x, xy4.y));
297 return ob; 296 return ob;
298 } 297 }
299 298
1 import 'dart:io'; 1 import 'dart:io';
2 -import 'dart:math';  
3 -import 'dart:typed_data';  
4 2
5 import 'package:pdf/pdf.dart'; 3 import 'package:pdf/pdf.dart';
6 import 'package:test/test.dart'; 4 import 'package:test/test.dart';
7 -import 'package:vector_math/vector_math_64.dart';  
8 5
9 void main() { 6 void main() {
10 test('Pdf', () { 7 test('Pdf', () {
11 var pdf = PdfDocument(); 8 var pdf = PdfDocument();
12 var page = PdfPage(pdf, pageFormat: const PdfPageFormat(500.0, 300.0)); 9 var page = PdfPage(pdf, pageFormat: const PdfPageFormat(500.0, 300.0));
  10 + var page1 = PdfPage(pdf, pageFormat: const PdfPageFormat(500.0, 300.0));
13 11
14 - page.annotations.add(PdfAnnot.annotation(page, "Hello", PdfRect(100.0, 100.0, 50.0, 50.0))); 12 + var g = page.getGraphics();
  13 +
  14 + PdfAnnot.text(page,
  15 + content: "Hello", rect: PdfRect(100.0, 100.0, 50.0, 50.0));
  16 +
  17 + PdfAnnot.link(page,
  18 + dest: page1, srcRect: PdfRect(100.0, 150.0, 50.0, 50.0));
  19 + g.drawRect(100.0, 150.0, 50.0, 50.0);
  20 + g.strokePath();
15 21
16 var file = File('annot.pdf'); 22 var file = File('annot.pdf');
17 file.writeAsBytesSync(pdf.save()); 23 file.writeAsBytesSync(pdf.save());