main.dart 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /// Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
  2. ///
  3. /// THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  4. /// AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  5. /// UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  6. /// This notice may not be removed from this file.
  7. import 'dart:io';
  8. import 'package:compdfkit_flutter/compdfkit.dart';
  9. import 'package:compdfkit_flutter/cpdf_configuration.dart';
  10. import 'package:flutter/material.dart';
  11. const String DOCUMENT_PATH = 'pdfs/PDF_Document.pdf';
  12. void main() {
  13. runApp(const MyApp());
  14. }
  15. class MyApp extends StatefulWidget {
  16. const MyApp({super.key});
  17. @override
  18. State<MyApp> createState() => _MyAppState();
  19. }
  20. class _MyAppState extends State<MyApp> {
  21. @override
  22. void initState() {
  23. super.initState();
  24. _init();
  25. }
  26. void _init() async {
  27. // Please replace it with your ComPDFKit license
  28. ComPDFKit.init(
  29. '77/0LbrlNGNUEKWCmu4/iG3ZlSv4+47zdgsGOxDX3vGx64O2BtvxJyDzgqNvAmqU8eM0G0ALjeYHtV3vrL7mNtev5BHDGmg2ye3WigSqpN8y0gOPPutXKAQyW9vM+cC81ws4sjcXt2vphQXKjRGeVnQodoe+0FbKzffywJ2DORo32GO9qJ51qIKGmXhokKOuQIDJ2eDgQDkIwUChC+yVz088AjTSDSYOe0UobLHOpkP6Ou4qbkx6pKJ+WexOqFxIb90cAQVVa02NpLJdSu8VPIDJNzuwds1y2RLVD6lgBj8Zez+CGDL4JzeYnYhQhlOZNLXJQ4ZqkN/eHcvgIlpzL2u0lH1oY17eVN2TRW5amzqOuoo5orgnAvMGFLdEwMLlC+K5dn2h1bB4RjP9ZTqgoNaGtyKiQ+FhqHLgPV+faNMUdCBlrq4FNafN5ZoZwbHn4fzUh88DO0481O/H5F0zHak/PQJR7Gu1OfN94Q2uALpL4t3i0S76cdEeJ6wRw44AH0PQikF7jWqqmAB1bqcqsgePNE97RigZwYiDA0p2AGWxhBg2+pgZD9EPOOjdtWPXK9LTAop75OQ9whjDWL1y0LTP/JhOPQIOghNPepj3VtjzSVrUbTBFktXeDGlz0NH9TnbvjZoxJRlcNN1+9x100WEfF4A2XbJZEjcpxV9tk1r9UpDZNXkuRC5cqSCOndk3WcAQjbXfQ4Bb9zwxbcvORsTU9lFmAbRS4KFSnCfN/gMqYb0QzhAKt6Wube1sAVkj4n7AvEss/0SdC9zk5m0/E/c0dDshJ3XKSLU/PaI1wbf/SnQhn+gZICJWg9lWCAi16kSStNvD+Tlg8iXYGXcUT967Gjfe/7Au1tVEU3oE60OBrEnSCSSJJt3MWbr/52CRpTtQ6bC+eZK0ijaRGZnS60G4A4sqfUpH3dRQ0juEnz0zrfyaQi4TKGCC1SzT5YPtsIEy4Stbdh3CCWoYV8SehrEkB58JHrolHhy5cVPV2RRYE30JXG5sJOlwb6wuhHVLlanJ7OE5ewEJCtZIHMkJ/rlfinOunS0G9GL2IMBwsyfOB1Cxl+yXx4V3li2ymawe');
  30. }
  31. @override
  32. Widget build(BuildContext context) {
  33. return MaterialApp(
  34. home: Scaffold(
  35. body: SafeArea(
  36. child: Center(
  37. child: ElevatedButton(
  38. onPressed: () async {
  39. showDocument(context);
  40. },
  41. child: const Text(
  42. 'Open Document',
  43. style: TextStyle(color: Colors.white),
  44. )),
  45. ))),
  46. );
  47. }
  48. void showDocument(BuildContext context) async {
  49. final bytes = await DefaultAssetBundle.of(context).load(DOCUMENT_PATH);
  50. final list = bytes.buffer.asUint8List();
  51. final tempDir = await ComPDFKit.getTemporaryDirectory();
  52. var pdfsDir = Directory('${tempDir.path}/pdfs');
  53. pdfsDir.createSync(recursive: true);
  54. final tempDocumentPath = '${tempDir.path}/$DOCUMENT_PATH';
  55. final file = File(tempDocumentPath);
  56. if (!file.existsSync()) {
  57. file.create(recursive: true);
  58. file.writeAsBytesSync(list);
  59. }
  60. var configuration = CPDFConfiguration();
  61. // How to disable functionality:
  62. // setting the default display mode when opening
  63. // configuration.modeConfig = const ModeConfig(initialViewMode: CPreviewMode.annotations);
  64. // top toolbar configuration:
  65. // android:
  66. // configuration.toolbarConfig = const ToolbarConfig(androidAvailableActions: [
  67. // ToolbarAction.thumbnail, ToolbarAction.bota,
  68. // ToolbarAction.search, ToolbarAction.menu
  69. // ],
  70. // availableMenus: [
  71. // ToolbarMenuAction.viewSettings, ToolbarMenuAction.documentInfo, ToolbarMenuAction.security,
  72. // ]);
  73. // iOS:
  74. // configuration.toolbarConfig = const ToolbarConfig(iosLeftBarAvailableActions: [
  75. // ToolbarAction.back, ToolbarAction.thumbnail
  76. // ],
  77. // iosRightBarAvailableActions: [
  78. // ToolbarAction.bota, ToolbarAction.search, ToolbarAction.menu
  79. // ],
  80. // availableMenus: [
  81. // ToolbarMenuAction.viewSettings, ToolbarMenuAction.documentInfo, ToolbarMenuAction.security,
  82. // ]);
  83. // readerview configuration
  84. // configuration.readerViewConfig = const ReaderViewConfig(linkHighlight: true, formFieldHighlight: true);
  85. ComPDFKit.openDocument(tempDocumentPath,
  86. password: '', configuration: configuration);
  87. }
  88. }