|
@@ -14,9 +14,13 @@ package com.compdfkit.flutter.compdfkit_flutter.plugin;
|
|
|
import static com.compdfkit.flutter.compdfkit_flutter.constants.CPDFConstants.ChannelMethod.CHECK_OWNER_UNLOCKED;
|
|
|
import static com.compdfkit.flutter.compdfkit_flutter.constants.CPDFConstants.ChannelMethod.CHECK_PASSWORD;
|
|
|
import static com.compdfkit.flutter.compdfkit_flutter.constants.CPDFConstants.ChannelMethod.CLOSE;
|
|
|
+import static com.compdfkit.flutter.compdfkit_flutter.constants.CPDFConstants.ChannelMethod.GET_PAGE_COUNT;
|
|
|
+import static com.compdfkit.flutter.compdfkit_flutter.constants.CPDFConstants.ChannelMethod.REMOVE_ALL_ANNOTATIONS;
|
|
|
+import static com.compdfkit.flutter.compdfkit_flutter.constants.CPDFConstants.ChannelMethod.EXPORT_ANNOTATIONS;
|
|
|
import static com.compdfkit.flutter.compdfkit_flutter.constants.CPDFConstants.ChannelMethod.GET_FILE_NAME;
|
|
|
import static com.compdfkit.flutter.compdfkit_flutter.constants.CPDFConstants.ChannelMethod.GET_PERMISSIONS;
|
|
|
import static com.compdfkit.flutter.compdfkit_flutter.constants.CPDFConstants.ChannelMethod.HAS_CHANGE;
|
|
|
+import static com.compdfkit.flutter.compdfkit_flutter.constants.CPDFConstants.ChannelMethod.IMPORT_ANNOTATIONS;
|
|
|
import static com.compdfkit.flutter.compdfkit_flutter.constants.CPDFConstants.ChannelMethod.IS_ENCRYPTED;
|
|
|
import static com.compdfkit.flutter.compdfkit_flutter.constants.CPDFConstants.ChannelMethod.IS_IMAGE_DOC;
|
|
|
import static com.compdfkit.flutter.compdfkit_flutter.constants.CPDFConstants.ChannelMethod.OPEN_DOCUMENT;
|
|
@@ -27,14 +31,16 @@ import android.text.TextUtils;
|
|
|
import androidx.annotation.NonNull;
|
|
|
import com.compdfkit.core.document.CPDFDocument;
|
|
|
import com.compdfkit.core.document.CPDFDocument.PDFDocumentError;
|
|
|
+import com.compdfkit.core.page.CPDFPage;
|
|
|
+import com.compdfkit.tools.common.utils.CFileUtils;
|
|
|
import com.compdfkit.tools.common.utils.threadpools.CThreadPoolUtils;
|
|
|
import com.compdfkit.ui.reader.CPDFReaderView;
|
|
|
-import io.flutter.Log;
|
|
|
import io.flutter.plugin.common.BinaryMessenger;
|
|
|
import io.flutter.plugin.common.MethodCall;
|
|
|
import io.flutter.plugin.common.MethodChannel.Result;
|
|
|
+import java.io.File;
|
|
|
|
|
|
-public class CPDFDocumentPlugin extends BaseMethodChannelPlugin{
|
|
|
+public class CPDFDocumentPlugin extends BaseMethodChannelPlugin {
|
|
|
|
|
|
private String documentUid;
|
|
|
|
|
@@ -58,23 +64,23 @@ public class CPDFDocumentPlugin extends BaseMethodChannelPlugin{
|
|
|
|
|
|
@Override
|
|
|
public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
|
|
|
- if (readerView == null || readerView.getPDFDocument() == null){
|
|
|
+ if (readerView == null || readerView.getPDFDocument() == null) {
|
|
|
result.error("-1", "CPDFReaderView isnull or CPDFDocument is null", null);
|
|
|
return;
|
|
|
}
|
|
|
CPDFDocument document = readerView.getPDFDocument();
|
|
|
- switch (call.method){
|
|
|
+ switch (call.method) {
|
|
|
case OPEN_DOCUMENT:
|
|
|
String filePath = call.argument("filePath");
|
|
|
String fileUri = call.argument("fileUri");
|
|
|
String openPwd = call.argument("password");
|
|
|
PDFDocumentError error;
|
|
|
- if (!TextUtils.isEmpty(filePath)){
|
|
|
+ if (!TextUtils.isEmpty(filePath)) {
|
|
|
error = document.open(filePath, openPwd);
|
|
|
} else {
|
|
|
error = document.open(Uri.parse(fileUri), openPwd);
|
|
|
}
|
|
|
- if (error == PDFDocumentError.PDFDocumentErrorSuccess){
|
|
|
+ if (error == PDFDocumentError.PDFDocumentErrorSuccess) {
|
|
|
readerView.setPDFDocument(document);
|
|
|
}
|
|
|
result.success(error.ordinal());
|
|
@@ -86,7 +92,7 @@ public class CPDFDocumentPlugin extends BaseMethodChannelPlugin{
|
|
|
result.success(document.isEncrypted());
|
|
|
break;
|
|
|
case IS_IMAGE_DOC:
|
|
|
- CThreadPoolUtils.getInstance().executeIO(()->{
|
|
|
+ CThreadPoolUtils.getInstance().executeIO(() -> {
|
|
|
boolean isImageDoc = document.isImageDoc();
|
|
|
result.success(isImageDoc);
|
|
|
});
|
|
@@ -108,7 +114,61 @@ public class CPDFDocumentPlugin extends BaseMethodChannelPlugin{
|
|
|
case HAS_CHANGE:
|
|
|
result.success(document.hasChanges());
|
|
|
break;
|
|
|
- default:break;
|
|
|
+ case IMPORT_ANNOTATIONS:
|
|
|
+ try {
|
|
|
+ String xfdfFilePath = (String) call.arguments;
|
|
|
+ File file = new File(xfdfFilePath);
|
|
|
+ if (!file.exists()) {
|
|
|
+ result.success(false);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ File cacheFile = new File(context.getCacheDir(),
|
|
|
+ CFileUtils.CACHE_FOLDER + File.separator + "importAnnotCache/"
|
|
|
+ + CFileUtils.getFileNameNoExtension(document.getFileName()));
|
|
|
+ cacheFile.mkdirs();
|
|
|
+ boolean importResult = document.importAnnotations(xfdfFilePath,
|
|
|
+ cacheFile.getAbsolutePath());
|
|
|
+ readerView.reloadPages();
|
|
|
+ result.success(importResult);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ result.success(false);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case EXPORT_ANNOTATIONS:
|
|
|
+ try {
|
|
|
+ File dirFile = new File(context.getFilesDir(), "compdfkit/annotation/export/");
|
|
|
+ dirFile.mkdirs();
|
|
|
+ String fileName = CFileUtils.getFileNameNoExtension(document.getFileName());
|
|
|
+ File cacheFile = new File(context.getCacheDir(),
|
|
|
+ CFileUtils.CACHE_FOLDER + File.separator + "exportAnnotCache/" + fileName);
|
|
|
+ cacheFile.mkdirs();
|
|
|
+ File saveFile = new File(dirFile, fileName + ".xfdf");
|
|
|
+ saveFile = CFileUtils.renameNameSuffix(saveFile);
|
|
|
+ boolean exportResult = document.exportAnnotations(saveFile.getAbsolutePath(),
|
|
|
+ cacheFile.getAbsolutePath());
|
|
|
+ if (exportResult) {
|
|
|
+ result.success(saveFile.getAbsolutePath());
|
|
|
+ } else {
|
|
|
+ result.success("");
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ result.success("");
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case REMOVE_ALL_ANNOTATIONS:
|
|
|
+ boolean deleteResult = document.removeAllAnnotations();
|
|
|
+ if (deleteResult) {
|
|
|
+ readerView.invalidateAllChildren();
|
|
|
+ }
|
|
|
+ result.success(deleteResult);
|
|
|
+ break;
|
|
|
+ case GET_PAGE_COUNT:
|
|
|
+ result.success(document.getPageCount());
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
}
|
|
|
}
|
|
|
}
|