From 6e89550296aa810a2d6e4d6f4f524d02648799e0 Mon Sep 17 00:00:00 2001 From: Julius Kato Mutumba Date: Tue, 8 Aug 2023 01:07:29 +0300 Subject: [PATCH] Update PSPDFKitView.kt --- .../pspdfkit/flutter/pspdfkit/PSPDFKitView.kt | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/android/src/main/java/com/pspdfkit/flutter/pspdfkit/PSPDFKitView.kt b/android/src/main/java/com/pspdfkit/flutter/pspdfkit/PSPDFKitView.kt index 8b20ca19..11037d5a 100644 --- a/android/src/main/java/com/pspdfkit/flutter/pspdfkit/PSPDFKitView.kt +++ b/android/src/main/java/com/pspdfkit/flutter/pspdfkit/PSPDFKitView.kt @@ -9,6 +9,8 @@ import androidx.fragment.app.FragmentContainerView import androidx.fragment.app.commit import androidx.fragment.app.commitNow import com.pspdfkit.document.formatters.DocumentJsonFormatter +import com.pspdfkit.document.processor.PdfProcessor +import com.pspdfkit.document.processor.PdfProcessorTask import com.pspdfkit.flutter.pspdfkit.util.* import com.pspdfkit.flutter.pspdfkit.util.Preconditions.requireNotNullNotEmpty import com.pspdfkit.forms.ChoiceFormElement @@ -29,6 +31,7 @@ import io.reactivex.rxjava3.schedulers.Schedulers import org.json.JSONObject import java.io.ByteArrayOutputStream +import java.io.File internal class PSPDFKitView( val context: Context, @@ -420,6 +423,70 @@ internal class PSPDFKitView( } } + "processAnnotations" -> { + + val annotationType: String? = call.argument("type") + val action: String? = call.argument("processingMode") + val destinationPath: String? = call.argument("destinationPath") + + if (annotationType.isNullOrEmpty()) { + result.error( + "InvalidArgument", + "Annotation type argument is null or empty", null + ) + return + } + + if (destinationPath.isNullOrEmpty()) { + result.error( + "InvalidArgument", + "Destination path argument is null or empty", null + ) + return + } + + val destinationFile = File("$destinationPath") + + //create destination directory + val destinationDirectory = destinationFile.parentFile + if (destinationDirectory?.exists() != true) { + destinationDirectory?.mkdirs() + } + + if (action.isNullOrEmpty()) { + result.error("InvalidArgument", "Action argument is null or empty", null) + return + } + + val task: PdfProcessorTask = PdfProcessorTask.fromDocument(document) + if (annotationType == "all") { + when (action) { + "flatten" -> task.changeAllAnnotations(PdfProcessorTask.AnnotationProcessingMode.FLATTEN) + "remove" -> task.changeAllAnnotations(PdfProcessorTask.AnnotationProcessingMode.DELETE) + "embed" -> task.changeAllAnnotations(PdfProcessorTask.AnnotationProcessingMode.FLATTEN) + "print" -> task.changeAllAnnotations(PdfProcessorTask.AnnotationProcessingMode.PRINT) + else -> { + result.error("InvalidArgument", "Action argument is invalid", null) + return + } + } + } else { + // TODO: Handle specific annotation types + result.error("InvalidArgument", "Annotation type not supported", null) + } + // noinspection checkResult + PdfProcessor.processDocumentAsync(task, destinationFile) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe({ _: PdfProcessor.ProcessorProgress -> + + }, { throwable: Throwable -> + result.error("PdfProcessorException", throwable.message, null) + }, { + result.success(true) + }) + } + else -> result.notImplemented() } }