Skip to content

Commit 210d79c

Browse files
author
LavanyaA
authored
Merge pull request #854 from LokeshPalani/master
Added missing files in the pdf folder
2 parents 3725a67 + 1377e97 commit 210d79c

File tree

1 file changed

+273
-0
lines changed

1 file changed

+273
-0
lines changed
Lines changed: 273 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,273 @@
1+
///Pdf import
2+
import 'dart:convert';
3+
import 'dart:typed_data';
4+
5+
import 'package:flutter/material.dart';
6+
import 'package:flutter/services.dart';
7+
import 'package:syncfusion_flutter_pdf/pdf.dart';
8+
9+
///Local imports
10+
import '../../model/sample_view.dart';
11+
import 'helper/save_file_mobile.dart'
12+
if (dart.library.html) 'helper/save_file_web.dart';
13+
14+
/// Import and export Annotation data from the PDF document.
15+
class ImportAndExportAnnotationData extends SampleView {
16+
/// Import and export Annotation data from the PDF document.
17+
const ImportAndExportAnnotationData(Key key) : super(key: key);
18+
@override
19+
_ImportAndExportAnnotationDataState createState() =>
20+
_ImportAndExportAnnotationDataState();
21+
}
22+
23+
class _ImportAndExportAnnotationDataState extends SampleViewState {
24+
_ImportAndExportAnnotationDataState();
25+
26+
int _groupDataTypeValue = 0;
27+
int _groupProcessValue = 0;
28+
String _processText = 'Import Annotation Data';
29+
PdfAnnotationDataFormat _dataFormat = PdfAnnotationDataFormat.xfdf;
30+
String _fileExtension = 'xfdf';
31+
32+
void _dataTypeChanged(int? value) {
33+
setState(() {
34+
_groupDataTypeValue = value!;
35+
switch (_groupDataTypeValue) {
36+
case 0:
37+
_dataFormat = PdfAnnotationDataFormat.xfdf;
38+
_fileExtension = 'xfdf';
39+
break;
40+
case 1:
41+
_dataFormat = PdfAnnotationDataFormat.json;
42+
_fileExtension = 'json';
43+
break;
44+
default:
45+
_dataFormat = PdfAnnotationDataFormat.fdf;
46+
_fileExtension = 'fdf';
47+
break;
48+
}
49+
});
50+
}
51+
52+
void _processChanged(int? value) {
53+
setState(() {
54+
_groupProcessValue = value!;
55+
if (_groupProcessValue == 0) {
56+
_processText = 'Import Annotation Data';
57+
} else {
58+
_processText = 'Export Annotation Data';
59+
}
60+
});
61+
}
62+
63+
@override
64+
Widget build(BuildContext context) {
65+
return Scaffold(
66+
backgroundColor: model.sampleOutputCardColor,
67+
body: SingleChildScrollView(
68+
child: Padding(
69+
padding: const EdgeInsets.fromLTRB(10, 10, 10, 10),
70+
child: Column(
71+
crossAxisAlignment: CrossAxisAlignment.start,
72+
children: <Widget>[
73+
Text(
74+
'This sample shows how to import or export annotation data from the PDF document. It supports XFDF, FDF, and JSON format for import and export.',
75+
style: TextStyle(fontSize: 16, color: model.textColor)),
76+
const SizedBox(height: 20, width: 30),
77+
Text('Choose the data type for import or export:',
78+
style: TextStyle(
79+
fontSize: 16,
80+
color: model.textColor,
81+
fontWeight: FontWeight.bold)),
82+
const SizedBox(height: 10, width: 30),
83+
if (MediaQuery.of(context).size.width > 800)
84+
Row(children: getDataTypeChildWidgets(context))
85+
else
86+
Column(children: getDataTypeChildWidgets(context)),
87+
const SizedBox(height: 20, width: 30),
88+
Text('Select import or export:',
89+
style: TextStyle(
90+
fontSize: 16,
91+
color: model.textColor,
92+
fontWeight: FontWeight.bold)),
93+
const SizedBox(height: 10, width: 30),
94+
if (MediaQuery.of(context).size.width > 800)
95+
Row(children: getProcessChildWidgets(context))
96+
else
97+
Column(children: getProcessChildWidgets(context)),
98+
const SizedBox(height: 20, width: 30),
99+
Row(
100+
mainAxisAlignment: MainAxisAlignment.center,
101+
children: <Widget>[
102+
const SizedBox(height: 10, width: 30),
103+
TextButton(
104+
style: ButtonStyle(
105+
backgroundColor:
106+
MaterialStateProperty.all<Color>(model.primaryColor),
107+
padding: model.isMobile
108+
? null
109+
: MaterialStateProperty.all(
110+
const EdgeInsets.symmetric(
111+
vertical: 15, horizontal: 15)),
112+
),
113+
onPressed: _viewTemplate,
114+
child: const Text('View Template',
115+
style: TextStyle(color: Colors.white)),
116+
),
117+
const SizedBox(
118+
height: 10,
119+
width: 20,
120+
),
121+
TextButton(
122+
style: ButtonStyle(
123+
backgroundColor:
124+
MaterialStateProperty.all<Color>(model.primaryColor),
125+
padding: model.isMobile
126+
? null
127+
: MaterialStateProperty.all(
128+
const EdgeInsets.symmetric(
129+
vertical: 15, horizontal: 15)),
130+
),
131+
onPressed: _processData,
132+
child: Text(_processText,
133+
style: const TextStyle(color: Colors.white)),
134+
)
135+
],
136+
)
137+
],
138+
),
139+
),
140+
),
141+
);
142+
}
143+
144+
List<Widget> getDataTypeChildWidgets(BuildContext context) {
145+
return <Widget>[
146+
Row(children: <Widget>[
147+
Radio<int>(
148+
value: 0,
149+
groupValue: _groupDataTypeValue,
150+
onChanged: _dataTypeChanged),
151+
Text('XFDF', style: TextStyle(fontSize: 16, color: model.textColor)),
152+
]),
153+
Row(children: <Widget>[
154+
Radio<int>(
155+
value: 1,
156+
groupValue: _groupDataTypeValue,
157+
onChanged: _dataTypeChanged),
158+
Text('JSON', style: TextStyle(fontSize: 16, color: model.textColor)),
159+
]),
160+
Row(children: <Widget>[
161+
Radio<int>(
162+
value: 2,
163+
groupValue: _groupDataTypeValue,
164+
onChanged: _dataTypeChanged),
165+
Text('FDF', style: TextStyle(fontSize: 16, color: model.textColor)),
166+
])
167+
];
168+
}
169+
170+
List<Widget> getProcessChildWidgets(BuildContext context) {
171+
return <Widget>[
172+
Row(children: <Widget>[
173+
Radio<int>(
174+
value: 0,
175+
groupValue: _groupProcessValue,
176+
onChanged: _processChanged),
177+
Text('Import', style: TextStyle(fontSize: 16, color: model.textColor)),
178+
]),
179+
Row(children: <Widget>[
180+
Radio<int>(
181+
value: 1,
182+
groupValue: _groupProcessValue,
183+
onChanged: _processChanged),
184+
Text('Export', style: TextStyle(fontSize: 16, color: model.textColor)),
185+
])
186+
];
187+
}
188+
189+
Future<void> _viewTemplate() async {
190+
final List<int> documentBytes = await _readDocumentData(
191+
_groupProcessValue == 0
192+
? 'annotation_template.pdf'
193+
: 'annotations_export_template.pdf');
194+
await FileSaveHelper.saveAndLaunchFile(
195+
documentBytes, 'annotation_template.pdf');
196+
}
197+
198+
Future<void> _processData() async {
199+
if (_groupProcessValue == 0) {
200+
await _importData();
201+
} else {
202+
await _exportData();
203+
}
204+
}
205+
206+
Future<void> _importData() async {
207+
//Read the input PDF docuemnt bytes.
208+
final List<int> inputBytes =
209+
await _readDocumentData('annotation_template.pdf');
210+
211+
//Load the PDF data
212+
final PdfDocument document = PdfDocument(inputBytes: inputBytes);
213+
214+
//Get input file name
215+
final String importFileName = 'import_annotation_data.$_fileExtension';
216+
217+
//Read the input form data to import.
218+
final List<int> importData = await _readDocumentData(importFileName);
219+
220+
//Import form data
221+
document.importAnnotation(importData, _dataFormat);
222+
223+
//Save and launch the PDF document
224+
final List<int> documentBytes = await document.save();
225+
await FileSaveHelper.saveAndLaunchFile(
226+
documentBytes, 'annotation_import.pdf');
227+
}
228+
229+
Future<void> _exportData() async {
230+
//Read the input PDF docuemnt bytes.
231+
final List<int> inputBytes =
232+
await _readDocumentData('annotations_export_template.pdf');
233+
234+
//Load the PDF data
235+
final PdfDocument document = PdfDocument(inputBytes: inputBytes);
236+
237+
//Export the form data
238+
final List<int> dataBytes = document.exportAnnotation(_dataFormat);
239+
240+
//Show the extracted form data.
241+
_showDialog(utf8.decode(dataBytes));
242+
}
243+
244+
Future<List<int>> _readDocumentData(String name) async {
245+
final ByteData data = await rootBundle.load('assets/pdf/$name');
246+
return data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
247+
}
248+
249+
void _showDialog(String text) {
250+
showDialog<Widget>(
251+
context: context,
252+
builder: (BuildContext context) {
253+
return AlertDialog(
254+
title: Text(_fileExtension.toUpperCase() + ' Data'),
255+
content: Scrollbar(
256+
child: SingleChildScrollView(
257+
physics: const BouncingScrollPhysics(
258+
parent: AlwaysScrollableScrollPhysics()),
259+
child: Text(text),
260+
),
261+
),
262+
actions: <Widget>[
263+
TextButton(
264+
onPressed: () {
265+
Navigator.of(context).pop();
266+
},
267+
child: const Text('Close'),
268+
)
269+
],
270+
);
271+
});
272+
}
273+
}

0 commit comments

Comments
 (0)