我试图突出显示 pdf 的内容并保存和存储它,以便下次用户打开该 pdf 时他/她可以访问突出显示的项目。我为此使用syncfusion_flutter_pdf 包。到目前为止,我可以突出显示内容,但无法存储和访问突出显示的内容。 我的代码是:
class HomePage extends StatefulWidget {
@override
_HomePage createState() => _HomePage();
}
class _HomePage extends State<HomePage> {
final GlobalKey<SfPdfViewerState> _pdfViewerKey = GlobalKey();
PdfViewerController controller = PdfViewerController();
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Syncfusion Flutter PDF Viewer'),
actions: <Widget>[
IconButton(
icon: const Icon(
Icons.bookmark,
color: Colors.white,
semanticLabel: 'Bookmark',
),
onPressed: () {
_pdfViewerKey.currentState?.openBookmarkView();
},
),
],
),
body: SfPdfViewer.asset(
'assets/name.pdf',
controller: controller,
onAnnotationSelected: (Annotation annotation) {
annotation.addListener(() {});
},
onAnnotationAdded: (Annotation annotation) {
annotation.color = Colors.yellow;
log('selected lines ${controller.getAnnotations()}');
},
onAnnotationDeselected: (Annotation annotation) {
annotation.color = Colors.white;
},
onAnnotationEdited: (Annotation annotation) {
annotation.color = Colors.blue;
},
onAnnotationRemoved: (Annotation annotation) {},
key: _pdfViewerKey,
),
);
}
}
我想获得存储和访问突出显示的 pdf 内容的解决方案。
您可以使用 saveDocument 方法将注释保存到文档中。重新打开同一文档时,突出显示的内容将被保留。
class HomePage extends StatefulWidget {
@override
_HomePage createState() => _HomePage();
}
class _HomePage extends State<HomePage> {
final GlobalKey<SfPdfViewerState> _pdfViewerKey = GlobalKey();
PdfViewerController controller = PdfViewerController();
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Syncfusion Flutter PDF Viewer'),
actions: <Widget>[
IconButton(
icon: const Icon(
Icons.save,
),
onPressed: _save,
),
],
),
body: FutureBuilder<File>(
future: _getPdf(),
builder: (BuildContext context, AsyncSnapshot<File> snapshot) {
if (snapshot.hasData && snapshot.data != null) {
return SfPdfViewer.file(
snapshot.data!,
controller: controller,
key: _pdfViewerKey,
onAnnotationAdded: (annotation) {
annotation.author = 'Guest';
annotation.subject = 'Text Markup';
},
);
} else {
return const Center(child: CircularProgressIndicator());
}
}),
);
}
Future<File> _getPdf() async {
final Directory dir = await getTemporaryDirectory();
final File file = File('${dir.path}/temp.pdf');
// Check if the file exists
if (file.existsSync()) {
// If file exists, return the file
return file;
} else {
// If file don't exists, create the file from asset and return it
final ByteData data = await rootBundle.load('assets/name.pdf');
final Uint8List bytes = data.buffer.asUint8List();
await file.writeAsBytes(bytes, flush: true);
return file;
}
}
Future<void> _save() async {
final Directory dir = await getTemporaryDirectory();
// Saving the file to a temporary location
final File file = File('${dir.path}/temp.pdf');
// Save annotations to the PDF document
final List<int> bytes = await controller.saveDocument();
await file.writeAsBytes(bytes, flush: true);
}
}