Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Process Annotation Android Demo #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -420,6 +423,70 @@ internal class PSPDFKitView(
}
}

"processAnnotations" -> {

val annotationType: String? = call.argument<String>("type")
val action: String? = call.argument<String>("processingMode")
val destinationPath: String? = call.argument<String>("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()
}
}
Expand Down