-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
PSPDFKit
committed
Jun 22, 2024
1 parent
4362faf
commit a7cc9f5
Showing
42 changed files
with
1,156 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
android/src/main/java/com/pspdfkit/flutter/pspdfkit/document/FlutterPdfDocument.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.pspdfkit.flutter.pspdfkit.document | ||
|
||
import android.util.Log | ||
import com.pspdfkit.document.PdfDocument | ||
import com.pspdfkit.flutter.pspdfkit.PSPDFKitView | ||
import com.pspdfkit.flutter.pspdfkit.forms.FormHelper | ||
import io.flutter.plugin.common.BinaryMessenger | ||
import io.flutter.plugin.common.MethodCall | ||
import io.flutter.plugin.common.MethodChannel | ||
import io.flutter.plugin.common.MethodChannel.MethodCallHandler | ||
import java.io.File | ||
|
||
class FlutterPdfDocument( | ||
private val pdfDocument: PdfDocument, messenger: BinaryMessenger, | ||
) : MethodCallHandler { | ||
init { | ||
val channel = MethodChannel(messenger, "com.pspdfkit.document.${pdfDocument.uid}") | ||
channel.setMethodCallHandler(this) | ||
} | ||
|
||
override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) { | ||
when (call.method) { | ||
"getPageInfo" -> { | ||
val pageIndex = call.argument<Int>("pageIndex") ?: -1 | ||
if (pageIndex < 0 || pageIndex >= pdfDocument.pageCount) { | ||
result.error("InvalidArgument", "pageIndex is required", null) | ||
} else { | ||
try { | ||
val pageInfo = getPageInfo(pageIndex) | ||
result.success(pageInfo) | ||
} catch (e: Exception) { | ||
result.error("Error", e.message, null) | ||
} | ||
} | ||
} | ||
"getFormFields" -> { | ||
try { | ||
val formFields = pdfDocument.formProvider.formFields | ||
val formFieldsMap = FormHelper.formFieldPropertiesToMap(formFields) | ||
result.success(formFieldsMap) | ||
} catch (e: Exception) { | ||
result.error("Error", e.message, null) | ||
} | ||
} | ||
"exportPdf" -> { | ||
try { | ||
val fileUrl = pdfDocument.documentSource.fileUri?.path | ||
if (fileUrl == null) { | ||
result.error("DocumentException", "Document source is not a file", null) | ||
return | ||
} | ||
val data:ByteArray = fileUrl.let { File(it).readBytes() } | ||
result.success(data) | ||
} catch (e: Exception) { | ||
Log.e(LOG_TAG, "Error while exporting PDF", e) | ||
result.error("DocumentException", e.message, null) | ||
} | ||
} | ||
|
||
else -> { | ||
result.notImplemented() | ||
} | ||
} | ||
} | ||
|
||
private fun getPageInfo(pageIndex: Int): Map<String, Any?> { | ||
val pageInfo = mapOf( | ||
"width" to pdfDocument.getPageSize(pageIndex).width, | ||
"height" to pdfDocument.getPageSize(pageIndex).height, | ||
"label" to pdfDocument.getPageLabel(pageIndex, false), | ||
"index" to pageIndex, | ||
"rotation" to pdfDocument.getPageRotation(pageIndex) | ||
) | ||
return pageInfo | ||
} | ||
|
||
companion object { | ||
const val LOG_TAG = "FlutterPdfDocument" | ||
} | ||
} |
Oops, something went wrong.