Skip to content

Workaround date picker in date form field #8

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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 @@ -17,6 +17,7 @@ package com.pspdfkit.flutter.pspdfkit
/// This notice may not be removed from this file.
///
import com.pspdfkit.document.PdfDocument
import com.pspdfkit.flutter.pspdfkit.util.FormFieldMigrationHelper
import com.pspdfkit.ui.PdfUiFragment

class FlutterPdfUiFragment : PdfUiFragment() {
Expand All @@ -25,5 +26,6 @@ class FlutterPdfUiFragment : PdfUiFragment() {
super.onDocumentLoaded(document)
// Notify the Flutter PSPDFKit plugin that the document has been loaded.
EventDispatcher.getInstance().notifyDocumentLoaded(document)
FormFieldMigrationHelper.migrateFieldFormatActions(document)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright © 2025 PSPDFKit GmbH. 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 PSPDFKIT LICENSE AGREEMENT.
* UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
* This notice may not be removed from this file.
*/

package com.pspdfkit.flutter.pspdfkit.util

import com.pspdfkit.annotations.actions.AnnotationTriggerEvent
import com.pspdfkit.annotations.actions.JavaScriptAction
import com.pspdfkit.document.PdfDocument
import com.pspdfkit.forms.FormElement
import com.pspdfkit.forms.FormType
import com.pspdfkit.forms.TextInputFormat

object FormFieldMigrationHelper {
@JvmStatic
fun migrateFieldFormatActions(document: PdfDocument): Boolean {
var changed = false
document.formProvider.formElements.forEach { element ->
if (element.type == FormType.TEXT) {
changed = migrateFieldFormatActions(element) or changed
}
}
return changed
}

private fun migrateFieldFormatActions(element: FormElement): Boolean {
if (element.getInputFormat() in listOf(TextInputFormat.DATE, TextInputFormat.TIME)) return false

element.getJavaScriptActionForEvent(AnnotationTriggerEvent.FIELD_FORMAT)?.let { action ->
if (action.startsWith("AFTime") || action.startsWith("AFDate")) {
action.replace("_Format", "_Keystroke").also { fixedFormat ->
element.annotation.setAdditionalAction(AnnotationTriggerEvent.FORM_CHANGED, JavaScriptAction(fixedFormat))
return true
}
}
}
return false
}

private fun FormElement.getInputFormat(): TextInputFormat {
val formatString = getJavaScriptActionForEvent(AnnotationTriggerEvent.FORM_CHANGED)
?: return TextInputFormat.NORMAL

return when {
formatString.startsWith("AFDate_Keystroke")
-> TextInputFormat.DATE
formatString.startsWith("AFTime_Keystroke")
-> TextInputFormat.TIME
else -> TextInputFormat.NORMAL
}
}

private fun FormElement.getJavaScriptActionForEvent(event: AnnotationTriggerEvent): String? =
(annotation.getAdditionalAction(event) as? JavaScriptAction
?: formField.getAdditionalAction(event) as? JavaScriptAction)?.script
}
Binary file added example/PDFs/formdoc.pdf
Binary file not shown.
2 changes: 1 addition & 1 deletion example/lib/examples.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const String _documentPath = 'PDFs/PSPDFKit.pdf';
const String _measurementsDocs = 'PDFs/Measurements.pdf';
const String _lockedDocumentPath = 'PDFs/protected.pdf';
const String _imagePath = 'PDFs/PSPDFKit_Image_Example.jpg';
const String _formPath = 'PDFs/Form_example.pdf';
const String _formPath = 'PDFs/formdoc.pdf';
const String _instantDocumentJsonPath = 'PDFs/Instant/instant-document.json';
const String _xfdfPath = 'PDFs/Instant/document.xfdf';
const String _processedDocumentPath = 'PDFs/Embedded/PSPDFKit-processed.pdf';
Expand Down