Hi,
Is there any way to either detector enforce validation rules set on AcroForm fields when processing a pdf document with SyncFusion.Pdf?
I have a document with a number of fields set to a MaxLength of 1 that can be populated and saved with longer values.
Additionally is there any way to read other validation rules like; numeric, min/max number or regex for example?
We want to bulk populate a set of forms that will be retrieved dynamically, this needs to include picking up validation and (ideally) enforcing validation when setting values (like some kind of strict mode) programmatically so we can detect errors...
Hi,
Currently we are analyzing on this and will update the further details on April 5th, 2024.
Regards,
Jeyalakshmi T
Thank you for waiting patiently,
We can help you add actions to the fields. With this, we can validate the fields to meet your needs. Please try the solution below:
For more details, check out the User Guide documentation here:
https://help.syncfusion.com/file-formats/pdf/working-with-action#adding-an-action-to-the-form-field
PdfDocument document = new PdfDocument(); PdfPage page = document.Pages.Add();
PdfTextBoxField textBoxField = new PdfTextBoxField(page, "TextBox0"); textBoxField.Bounds = new RectangleF(10, 10, 100, 20); textBoxField.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 12); textBoxField.BorderColor = new PdfColor(Color.Black); textBoxField.BackColor = new PdfColor(Color.LightGray);
document.Form.Fields.Add(textBoxField);
PdfTextBoxField textBoxField2 = new PdfTextBoxField(page, "TextBox1"); textBoxField2.Bounds = new RectangleF(10, 40, 100, 20); textBoxField2.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 12); textBoxField2.BorderColor = new PdfColor(Color.Black); textBoxField2.BackColor = new PdfColor(Color.LightGray);
document.Form.Fields.Add(textBoxField2);
PdfTextBoxField textBoxField3 = new PdfTextBoxField(page, "TextBox2"); textBoxField3.Bounds = new RectangleF(10, 70, 100, 20); textBoxField3.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 12); textBoxField3.BorderColor = new PdfColor(Color.Black); textBoxField3.BackColor = new PdfColor(Color.LightGray);
document.Form.Fields.Add(textBoxField3);
//Create a new PdfButtonField PdfButtonField submitButton = new PdfButtonField(page, "submitButton"); submitButton.Bounds = new RectangleF(25, 160, 100, 20); submitButton.Text = "Apply"; submitButton.BackColor = new PdfColor(181, 191, 203);
//Create a new PdfJavaScriptAction PdfJavaScriptAction scriptAction = new PdfJavaScriptAction(File.ReadAllText(@"D://javaAction.txt")); //Set the scriptAction to submitButton submitButton.Actions.MouseDown = scriptAction;
//Add the submit button to the new document document.Form.Fields.Add(submitButton);
MemoryStream memoryStream = new MemoryStream(); document.Save(memoryStream);
document.Close(true); |
JavaAction.txt
// Function to validate the field function validateField(field, validationType, minValue, maxValue, regexPattern) { var value = field.valueAsString;
// Numeric validation if (validationType === "numeric") { if (isNaN(value)) { app.alert("Please enter a numeric value."); return false; } }
// Min/Max number validation if (validationType === "minmax") { if (parseFloat(value) < minValue || parseFloat(value) > maxValue) { app.alert("Please enter a value between " + minValue + " and " + maxValue + "."); return false; } }
// Regex validation if (validationType === "regex") { var regex = new RegExp(regexPattern); if (!regex.test(value)) { app.alert("Please enter a value that matches the pattern."); return false; } }
return true; }
// Function to validate all fields function validateFields() { // Validate field1 var field1 = this.getField("TextBox0"); if (!validateField(field1, "numeric", null, null, null)) { return; }
// Validate field2 var field2 = this.getField("TextBox1"); if (!validateField(field2, "minmax", 10, 100, null)) { return; }
// Validate field3 var field3 = this.getField("TextBox2"); if (!validateField(field3, "regex", null, null, "^\\d{5}$")) { return; }
// If all validations pass, perform other actions or submit the form // Example: // this.submitForm(); }
// Attach the validateFields function to the button click event var validateButton = this.getField("submitButton"); validateButton.setAction("MouseUp", "validateFields();"); |
Hi,
Thanks for your reply, sorry if there is a misunderstanding here. Your example shows how to create custom javascript validation and attach it to a new filed in a new document created with SyncFusion.
My use case is the opposite, I am (evaluating) using SyncFusion to read an existing pdf with an Acroform form on it, so that we can then automatically fill the form out. As explained in the original post I am trying to detect validation and formatting options applied to the fields when the user created the form.
Given your example which clearly applies an Action to the onClick event of a PdfButton, I assume I can iterate the Actions collection somehow to detect specific JS attached (please can you provide example)?
However will this Actions collection expose the "Custom Format Script" or "Custom Keystroke Script" as specified in Adobe Acrobats Field Properties 'Format' Tab (Custom option)? What about the "Custom validation script" as specified in
Adobe Acrobats Field Properties 'Validate' tab?
Furthermore, we are keep to detect other pre-supplied actions that Adobe Acrobat presents, these and their accompanying settings are within the 'Format' tab of the fields properties and consist of ; Number, Percentage, Date, Time 7 Special. We also would want to detect provided validation options in the 'Validate' tabs namely the min and max settings? My understanding is that these Actions are applied with preset Javascript and stored in the field dictionary, so I'm asking if there's any way to iterate the preset Javascript to detect these options or analyse the field flags directly?
Finally, is there any way to detect if fields are "Locked" (as they are when a document is signed)?
I am evaluating your library locally for use in parsing documents and applying the required formatting (In Acroforms only initially, not XFA forms) so being able to detect and validate our users inputs before populating the form is critical to the evaluation. Given this I hope you'll forgive the elaborate questions!
Thanks,
Thank you for your patience,
Currently, we don’t have support to create and parse the format, key press and validation action in the AcroFrom field. We have logged this requirement as a feature request with “Support to create and parse the format and validation action in the AcroFrom” in our library. At present, we do not have any immediate plans to implement this feature and we will implement this support in any of our upcoming releases. We usually have an interval of at least three months between releases, at the planning stage for every release cycle, we review all open features. We will let you know when this feature is implemented.
Please use the below feedback link to track the status of the feature.
is there any way to detect if fields are "Locked" | We don’t have support to detect whether or not fields are locked. Currently, we are analyzing this, and we will update further information on April 12th, 2024. |
Currently, we do not support to Locked property for all the fields.We have logged the feature request called “Support to Locked property for all the fields.” in our end. And we do not have any immediate plan to implement this feature. It will be available any of our upcoming releases.
You can track the status using the following feedback link:
https://www.syncfusion.com/feedback/52943/support-to-locked-property-for-all-the-fields