stamp_util.dart 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import 'dart:convert';
  2. import 'dart:io';
  3. import 'dart:ui' as ui;
  4. import 'package:flutter/material.dart';
  5. import 'package:flutter/rendering.dart';
  6. import 'package:kmpdfkit_demo/widgets/models/text_stamp_bean.dart';
  7. import 'package:path/path.dart';
  8. import 'package:path_provider/path_provider.dart';
  9. import 'image_util.dart';
  10. /// stamp_util.dart
  11. ///
  12. /// Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
  13. ///
  14. /// THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  15. /// AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  16. /// UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  17. /// This notice may not be removed from this file.
  18. class StampUtil {
  19. static Future<bool> saveTextStampData(
  20. GlobalKey globalKey, TextStampBean bean) async {
  21. RenderRepaintBoundary? boundary =
  22. globalKey.currentContext?.findRenderObject() as RenderRepaintBoundary?;
  23. ui.Image? image = await boundary?.toImage(pixelRatio: 1.0);
  24. final directory = await getApplicationDocumentsDirectory();
  25. int millisecond = DateTime.now().microsecondsSinceEpoch;
  26. String directoryPath = '${directory.path}/pdfview/stamp/$millisecond';
  27. Directory folder = Directory(directoryPath);
  28. folder.createSync(recursive: true);
  29. Future<String?> path = ImageUtil.saveByteDataImage(
  30. image, folder.path, 'text_stamp_preview_$millisecond.png');
  31. File file = File('${folder.path}/data.json');
  32. File resultFile = await file.writeAsString(jsonEncode(bean.toJson()));
  33. return path != null && resultFile.existsSync();
  34. }
  35. static Future<List<TextStampBean>> getCustomTextStampList() async {
  36. final directory = await getApplicationDocumentsDirectory();
  37. String path = '${directory.path}/pdfview/stamp/';
  38. final folder = Directory(path);
  39. if (!await folder.exists()) {
  40. return [];
  41. }
  42. final fileList = await folder.list().toList();
  43. final files = <TextStampBean>[];
  44. for (final entity in fileList) {
  45. if (entity is Directory) {
  46. final stampFolder = Directory(entity.path);
  47. final list = await stampFolder.list().toList();
  48. final jsonFileEntity =
  49. list.firstWhere((element) => basename(element.path) == 'data.json');
  50. final previewImageFileEntity = list.firstWhere((element) {
  51. String name = basename(element.path);
  52. String previewImageName = 'text_stamp_preview_${basename(stampFolder.path)}.png';
  53. return name == previewImageName;
  54. });
  55. if (await jsonFileEntity.exists() &&
  56. await previewImageFileEntity.exists()) {
  57. File jsonFile = File(jsonFileEntity.path);
  58. File previewImageFile = File(previewImageFileEntity.path);
  59. files.add(
  60. TextStampBean.fromJson(jsonDecode(await jsonFile.readAsString()))
  61. ..previewImagePath = previewImageFile.path);
  62. }
  63. }
  64. }
  65. return files;
  66. }
  67. static Future<void> deleteTextStamp(List<TextStampBean> list) async {
  68. for (var value in list) {
  69. if (value.previewImagePath != null) {
  70. File file = File(value.previewImagePath!);
  71. await file.parent.delete(recursive: true);
  72. }
  73. }
  74. }
  75. }