main.dart 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /// Copyright © 2014-2024 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:compdfkit_flutter/cpdf_options.dart';
  11. import 'package:flutter/material.dart';
  12. const String DOCUMENT_PATH = 'pdfs/PDF_Document.pdf';
  13. void main() {
  14. runApp(const MyApp());
  15. }
  16. class MyApp extends StatefulWidget {
  17. const MyApp({super.key});
  18. @override
  19. State<MyApp> createState() => _MyAppState();
  20. }
  21. class _MyAppState extends State<MyApp> {
  22. @override
  23. void initState() {
  24. super.initState();
  25. _init();
  26. }
  27. void _init() async {
  28. // Please replace it with your ComPDFKit license
  29. ComPDFKit.initialize(
  30. '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',
  31. offline: true);
  32. }
  33. @override
  34. Widget build(BuildContext context) {
  35. return MaterialApp(
  36. home: Scaffold(
  37. body: SafeArea(
  38. child: Center(
  39. child: ElevatedButton(
  40. onPressed: () async {
  41. showDocument(context);
  42. },
  43. child: const Text('Open Document')),
  44. ))),
  45. );
  46. }
  47. void showDocument(BuildContext context) async {
  48. final bytes = await DefaultAssetBundle.of(context).load(DOCUMENT_PATH);
  49. final list = bytes.buffer.asUint8List();
  50. final tempDir = await ComPDFKit.getTemporaryDirectory();
  51. var pdfsDir = Directory('${tempDir.path}/pdfs');
  52. pdfsDir.createSync(recursive: true);
  53. final tempDocumentPath = '${tempDir.path}/$DOCUMENT_PATH';
  54. final file = File(tempDocumentPath);
  55. if (!file.existsSync()) {
  56. file.create(recursive: true);
  57. file.writeAsBytesSync(list);
  58. }
  59. var configuration = CPDFConfiguration();
  60. // How to disable functionality:
  61. // setting the default display mode when opening
  62. // configuration.modeConfig = const ModeConfig(initialViewMode: CPreviewMode.digitalSignatures);
  63. // top toolbar configuration:
  64. // android:
  65. // configuration.toolbarConfig = const ToolbarConfig(androidAvailableActions: [
  66. // ToolbarAction.thumbnail, ToolbarAction.bota,
  67. // ToolbarAction.search, ToolbarAction.menu
  68. // ],
  69. // availableMenus: [
  70. // ToolbarMenuAction.viewSettings, ToolbarMenuAction.documentInfo, ToolbarMenuAction.security,
  71. // ]);
  72. // iOS:
  73. // configuration.toolbarConfig = const ToolbarConfig(iosLeftBarAvailableActions: [
  74. // ToolbarAction.back, ToolbarAction.thumbnail
  75. // ],
  76. // iosRightBarAvailableActions: [
  77. // ToolbarAction.bota, ToolbarAction.search, ToolbarAction.menu
  78. // ],
  79. // availableMenus: [
  80. // ToolbarMenuAction.viewSettings, ToolbarMenuAction.documentInfo, ToolbarMenuAction.security,
  81. // ]);
  82. // readerview configuration
  83. // configuration.readerViewConfig = const ReaderViewConfig(linkHighlight: true, formFieldHighlight: true);
  84. ComPDFKit.openDocument(tempDocumentPath,
  85. password: '', configuration: configuration);
  86. }
  87. }