-
Notifications
You must be signed in to change notification settings - Fork 778
/
Copy pathannotations.dart
207 lines (191 loc) · 6.35 KB
/
annotations.dart
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
///Package imports
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
///Pdf import
import 'package:syncfusion_flutter_pdf/pdf.dart';
///Local imports
import '../../model/sample_view.dart';
import 'helper/save_file_mobile.dart'
if (dart.library.js_interop) 'helper/save_file_web.dart';
/// Render pdf with annotations
class AnnotationsPdf extends SampleView {
/// Creates pdf with annotations
const AnnotationsPdf(Key key) : super(key: key);
@override
_AnnotationsPdfState createState() => _AnnotationsPdfState();
}
class _AnnotationsPdfState extends SampleViewState {
_AnnotationsPdfState();
bool flatten = false;
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: model.sampleOutputCardColor,
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.fromLTRB(10, 10, 10, 10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
'This sample shows how to create annotations such as rectangle, ellipse, polygon, and line in a PDF document. ',
style: TextStyle(fontSize: 16, color: model.textColor),
),
const SizedBox(height: 10, width: 30),
Row(
children: <Widget>[
Checkbox(
value: flatten,
activeColor: model.primaryColor,
onChanged: (bool? value) {
setState(() {
flatten = value!;
});
},
),
Text(
'Flatten Annotation',
style: TextStyle(fontSize: 16, color: model.textColor),
),
],
),
const SizedBox(height: 10, width: 30),
Align(
child: TextButton(
style: ButtonStyle(
backgroundColor: WidgetStateProperty.all<Color>(
model.primaryColor,
),
padding:
model.isMobile
? null
: WidgetStateProperty.all(
const EdgeInsets.symmetric(
vertical: 15,
horizontal: 15,
),
),
),
onPressed: _generatePDF,
child: const Text(
'Generate PDF',
style: TextStyle(color: Colors.white),
),
),
),
],
),
),
),
);
}
Future<void> _generatePDF() async {
//Load the PDF document.
final PdfDocument document = PdfDocument(
inputBytes: await _readDocumentData('annotation_template.pdf'),
);
//Get the page.
final PdfPage page = document.pages[0];
//Create a line annotation.
final PdfLineAnnotation lineAnnotation = PdfLineAnnotation(
<int>[60, 710, 187, 710],
'Introduction',
color: PdfColor(0, 0, 255),
author: 'John Milton',
border: PdfAnnotationBorder(2),
setAppearance: true,
lineIntent: PdfLineIntent.lineDimension,
);
//Add the line annotation to the page.
page.annotations.add(lineAnnotation);
//Create a ellipse Annotation.
final PdfEllipseAnnotation ellipseAnnotation = PdfEllipseAnnotation(
const Rect.fromLTRB(475, 771, 549, 815),
'Page Number',
author: 'John Milton',
border: PdfAnnotationBorder(2),
color: PdfColor(255, 0, 0),
setAppearance: true,
);
//Add the ellipse annotation to the page.
page.annotations.add(ellipseAnnotation);
//Create a rectangle annotation.
final PdfRectangleAnnotation rectangleAnnotation = PdfRectangleAnnotation(
const Rect.fromLTRB(57, 250, 565, 349),
'Usage',
color: PdfColor(255, 170, 0),
border: PdfAnnotationBorder(2),
author: 'John Milton',
setAppearance: true,
);
//Add the rectangle annotation to the page.
page.annotations.add(rectangleAnnotation);
//Create a polygon annotation.
final PdfPolygonAnnotation polygonAnnotation = PdfPolygonAnnotation(
<int>[
129,
356,
486,
356,
532,
333,
486,
310,
129,
310,
83,
333,
129,
356,
],
'Chapter 1 Conceptual Overview',
color: PdfColor(255, 0, 0),
border: PdfAnnotationBorder(2),
author: 'John Milton',
setAppearance: true,
);
//Add the polygon annotation to the page.
page.annotations.add(polygonAnnotation);
//Create a text markup annotation.
final PdfTextMarkupAnnotation textMarkupAnnotation =
PdfTextMarkupAnnotation(
const Rect.fromLTWH(60, 165, 495, 45),
'Introduction',
PdfColor(255, 255, 0),
author: 'John Milton',
);
//Add the bounds collection to highlight the text on more than one line.
textMarkupAnnotation.boundsCollection = <Rect>[
const Rect.fromLTWH(251, 165, 304, 15),
const Rect.fromLTWH(60, 180, 495, 15),
const Rect.fromLTWH(60, 195, 100, 15),
];
//Add the text markup annotation to the page.
page.annotations.add(textMarkupAnnotation);
//Create a popup annotation.
final PdfPopupAnnotation popupAnnotation = PdfPopupAnnotation(
const Rect.fromLTWH(225, 371, 20, 20),
'PDF Standard',
author: 'John Milton',
color: PdfColor(255, 255, 0),
icon: PdfPopupIcon.comment,
open: true,
setAppearance: true,
);
//Add the popup annotation to the page.
page.annotations.add(popupAnnotation);
if (flatten) {
//Flatten all the annotations.
page.annotations.flattenAllAnnotations();
}
//Save and dispose the document.
final List<int> bytes = await document.save();
document.dispose();
//Launch file.
await FileSaveHelper.saveAndLaunchFile(bytes, 'Annotations.pdf');
}
Future<List<int>> _readDocumentData(String name) async {
final ByteData data = await rootBundle.load('assets/pdf/$name');
return data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
}
}