How to Create a PDF Button That Opens a Dynamically Generated URL
Question details
The user wants a button in a PDF form to generate a URL dynamically based on values entered in specific fields, and then open the link without displaying the complete web address on the page.

- Product
- PDF Editor
- Device & OS
- not provided
- Scenario
- Creating an interactive PDF form where a destination web address depends on user inputs.
- Observed behavior
- The goal is to seamlessly construct and launch a dynamically built web address upon clicking a button, rather than having the user manually copy or view the entire URL.
Ensure you are using a PDF editor that supports form field editing and JavaScript actions, and identify the exact names of the text fields you plan to use for the dynamic URL parameters.
Add a Custom JavaScript Action to the Button
Use a custom JavaScript snippet triggered by a 'Mouse Up' action to read field values, construct the URL, and launch it in the browser.
By writing a simple JavaScript snippet, you can concatenate static URL elements with dynamic text field values entered by the user. The application's native launch method will then open the resulting web address.
Open your PDF file in your chosen PDF editor and switch to the Form Editing or Prepare Form mode.
Right-click the target button you want to use for the link and select 'Properties' from the context menu.
Navigate to the 'Actions' tab. Select 'Mouse Up' as the trigger and 'Run a JavaScript' as the action type.
Click the 'Add' button and input your script. For example: var fullURL = 'http://www.' + this.getField('URL1').valueAsString + '.com/' + this.getField('URL2').valueAsString + '.html'; app.launchURL(fullURL);
Replace 'URL1' and 'URL2' in the script with your exact text field names. Click 'OK' to save the script, then close the properties dialog and test your button in standard viewing mode.

Create and Manage Interactive PDF Forms with WPS Office
WPS Office provides a comprehensive suite of PDF tools that make it easy to design interactive forms, insert push buttons, and handle advanced PDF documents effortlessly.
- 1. Open the PDF document: Launch WPS Office and open the PDF file you wish to make interactive.
- 2. Access form controls: Navigate to the 'Form' tab on the top ribbon to view all interactive form controls.
- 3. Insert a button: Click the 'Push Button' tool and click and drag on the document page to place your button.
- 4. Configure button properties: Double-click the new button to open its properties, where you can assign actions or adjust its appearance.

Frequently Asked Questions
Why doesn't the app.launchURL() function work when the button is clicked?
Security settings in some PDF readers prevent links from opening automatically to protect users. You may need to grant the document permission to open external links. Additionally, ensure there are no syntax errors or typos in your JavaScript.
Can I test the dynamic URL while in form editing mode?
No, interactive elements like buttons generally do not execute scripts while in editing mode. You need to switch to form preview or standard document viewing mode to click the button and test the functionality.
What happens if a user leaves the referenced text fields blank?
If the text fields are empty, the script will retrieve an empty string. This will result in an incomplete or broken URL being launched. You can prevent this by adding an 'if' statement to your JavaScript to verify the fields are filled before executing the launch command.




