advanced_pdf_viewer 0.13.0
advanced_pdf_viewer: ^0.13.0 copied to clipboard
A high-performance PDF viewer for Flutter with support for Arabic text rendering, RTL reordering, and advanced annotations (draw, text, highlight, underline) with snap-to-text.
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:advanced_pdf_viewer/advanced_pdf_viewer.dart';
import 'dart:typed_data';
import 'package:http/http.dart' as http;
void main() {
runApp(
MaterialApp(
theme: ThemeData(
useMaterial3: true,
colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
),
home: const MyApp(),
),
);
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String? _url;
Uint8List? _bytes;
final AdvancedPdfViewerController _controller = AdvancedPdfViewerController();
Future<void> _loadNetworkPdf() async {
setState(() {
_bytes = null;
_url = 'https://easy.easy-stream.net/pdfs/d3982bb2-9eec-4167-9194-dfea5082022e.pdf';
// _url ='https://easy.easy-stream.net/pdfs/7070626f-9557-4331-b152-faf7cf76a0fc.pdf';
// _url =
// 'https://easy.easy-stream.net/pdfs/c05d15da-c41b-4858-9afe-defb0a693312.pdf';
// _url= 'https://easy.easy-stream.net/pdfs/3f7d29e1-6d89-413f-ae83-f3086bafe565.pdf';
// _url= 'https://easy.easy-stream.net/pdfs/73bdf5e6-f4d6-44d3-9148-14f08a8f3219.pdf';
});
}
Future<void> _loadBytesPdf() async {
// For demo, we'll download it first and then use bytes
final response = await http.get(
Uri.parse(
'https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf',
),
);
setState(() {
_url = null;
// _bytes = response.bodyBytes;
});
}
Future<void> _savePdf() async {
final data = await _controller.savePdf();
if (data != null) {
setState(() {
_bytes = Uint8List.fromList(data);
_url = null; // Switch to displaying bytes
});
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('PDF Saved! Data length: ${data.length}')),
);
}
}
@override
Widget build(BuildContext context) {
// log("zoom is ${_controller.setZoom(scale)}");
return Scaffold(
appBar: AppBar(
title: const Text('Advanced PDF Viewer'),
actions: [
IconButton(
icon: const Icon(Icons.cloud_download),
onPressed: _loadNetworkPdf,
tooltip: 'Load Network PDF',
),
IconButton(
icon: const Icon(Icons.data_object),
onPressed: _loadBytesPdf,
tooltip: 'Load Bytes PDF',
),
if (_url != null || _bytes != null) ...[
IconButton(
icon: const Icon(Icons.save),
onPressed: _savePdf,
tooltip: 'Save PDF',
),
],
],
),
// floatingActionButton: Column(
// mainAxisAlignment: MainAxisAlignment.end,
// children: [
// FloatingActionButton(
// heroTag: 'text',
// onPressed: () async {
// await _controller.addTextAnnotation(
// "Programmatic Text",
// 100.0,
// 200.0,
// 0, // pageIndex
// color: Colors.purple,
// );
// },
// tooltip: 'Add Programmatic Text',
// child: const Icon(Icons.text_fields),
// ),
// const SizedBox(height: 10),
// FloatingActionButton(
// heroTag: 'page',
// onPressed: () async {
// final total = await _controller.getTotalPages();
// log("Total pages: $total");
// if (total > 0) {
// await _controller.jumpToPage(1);
// if (mounted) {
// ScaffoldMessenger.of(context).showSnackBar(
// SnackBar(content: Text('Jumped to last page of $total')),
// );
// }
// }
// },
// tooltip: 'Jump to Last Page',
// child: const Icon(Icons.last_page),
// ),
// ],
// ),
body: _url == null && _bytes == null
? const Center(child: Text('Pick a source to load PDF'))
: Column(
children: [
Expanded(
child: _url != null
? AdvancedPdfViewer.network(
_url!,
controller: _controller,
key: ValueKey(_url),
config: PdfViewerConfig(
showTextButton: true,
drawColor: Colors.red,
allowFullScreen: false,
showZoomButtons: true,
toolbarColor: Colors.white,
enablePageNumber: true,
language: PdfViewerLanguage.arabic,
onFullScreenInit: () {
log('full screen initialized');
},
showBookmarkButton: true,
enableBookmarks: true,
showBookmarksListButton: true,
// showToolbarSettings: false,
toolbarStyle: PdfToolbarStyle(
// activeColor: Colors.red,
// inactiveColor: Colors.black,
// useBlur: true,
// blurSigma: 100,
// backgroundColor: Colors.orange,
// elevation: 20,
),
// bookmarkStorageKey: ,
highlightColor: Color(
0x8000FF00,
), // Semi-transparent green
),
)
: AdvancedPdfViewer.bytes(
_bytes!,
controller: _controller,
key: ValueKey(_bytes.hashCode),
config: PdfViewerConfig(
showTextButton: false,
drawColor: Colors.red,
allowFullScreen: false,
showZoomButtons: false,
enablePageNumber: true,
language: PdfViewerLanguage.arabic,
onFullScreenInit: () {
print("Entered full screen!");
},
),
),
),
],
),
);
}
}