88///
99import 'dart:io' ;
1010import 'dart:typed_data' ;
11+ import 'dart:async' ;
1112
1213import 'package:flutter/cupertino.dart' ;
1314import 'package:flutter/material.dart' ;
@@ -19,6 +20,7 @@ const String _documentPath = 'PDFs/PSPDFKit.pdf';
1920const String _lockedDocumentPath = 'PDFs/protected.pdf' ;
2021const String _imagePath = 'PDFs/PSPDFKit Image Example.jpg' ;
2122const String _formPath = 'PDFs/Form_example.pdf' ;
23+ const String _instantDocumentJsonPath = 'PDFs/Instant/instant-document.json' ;
2224const String _pspdfkitFlutterPluginTitle = 'PSPDFKit Flutter Plugin example app' ;
2325const String _basicExample = 'Basic Example' ;
2426const String _basicExampleSub = 'Opens a PDF Document.' ;
@@ -32,6 +34,8 @@ const String _passwordProtectedDocument = 'Opens and unlocks a password protecte
3234const String _passwordProtectedDocumentSub = 'Programmatically unlocks a password protected document.' ;
3335const String _formExample = 'Programmatic Form Filling Example' ;
3436const String _formExampleSub = 'Programmatically set and get the value of a form field.' ;
37+ const String _importInstantJsonExample = 'Import Instant Document JSON' ;
38+ const String _importInstantJsonExampleSub = 'Shows how to programmatically import Instant Document JSON.' ;
3539const String _pspdfkitFor = 'PSPDFKit for' ;
3640const double _fontSize = 21.0 ;
3741
@@ -45,53 +49,40 @@ class MyApp extends StatefulWidget {
4549class _MyAppState extends State <MyApp > {
4650 String _frameworkVersion = '' ;
4751
48- void showDocument () async {
49- try {
50- final ByteData bytes = await DefaultAssetBundle .of (context).load (_documentPath);
51- final Uint8List list = bytes.buffer.asUint8List ();
52+ Future <File > extractAsset (String assetPath) async {
53+ final ByteData bytes = await DefaultAssetBundle .of (context).load (assetPath);
54+ final Uint8List list = bytes.buffer.asUint8List ();
5255
53- final tempDir = await getTemporaryDirectory ();
54- final tempDocumentPath = '${tempDir .path }/$_documentPath ' ;
56+ final Directory tempDir = await getTemporaryDirectory ();
57+ final String tempDocumentPath = '${tempDir .path }/$assetPath ' ;
5558
56- final file = await File (tempDocumentPath).create (recursive: true );
57- file.writeAsBytesSync (list);
59+ final File file = await File (tempDocumentPath).create (recursive: true );
60+ file.writeAsBytesSync (list);
61+ return file;
62+ }
5863
59- Pspdfkit .present (tempDocumentPath);
64+ void showDocument () async {
65+ try {
66+ final File extractedDocument = await extractAsset (_documentPath);
67+ Pspdfkit .present (extractedDocument.path);
6068 } on PlatformException catch (e) {
6169 print ("Failed to present document: '${e .message }'." );
6270 }
6371 }
6472
6573 void showImage () async {
6674 try {
67- final ByteData bytes =
68- await DefaultAssetBundle .of (context).load (_imagePath);
69- final Uint8List list = bytes.buffer.asUint8List ();
70-
71- final tempDir = await getTemporaryDirectory ();
72- final tempDocumentPath = '${tempDir .path }/$_imagePath ' ;
73-
74- final file = await File (tempDocumentPath).create (recursive: true );
75- file.writeAsBytesSync (list);
76-
77- Pspdfkit .present (tempDocumentPath);
75+ final File extractedImage = await extractAsset (_imagePath);
76+ Pspdfkit .present (extractedImage.path);
7877 } on PlatformException catch (e) {
79- print ("Failed to present document: '${e .message }'." );
78+ print ("Failed to present image document: '${e .message }'." );
8079 }
8180 }
8281
8382 void applyDarkTheme () async {
8483 try {
85- final ByteData bytes = await DefaultAssetBundle .of (context).load (_documentPath);
86- final Uint8List list = bytes.buffer.asUint8List ();
87-
88- final tempDir = await getTemporaryDirectory ();
89- final tempDocumentPath = '${tempDir .path }/$_documentPath ' ;
90-
91- final file = await File (tempDocumentPath).create (recursive: true );
92- file.writeAsBytesSync (list);
93-
94- Pspdfkit .present (tempDocumentPath, {
84+ final File extractedDocument = await extractAsset (_documentPath);
85+ Pspdfkit .present (extractedDocument.path, {
9586 appearanceMode: appearanceModeNight,
9687 androidDarkThemeResource: 'PSPDFKit.Theme.Example.Dark'
9788 });
@@ -102,16 +93,8 @@ class _MyAppState extends State<MyApp> {
10293
10394 void applyCustomConfiguration () async {
10495 try {
105- final ByteData bytes = await DefaultAssetBundle .of (context).load (_documentPath);
106- final Uint8List list = bytes.buffer.asUint8List ();
107-
108- final tempDir = await getTemporaryDirectory ();
109- final tempDocumentPath = '${tempDir .path }/$_documentPath ' ;
110-
111- final file = await File (tempDocumentPath).create (recursive: true );
112- file.writeAsBytesSync (list);
113-
114- Pspdfkit .present (tempDocumentPath, {
96+ final File extractedDocument = await extractAsset (_documentPath);
97+ Pspdfkit .present (extractedDocument.path, {
11598 pageScrollDirection: pageScrollDirectionVertical,
11699 pageScrollContinuous: true ,
117100 fitPageToWidth: true ,
@@ -153,16 +136,8 @@ class _MyAppState extends State<MyApp> {
153136
154137 void unlockPasswordProtectedDocument () async {
155138 try {
156- final ByteData bytes = await DefaultAssetBundle .of (context).load (_lockedDocumentPath);
157- final Uint8List list = bytes.buffer.asUint8List ();
158-
159- final tempDir = await getTemporaryDirectory ();
160- final tempDocumentPath = '${tempDir .path }/$_lockedDocumentPath ' ;
161-
162- final file = await File (tempDocumentPath).create (recursive: true );
163- file.writeAsBytesSync (list);
164-
165- Pspdfkit .present (tempDocumentPath, {
139+ final File extractedLockedDocument = await extractAsset (_lockedDocumentPath);
140+ Pspdfkit .present (extractedLockedDocument.path, {
166141 password: 'test123'
167142 });
168143 } on PlatformException catch (e) {
@@ -171,17 +146,9 @@ class _MyAppState extends State<MyApp> {
171146 }
172147
173148 void showFormDocumentExample () async {
174- final ByteData bytes = await DefaultAssetBundle .of (context).load (_formPath);
175- final Uint8List list = bytes.buffer.asUint8List ();
176-
177- final tempDir = await getTemporaryDirectory ();
178- final tempDocumentPath = '${tempDir .path }/$_formPath ' ;
179-
180- final file = await File (tempDocumentPath).create (recursive: true );
181- file.writeAsBytesSync (list);
182-
183149 try {
184- await Pspdfkit .present (tempDocumentPath);
150+ final File formDocument = await extractAsset (_formPath);
151+ await Pspdfkit .present (formDocument.path);
185152 } on PlatformException catch (e) {
186153 print ("Failed to present document: '${e .message }'." );
187154 }
@@ -209,6 +176,24 @@ class _MyAppState extends State<MyApp> {
209176 }
210177 }
211178
179+ void importInstantJsonExample () async {
180+ try {
181+ final File extractedDocument = await extractAsset (_documentPath);
182+ await Pspdfkit .present (extractedDocument.path);
183+ } on PlatformException catch (e) {
184+ print ("Failed to present document: '${e .message }'." );
185+ }
186+
187+ // Extract a string from a file.
188+ final String annotationsJson = await DefaultAssetBundle .of (context).loadString (_instantDocumentJsonPath);
189+
190+ try {
191+ Pspdfkit .applyInstantJson (annotationsJson);
192+ } on PlatformException catch (e) {
193+ print ("Failed to import Instant Document JSON '${e .message }'." );
194+ }
195+ }
196+
212197 @override
213198 initState () {
214199 super .initState ();
@@ -325,7 +310,6 @@ class _MyAppState extends State<MyApp> {
325310 themeData, frameworkVersion (), cupertinoListTiles)));
326311 } else {
327312 List <Widget > listTiles = < Widget > [
328- Divider (),
329313 ListTile (
330314 title: Text (_basicExample),
331315 subtitle: Text (_basicExampleSub),
@@ -356,6 +340,11 @@ class _MyAppState extends State<MyApp> {
356340 subtitle: Text (_formExampleSub),
357341 onTap: () => showFormDocumentExample ()),
358342 Divider (),
343+ ListTile (
344+ title: Text (_importInstantJsonExample),
345+ subtitle: Text (_importInstantJsonExampleSub),
346+ onTap: () => importInstantJsonExample ()),
347+ Divider (),
359348 ];
360349 return MaterialApp (
361350 home: Scaffold (
0 commit comments