examples.dart 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright © 2014-2025 PDF Technologies, Inc. All Rights Reserved.
  3. *
  4. * THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  5. * AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  6. * UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  7. * This notice may not be removed from this file.
  8. *
  9. */
  10. import 'dart:io';
  11. import 'package:compdfkit_flutter/compdfkit.dart';
  12. import 'package:compdfkit_flutter/configuration/cpdf_configuration.dart';
  13. import 'package:compdfkit_flutter_example/cpdf_document_examples.dart';
  14. import 'package:compdfkit_flutter_example/cpdf_reader_widget_controller_example.dart';
  15. import 'package:compdfkit_flutter_example/cpdf_reader_widget_dark_theme_example.dart';
  16. import 'package:compdfkit_flutter_example/cpdf_reader_widget_security_example.dart';
  17. import 'package:compdfkit_flutter_example/utils/file_util.dart';
  18. import 'package:flutter/material.dart';
  19. import 'cpdf_reader_widget_annotations_example.dart';
  20. import 'cpdf_reader_widget_example.dart';
  21. import 'widgets/cpdf_fun_item.dart';
  22. const String _documentPath = 'pdfs/PDF_Document.pdf';
  23. List<Widget> examples(BuildContext context) => [
  24. _title(context, 'Widget Examples'),
  25. FeatureItem(
  26. title: 'Show CPDFReaderWidget',
  27. description: 'Display PDF view in flutter widget',
  28. onTap: () async {
  29. File document = await extractAsset(context, _documentPath,
  30. shouldOverwrite: false);
  31. if (context.mounted) {
  32. showCPDFReaderWidget(context, document.path);
  33. }
  34. }),
  35. FeatureItem(
  36. title: 'Select External Files',
  37. description: 'Select pdf document from system file manager',
  38. onTap: () async {
  39. String? path = await pickDocument();
  40. if (context.mounted) {
  41. showCPDFReaderWidget(context, path);
  42. }
  43. }),
  44. if (Platform.isAndroid) ...[
  45. FeatureItem(
  46. title: 'CPDFReaderWidget Dark Theme',
  47. description:
  48. 'Opens a document in night mode with a custom dark theme',
  49. onTap: () => showDarkThemeCPDFReaderWidget(context))
  50. ],
  51. FeatureItem(
  52. title: 'Widget Controller Examples',
  53. description: 'CPDFReaderWidget Controller fun example',
  54. onTap: () => showCPDFReaderWidgetTest(context)),
  55. FeatureItem(
  56. title: 'Security feature Examples',
  57. description:
  58. 'This example shows how to set passwords, watermarks, etc.',
  59. onTap: () async {
  60. File document = await extractAsset(context, _documentPath,
  61. shouldOverwrite: false);
  62. if (context.mounted) {
  63. goTo(CPDFReaderWidgetSecurityExample(documentPath: document.path),
  64. context);
  65. }
  66. }),
  67. FeatureItem(
  68. title: 'Annotations Examples',
  69. description:
  70. 'Demonstrate how to implement annotation functionality using the CPDFReaderWidget , including adding, editing, and deleting annotations.',
  71. onTap: () async {
  72. File document = await extractAsset(context, _documentPath,
  73. shouldOverwrite: false);
  74. if (context.mounted) {
  75. goTo(
  76. CPDFReaderWidgetAnnotationsExample(
  77. documentPath: document.path),
  78. context);
  79. }
  80. }),
  81. _title(context, 'Modal View Examples'),
  82. FeatureItem(
  83. title: 'Basic Example',
  84. description: 'Open sample pdf document',
  85. onTap: () => showDocument(context)),
  86. FeatureItem(
  87. title: 'Select External Files',
  88. description: 'Select pdf document from system file manager',
  89. onTap: () async {
  90. String? path = await pickDocument();
  91. if (path != null) {
  92. ComPDFKit.openDocument(path,
  93. password: '', configuration: CPDFConfiguration());
  94. }
  95. }),
  96. _title(context, 'CPDFDocument Examples'),
  97. FeatureItem(
  98. title: 'CPDFDocument Example',
  99. description:
  100. 'This example demonstrates how to use CPDFDocument independently for PDF document operations, including opening documents, importing and exporting XFDF annotation files, setting passwords, and more.',
  101. onTap: () async {
  102. goTo(const CPDFDocumentExamples(), context);
  103. }),
  104. ];
  105. void showDocument(context) async {
  106. File document = await extractAsset(context, _documentPath);
  107. ComPDFKit.openDocument(document.path,
  108. password: '', configuration: CPDFConfiguration());
  109. }
  110. Future<String?> pickDocument() async {
  111. return await ComPDFKit.pickFile();
  112. }
  113. void showCPDFReaderWidget(context, String? path) async {
  114. goTo(CPDFReaderWidgetExample(documentPath: path!), context);
  115. }
  116. void showDarkThemeCPDFReaderWidget(context) async {
  117. File document =
  118. await extractAsset(context, _documentPath, shouldOverwrite: false);
  119. goTo(CPDFDarkThemeExample(documentPath: document.path), context);
  120. }
  121. void showCPDFReaderWidgetTest(context) async {
  122. File document =
  123. await extractAsset(context, _documentPath, shouldOverwrite: false);
  124. goTo(CPDFReaderWidgetControllerExample(documentPath: document.path), context);
  125. }
  126. void goTo(Widget widget, BuildContext context) =>
  127. Navigator.push(context, MaterialPageRoute(builder: (context) {
  128. return widget;
  129. }));
  130. Widget _title(BuildContext context, String title) {
  131. return Padding(
  132. padding: const EdgeInsets.only(top: 8, bottom: 8),
  133. child: Text(
  134. title,
  135. style: Theme.of(context)
  136. .textTheme
  137. .bodyLarge
  138. ?.copyWith(fontWeight: FontWeight.w500),
  139. ));
  140. }