123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- import 'dart:convert';
- import 'dart:io';
- import 'dart:ui' as ui;
- import 'package:flutter/material.dart';
- import 'package:flutter/rendering.dart';
- import 'package:kmpdfkit_demo/widgets/models/text_stamp_bean.dart';
- import 'package:path/path.dart';
- import 'package:path_provider/path_provider.dart';
- import 'image_util.dart';
- /// stamp_util.dart
- ///
- /// Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
- ///
- /// THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
- /// AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
- /// UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
- /// This notice may not be removed from this file.
- class StampUtil {
- static Future<bool> saveTextStampData(
- GlobalKey globalKey, TextStampBean bean) async {
- RenderRepaintBoundary? boundary =
- globalKey.currentContext?.findRenderObject() as RenderRepaintBoundary?;
- ui.Image? image = await boundary?.toImage(pixelRatio: 1.0);
- final directory = await getApplicationDocumentsDirectory();
- int millisecond = DateTime.now().microsecondsSinceEpoch;
- String directoryPath = '${directory.path}/pdfview/stamp/$millisecond';
- Directory folder = Directory(directoryPath);
- folder.createSync(recursive: true);
- Future<String?> path = ImageUtil.saveByteDataImage(
- image, folder.path, 'text_stamp_preview_$millisecond.png');
- File file = File('${folder.path}/data.json');
- File resultFile = await file.writeAsString(jsonEncode(bean.toJson()));
- return path != null && resultFile.existsSync();
- }
- static Future<List<TextStampBean>> getCustomTextStampList() async {
- final directory = await getApplicationDocumentsDirectory();
- String path = '${directory.path}/pdfview/stamp/';
- final folder = Directory(path);
- if (!await folder.exists()) {
- return [];
- }
- final fileList = await folder.list().toList();
- final files = <TextStampBean>[];
- for (final entity in fileList) {
- if (entity is Directory) {
- final stampFolder = Directory(entity.path);
- final list = await stampFolder.list().toList();
- final jsonFileEntity =
- list.firstWhere((element) => basename(element.path) == 'data.json');
- final previewImageFileEntity = list.firstWhere((element) {
- String name = basename(element.path);
- String previewImageName = 'text_stamp_preview_${basename(stampFolder.path)}.png';
- return name == previewImageName;
- });
- if (await jsonFileEntity.exists() &&
- await previewImageFileEntity.exists()) {
- File jsonFile = File(jsonFileEntity.path);
- File previewImageFile = File(previewImageFileEntity.path);
- files.add(
- TextStampBean.fromJson(jsonDecode(await jsonFile.readAsString()))
- ..previewImagePath = previewImageFile.path);
- }
- }
- }
- return files;
- }
- static Future<void> deleteTextStamp(List<TextStampBean> list) async {
- for (var value in list) {
- if (value.previewImagePath != null) {
- File file = File(value.previewImagePath!);
- await file.parent.delete(recursive: true);
- }
- }
- }
- }
|