How to Launch a Custom URL with PDF Form Values Using JavaScript
Question details
How to retrieve a user-entered value from a PDF form field and use it to dynamically construct and launch a custom URL via a JavaScript button action.

- Product
- Device & OS
- not provided
- Scenario
- Creating an interactive PDF document where a push button or text field action launches a URL containing dynamic user input, such as a custom tabletop RPG dice command.
- Observed behavior
- The user needs the correct JavaScript syntax to extract the field data and concatenate it to a base URL string, ensuring the script executes properly without external text editor saving issues.
Ensure your PDF editor supports JavaScript execution and interactive form creation. Verify the exact name of the text field from which you intend to retrieve the value.
Concatenate Field Values using app.launchURL
Extract the target form field's value using the getField method and combine it with your base URL string to launch the dynamic web or app request.
By using the native PDF JavaScript API, you can dynamically retrieve text inputted by the document user. You can then append this text to a static URL base to trigger customized web queries or local application commands (such as a 'talespire://' URL scheme).
Open your PDF form editor, right-click the text field you want to extract data from, and check its properties to note the exact field name (e.g., 'details_str_modifier').
Create a Push Button on your PDF. Open its Properties, navigate to the Actions tab, select 'Run a JavaScript' as the action type, and click Add.
In the JavaScript editor window, define a variable to capture the input: var StrMod = this.getField("details_str_modifier").value; Then construct and launch the URL: app.launchURL("talespire://dice/d20+" + StrMod, true);
Click OK to save the JavaScript changes directly in your PDF editor. If you are using an external editor like Atom to write your script, ensure the file is properly saved and synced with your PDF software before testing.

Easily Add JavaScript and Forms with WPS Office
WPS Office provides robust PDF editing capabilities, enabling you to effortlessly insert interactive text fields, configure push buttons, and add custom JavaScript actions to create dynamic document workflows.
- 1. Open your PDF: Launch WPS Office and open the PDF document you want to make interactive.
- 2. Navigate to Form Tools: Go to the 'Form' tab in the top ribbon menu to access the text field and push button insertion tools.
- 3. Insert Form Elements: Click 'Text Field' to add user input areas, then click 'Push Button' to place your interactive trigger.
- 4. Add JavaScript: Right-click the button, select Properties, go to Actions, and choose 'Run a JavaScript' to paste your custom app.launchURL code.
- 5. Test your document: Save the PDF file, exit form editing mode, and click the button to verify that the dynamic URL launches successfully.

Frequently Asked Questions
Why isn't my JavaScript executing when I click the PDF button?
Ensure that JavaScript is enabled in your PDF viewer's security preferences. Also, verify that the button's action trigger is correctly set to 'Mouse Up' rather than 'Mouse Down', which is the standard trigger for interactive buttons.
Can I use this method to pass multiple form values into a single URL?
Yes. You can declare multiple variables to retrieve different field values using this.getField("fieldname").value, and then concatenate all of them into a single URL string before calling app.launchURL.
Why didn't my JavaScript changes save when editing via an external app?
If you use an external editor like Atom to write your script, the changes will not take effect until the file is physically saved in that editor and your PDF program refreshes or detects those external changes. Writing the script directly in your PDF editor's native JavaScript console often prevents this synchronization issue.




