forked from PSPDFKit/pspdfkit-flutter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples.dart
More file actions
596 lines (555 loc) · 23.3 KB
/
examples.dart
File metadata and controls
596 lines (555 loc) · 23.3 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
///
/// Copyright 2024-2025 PSPDFKit GmbH. All rights reserved.
///
/// THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
/// AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE PSPDFKIT LICENSE AGREEMENT.
/// UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
/// This notice may not be removed from this file.
///
// ignore_for_file: use_build_context_synchronously
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:pspdfkit_example/models/papsdkit_example_item.dart';
import 'package:pspdfkit_example/pspdfkit_toolbar_customization.dart';
import 'instant_collaboration_web.dart';
import 'pspdfkit_annotation_preset_customisation.dart';
import 'pspdfkit_document_example.dart';
import 'pspdfkit_event_listeners_example.dart';
import 'pspdfkit_zoom_example.dart';
import 'utils/file_utils.dart';
import 'utils/platform_utils.dart';
import 'package:pspdfkit_example/pspdfkit_configuration_example.dart';
import 'package:pspdfkit_example/pspdfkit_instant_collaboration_example.dart';
import 'package:pspdfkit_example/pspdfkit_measurement_tools.dart';
import 'package:pspdfkit_example/pspdfkit_pdf_generation_example.dart';
import 'package:pspdfkit_example/pspdfkit_save_as_example.dart';
import 'package:pspdfkit_example/nutrient_annotation_flags.dart';
import 'package:pspdfkit_flutter/pspdfkit.dart';
import 'pspdfkit_basic_example.dart';
import 'pspdfkit_form_example.dart';
import 'pspdfkit_instantjson_example.dart';
import 'pspdfkit_annotations_example.dart';
import 'pspdfkit_manual_save_example.dart';
import 'pspdfkit_annotation_processing_example.dart';
import 'pspdfkit_password_example.dart';
import 'nutrient_annotation_creation_mode_example.dart';
const String _documentPath = 'PDFs/PSPDFKit.pdf';
const String _measurementsDocs = 'PDFs/Measurements.pdf';
const String _lockedDocumentPath = 'PDFs/protected.pdf';
const String _imagePath = 'PDFs/PSPDFKit_Image_Example.jpg';
const String _formPath = 'PDFs/formdoc.pdf';
const String _instantDocumentJsonPath = 'PDFs/Instant/instant-document.json';
const String _xfdfPath = 'PDFs/Instant/document.xfdf';
const String _processedDocumentPath = 'PDFs/Embedded/PSPDFKit-processed.pdf';
List<PspdfkitExampleItem> examples(BuildContext context) => [
PspdfkitExampleItem(
title: 'Basic Example',
description: 'Opens a PDF Document.',
onTap: () async {
await extractAsset(context, _documentPath).then((value) =>
goTo(PspdfkitBasicExample(documentPath: value.path), context));
},
),
if (!kIsWeb)
PspdfkitExampleItem(
title: 'Basic Example using Platform Style',
description:
'Opens a PDF Document using Material page scaffolding for Android, and Cupertino page scaffolding for iOS.',
onTap: () async {
await extractAsset(context, _documentPath).then((value) =>
goTo(PspdfkitBasicExample(documentPath: value.path), context));
},
),
PspdfkitExampleItem(
title: 'Image Document',
description: 'Opens an image document.',
onTap: () => showImage(context),
),
PspdfkitExampleItem(
title: 'Document Example',
description: 'Shows how to get document properties after loading.',
onTap: () async {
await extractAsset(context, _documentPath).then((value) => goTo(
PspdfkitDocumentExample(documentPath: value.path), context));
}),
PspdfkitExampleItem(
title: 'Dark Theme',
description: 'Opens a document in night mode with a custom dark theme.',
onTap: () => applyDarkTheme(context),
),
PspdfkitExampleItem(
title: 'Custom configuration options',
description: 'Opens a document with custom configuration options.',
onTap: () => applyCustomConfiguration(context),
),
PspdfkitExampleItem(
title: 'Opens and unlocks a password protected document',
description: 'Programmatically unlocks a password protected document.',
onTap: () => unlockPasswordProtectedDocument(context),
),
PspdfkitExampleItem(
title: 'Programmatic Form Filling Example',
description:
'Programmatically sets and gets the value of a form field using a custom Widget.',
onTap: () => showFormDocumentExample(context),
),
PspdfkitExampleItem(
title: 'Programmatically Adds and Removes Annotations',
description:
'Programmatically adds and removes annotations using a custom Widget.',
onTap: () => annotationsExample(context),
),
PspdfkitExampleItem(
title: 'Annotation Flags Example',
description: 'Shows how to click an annotation and modify its flags.',
onTap: () => annotationFlagsExample(context),
),
if (!kIsWeb)
PspdfkitExampleItem(
title: 'PDF generation',
description:
'Programmatically generate PDFs from images, templates, and HTML.',
onTap: () => pdfGenerationExample(context),
),
PspdfkitExampleItem(
title: 'Manual Save',
description:
'Add a save button at the bottom and disable automatic saving.',
onTap: () => manualSaveExample(context),
),
if (PlatformUtils.isCupertino(context))
PspdfkitExampleItem(
title: 'Save As',
description:
'Embed and save the changes made to a document into a new file',
onTap: () => saveAsExample(context),
),
if (PlatformUtils.isCupertino(context) || PlatformUtils.isAndroid())
PspdfkitExampleItem(
title: 'Process Annotations',
description:
'Programmatically adds and removes annotations using a custom Widget.',
onTap: () => annotationProcessingExample(context),
),
PspdfkitExampleItem(
title: 'Import Instant Document JSON',
description:
'Shows how to programmatically import Instant Document JSON using a custom Widget.',
onTap: () => importInstantJsonExample(context),
),
if (PlatformUtils.isCupertino(context))
PspdfkitExampleItem(
title: 'Shows two PSPDFKit Widgets simultaneously',
description:
'Opens two different PDF documents simultaneously using two PSPDFKit Widgets.',
onTap: () => pushTwoPspdfWidgetsSimultaneously(context),
),
PspdfkitExampleItem(
title: 'PSPDFKit Events Listeners',
description: 'Shows how to use PSPDFKit Events Listeners.',
onTap: () async {
await extractAsset(context, _documentPath).then((value) => goTo(
PspdfkitEventListenerExample(documentPath: value.path),
context));
}),
PspdfkitExampleItem(
title: 'Measurement tools',
description: 'Shows how to use PSPDFKit Measurement tools.',
onTap: () async {
await extractAsset(context, _measurementsDocs).then((value) => goTo(
PspdfkitMeasurementsExample(documentPath: value.path),
context));
}),
if (!kIsWeb)
PspdfkitExampleItem(
title: 'Annotations Preset Customization',
description: 'PSPDFKit Annotations Preset Customization Example.',
onTap: () async {
await extractAsset(context, _documentPath).then((value) => goTo(
PspdfkitAnnotationPresetCustomization(
documentPath: value.path),
context));
}),
PspdfkitExampleItem(
title: 'Toolbar Customization',
description: 'Shows how to customize the toolbar items.',
onTap: () async {
await extractAsset(context, _documentPath).then((value) => goTo(
PspdfkitToolbarCustomization(
documentPath: value.path,
),
context));
}),
if (kIsWeb)
PspdfkitExampleItem(
title: 'Instant collaboration Web',
description: 'Instant Synchronization Web Example',
onTap: () => goTo(const InstantCollaborationWeb(), context),
),
PspdfkitExampleItem(
title: 'Zoom to Rect',
description: 'Zoom and restore page zoom example ',
onTap: () async {
await extractAsset(context, _documentPath).then((value) =>
goTo(PspdfkitZoomExample(documentPath: value.path), context));
}),
PspdfkitExampleItem(
title: 'Annotation Creation Mode',
description: 'Shows how to use annotation creation mode.',
onTap: () async {
await extractAsset(context, _documentPath).then((value) => goTo(
NutrientAnnotationCreationModeExampleWidget(
documentPath: value.path),
context));
})
];
List<PspdfkitExampleItem> globalExamples(BuildContext context) => [
PspdfkitExampleItem(
title: 'Basic Example',
description: 'Opens a PDF Document.',
onTap: () => showDocumentGlobal(context),
),
PspdfkitExampleItem(
title: 'Image Document',
description: 'Opens an image document.',
onTap: () => showImageGlobal(context),
),
PspdfkitExampleItem(
title: 'Dark Theme',
description: 'Opens a document in night mode with a custom dark theme.',
onTap: () => applyDarkThemeGlobal(context),
),
PspdfkitExampleItem(
title: 'Custom configuration options',
description: 'Opens a document with custom configuration options.',
onTap: () => applyCustomConfigurationGlobal(context),
),
PspdfkitExampleItem(
title: 'Opens and unlocks a password protected document',
description: 'Programmatically unlocks a password protected document.',
onTap: () => unlockPasswordProtectedDocumentGlobal(context),
),
PspdfkitExampleItem(
title: 'Programmatic Form Filling Example',
description:
'Programmatically sets and gets the value of a form field.',
onTap: () => showFormDocumentExampleGlobal(context),
),
PspdfkitExampleItem(
title: 'Import Instant Document JSON',
description:
'Shows how to programmatically import Instant Document JSON.',
onTap: () => importInstantJsonExampleGlobal(context),
),
PspdfkitExampleItem(
title: 'PSPDFKit Instant',
description: 'PSPDFKit Instant Synchronization Example',
onTap: () => presentInstant(context),
),
PspdfkitExampleItem(
title: 'Measurement tools',
description: 'Shows how to use PSPDFKit Measurement tools.',
onTap: () => showMeasurementExampleGlobal(context),
),
];
void showDocument(context) async {
final extractedDocument = await extractAsset(context, _documentPath);
await Navigator.of(context).push<dynamic>(MaterialPageRoute<dynamic>(
builder: (_) =>
PspdfkitBasicExample(documentPath: extractedDocument.path)));
}
void showImage(context) async {
final extractedImage = await extractAsset(context, _imagePath);
await Navigator.of(context).push<dynamic>(MaterialPageRoute<dynamic>(
builder: (_) => Scaffold(
extendBodyBehindAppBar:
PlatformUtils.isCupertino(context) ? false : true,
appBar: AppBar(),
body: SafeArea(
top: false,
bottom: false,
child: Container(
padding: PlatformUtils.isCupertino(context)
? null
: const EdgeInsets.only(top: kToolbarHeight),
child: PspdfkitWidget(documentPath: extractedImage.path))))));
}
void applyDarkTheme(context) async {
final extractedDocument = await extractAsset(context, _documentPath);
await Navigator.of(context).push<dynamic>(MaterialPageRoute<dynamic>(
builder: (_) => Scaffold(
extendBodyBehindAppBar:
PlatformUtils.isCupertino(context) ? false : true,
appBar: AppBar(),
body: SafeArea(
top: false,
bottom: false,
child: Container(
padding: PlatformUtils.isCupertino(context)
? null
: const EdgeInsets.only(top: kToolbarHeight),
child: PspdfkitWidget(
documentPath: extractedDocument.path,
configuration: PdfConfiguration(
appearanceMode: PspdfkitAppearanceMode.night,
androidDarkThemeResource:
'PSPDFKit.Theme.Example.Dark')))))));
}
void applyCustomConfiguration(context) async {
final extractedDocument = await extractAsset(context, _documentPath);
await Navigator.of(context).push<dynamic>(MaterialPageRoute<dynamic>(
builder: (_) =>
PspdfkitConfigurationExample(documentPath: extractedDocument.path)));
}
void unlockPasswordProtectedDocument(context) async {
final extractedLockedDocument =
await extractAsset(context, _lockedDocumentPath);
await Navigator.of(context).push<dynamic>(MaterialPageRoute<dynamic>(
builder: (_) =>
PspdfkitPasswordExample(documentPath: extractedLockedDocument.path)));
}
void showFormDocumentExample(context) async {
final extractedFormDocument = await extractAsset(context, _formPath);
await Navigator.of(context).push<dynamic>(MaterialPageRoute<dynamic>(
builder: (_) =>
PspdfkitFormExampleWidget(documentPath: extractedFormDocument.path)));
}
void importInstantJsonExample(context) async {
final extractedFormDocument = await extractAsset(context, _documentPath);
await Navigator.of(context).push<dynamic>(MaterialPageRoute<dynamic>(
builder: (_) => PspdfkitInstantJsonExampleWidget(
documentPath: extractedFormDocument.path,
instantJsonPath: _instantDocumentJsonPath,
xfaPath: _xfdfPath,
)));
}
void annotationsExample(context) async {
final extractedDocument = await extractAsset(context, _documentPath);
await Navigator.of(context).push<dynamic>(MaterialPageRoute<dynamic>(
builder: (_) => PspdfkitAnnotationsExampleWidget(
documentPath: extractedDocument.path)));
}
void pdfGenerationExample(context) async {
await Navigator.of(context).push<dynamic>(MaterialPageRoute<dynamic>(
builder: (_) => const PspdfkitPDFGenerationExampleWidget()));
}
void manualSaveExample(context) async {
final extractedWritableDocument = await extractAsset(context, _documentPath,
shouldOverwrite: false, prefix: 'persist');
// Automatic Saving of documents is enabled by default in certain scenarios [see for details: https://pspdfkit.com/guides/flutter/save-a-document/#auto-save]
// In order to manually save documents, you might consider disabling automatic saving with disableAutosave: true in the config
await Navigator.of(context).push<dynamic>(MaterialPageRoute<dynamic>(
builder: (_) => PspdfkitManualSaveExampleWidget(
documentPath: extractedWritableDocument.path,
configuration: PdfConfiguration(disableAutosave: true))));
}
void saveAsExample(context) async {
final extractedWritableDocument = await extractAsset(context, _documentPath,
shouldOverwrite: false, prefix: 'persist');
// Automatic Saving of documents is enabled by default in certain scenarios [see for details: https://pspdfkit.com/guides/flutter/save-a-document/#auto-save]
// In order to manually save documents, you might consider disabling automatic saving with disableAutosave: true in the config
await Navigator.of(context).push<dynamic>(MaterialPageRoute<dynamic>(
builder: (_) => PspdfkitSaveAsExampleWidget(
documentPath: extractedWritableDocument.path,
configuration: PdfConfiguration(disableAutosave: true))));
}
void annotationProcessingExample(context) async {
final extractedDocument = await extractAsset(context, _documentPath);
await Navigator.of(context).push<dynamic>(MaterialPageRoute<dynamic>(
builder: (_) => PspdfkitAnnotationProcessingExampleWidget(
documentPath: extractedDocument.path,
exportPath: _processedDocumentPath)));
}
void pushTwoPspdfWidgetsSimultaneously(context) async {
try {
final extractedDocument = await extractAsset(context, _documentPath);
final extractedFormDocument = await extractAsset(context, _formPath);
if (PlatformUtils.isCupertino(context)) {
await Navigator.of(context).push<dynamic>(CupertinoPageRoute<dynamic>(
builder: (_) => CupertinoPageScaffold(
navigationBar: const CupertinoNavigationBar(),
child: SafeArea(
bottom: false,
child: Column(children: <Widget>[
Expanded(
child: PspdfkitWidget(
documentPath: extractedDocument.path)),
Expanded(
child: PspdfkitWidget(
documentPath: extractedFormDocument.path))
])))));
} else {
// This example is only supported in iOS at the moment.
// Support for Android is coming soon.
}
} on PlatformException catch (e) {
if (kDebugMode) {
print("Failed to present document: '${e.message}'.");
}
}
}
void showDocumentGlobal(context) async {
final extractedDocument = await extractAsset(context, _documentPath);
await Pspdfkit.present(extractedDocument.path);
}
void showImageGlobal(context) async {
final extractedImage = await extractAsset(context, _imagePath);
await Pspdfkit.present(extractedImage.path);
}
void applyDarkThemeGlobal(context) async {
final extractedDocument = await extractAsset(context, _documentPath);
await Pspdfkit.present(extractedDocument.path,
configuration: PdfConfiguration(
appearanceMode: PspdfkitAppearanceMode.night,
androidDarkThemeResource: 'PSPDFKit.Theme.Example.Dark'));
}
void applyCustomConfigurationGlobal(context) async {
final extractedDocument = await extractAsset(context, _documentPath);
await Pspdfkit.present(extractedDocument.path,
configuration: PdfConfiguration(
scrollDirection: PspdfkitScrollDirection.vertical,
pageTransition: PspdfkitPageTransition.scrollPerSpread,
spreadFitting: PspdfkitSpreadFitting.fit,
userInterfaceViewMode: PspdfkitUserInterfaceViewMode.always,
androidShowSearchAction: true,
inlineSearch: false,
showThumbnailBar: PspdfkitThumbnailBarMode.floating,
androidShowThumbnailGridAction: true,
androidShowOutlineAction: true,
androidShowAnnotationListAction: true,
showPageLabels: true,
documentLabelEnabled: false,
invertColors: false,
androidGrayScale: false,
startPage: 2,
enableAnnotationEditing: true,
enableTextSelection: false,
androidShowBookmarksAction: false,
androidEnableDocumentEditor: false,
androidShowShareAction: true,
androidShowPrintAction: false,
androidShowDocumentInfoView: true,
appearanceMode: PspdfkitAppearanceMode.defaultMode,
androidDefaultThemeResource: 'PSPDFKit.Theme.Example',
iOSRightBarButtonItems: [
'thumbnailsButtonItem',
'activityButtonItem',
'searchButtonItem',
'annotationButtonItem'
],
iOSLeftBarButtonItems: ['settingsButtonItem'],
iOSAllowToolbarTitleChange: false,
toolbarTitle: 'Custom Title',
settingsMenuItems: [
'pageTransition',
'scrollDirection',
'androidTheme',
'iOSAppearance',
'androidPageLayout',
'iOSPageMode',
'iOSSpreadFitting',
'androidScreenAwake',
'iOSBrightness'
],
showActionNavigationButtons: false,
pageLayoutMode: PspdfkitPageLayoutMode.double,
firstPageAlwaysSingle: true));
}
void unlockPasswordProtectedDocumentGlobal(context) async {
final extractedLockedDocument =
await extractAsset(context, _lockedDocumentPath);
await Pspdfkit.present(extractedLockedDocument.path,
configuration: PdfConfiguration(password: 'test123'));
}
void showFormDocumentExampleGlobal(context) async {
final formDocument = await extractAsset(context, _formPath);
await Pspdfkit.present(formDocument.path);
try {
await Pspdfkit.setFormFieldValue('Lastname', 'Name_Last');
await Pspdfkit.setFormFieldValue('0123456789', 'Telephone_Home');
await Pspdfkit.setFormFieldValue('City', 'City');
await Pspdfkit.setFormFieldValue('selected', 'Sex.0');
await Pspdfkit.setFormFieldValue('deselected', 'Sex.1');
await Pspdfkit.setFormFieldValue('selected', 'HIGH SCHOOL DIPLOMA');
} on PlatformException catch (e) {
if (kDebugMode) {
print("Failed to set form field values '${e.message}'.");
}
}
String? lastName;
try {
lastName = await Pspdfkit.getFormFieldValue('Name_Last');
} on PlatformException catch (e) {
if (kDebugMode) {
print("Failed to get form field value '${e.message}'.");
}
}
if (lastName != null) {
if (kDebugMode) {
print(
"Retrieved form field for fully qualified name 'Name_Last' is $lastName.");
}
}
}
void importInstantJsonExampleGlobal(context) async {
final extractedDocument = await extractAsset(context, _documentPath);
await Pspdfkit.present(extractedDocument.path);
// Extract a string from a file.
final annotationsJson =
await DefaultAssetBundle.of(context).loadString(_instantDocumentJsonPath);
try {
await Pspdfkit.applyInstantJson(annotationsJson);
} on PlatformException catch (e) {
if (kDebugMode) {
print("Failed to import Instant Document JSON '${e.message}'.");
}
}
}
void presentInstant(BuildContext context) async {
await Navigator.push<dynamic>(
context,
MaterialPageRoute<dynamic>(
builder: (context) => const PspdfkitInstantCollaborationExample()));
}
void measurementExample(BuildContext context) async {
await extractAsset(context, _measurementsDocs).then((value) {
Navigator.push<dynamic>(
context,
MaterialPageRoute<dynamic>(
builder: (context) => PspdfkitMeasurementsExample(
documentPath: value.path,
)));
});
}
void showMeasurementExampleGlobal(BuildContext context) {
var scale = MeasurementScale(
unitFrom: UnitFrom.inch,
valueFrom: 1.0,
unitTo: UnitTo.cm,
valueTo: 2.54);
var precision = MeasurementPrecision.fourDP;
var measurementValueConfigurations = [
MeasurementValueConfiguration(
name: 'Custom Scale', scale: scale, precision: precision)
];
extractAsset(context, _measurementsDocs).then((value) {
Pspdfkit.present(
value.path,
configuration: PdfConfiguration(
measurementValueConfigurations: measurementValueConfigurations,
),
);
});
}
void goTo(Widget widget, BuildContext context) {
Navigator.push<dynamic>(
context, MaterialPageRoute<dynamic>(builder: (context) => widget));
}
void annotationFlagsExample(BuildContext context) {
goTo(
const NutrientAnnotationFlagsExample(),
context,
);
}