How to Extract the First Three Characters from a PDF Form Field
Question details
The user needs to automatically copy a specific substring (the first three characters) from one PDF form field into a destination field.

- Product
- PDF Editor
- Device & OS
- not provided
- Scenario
- Setting up a dynamic PDF form where specific fields are auto-populated based on the inputs from other text fields.
- Observed behavior
- The destination field requires an automated script to extract and display characters like 'SMI' from a source field containing 'SMITH'.
Verify the exact name of your source field (e.g., 'Last Name'), as field references in PDF scripts are strictly case-sensitive and must account for spaces.
Apply a Custom Calculation Script
Use a simple JavaScript substring method in the destination field's properties to pull the first three characters from the source field.
PDF forms support JavaScript for field calculations. By targeting the source field and using the substr() method, you can extract any specific portion of a text string.
Right-click the destination field where the extracted characters should appear, and select 'Properties' from the context menu.
In the Properties dialog box, click on the 'Calculate' tab to view the calculation options.
Select the 'Custom calculation script' option and click the 'Edit' button to open the JavaScript editor.
Type the following code: event.value = this.getField("Last Name").valueAsString.substr(0,3); Make sure to replace "Last Name" with the exact name of your source field.
Click 'OK', then 'Close'. Preview your PDF form and type a word into the source field to verify the destination field updates automatically.

Create and Manage Interactive PDF Forms with WPS Office
WPS Office provides an intuitive suite of PDF editing tools, allowing you to easily create, format, and manage interactive PDF forms with full support for JavaScript calculations.
- 1. Open PDF Document: Launch WPS Office and open your PDF file using the PDF editor.
- 2. Access Form Tools: Navigate to the 'Form' tab on the top ribbon to view all interactive form creation tools.
- 3. Edit Field Properties: Double-click any text field to access its properties, where you can easily configure custom calculations and actions.

Frequently Asked Questions
How can I extract characters from the end of a field instead of the beginning?
To extract characters from the end of a string, you can use the slice() method with a negative index in your JavaScript. For example, replace .substr(0,3) with .slice(-3) to get the last three characters.
Why isn't my destination field updating when I type in the source field?
The source field must be configured to commit its value immediately. Right-click the source field, go to Properties > Options, and check the box for 'Commit selected value immediately'.
Does this JavaScript calculation work on mobile PDF viewers?
Advanced JavaScript calculations in PDF forms are generally supported on robust desktop editors like WPS Office or Adobe Acrobat. Basic mobile viewers may not execute custom scripts, causing the fields to remain blank.




