How to Automatically Calculate Costs in PDF Forms Based on Hours
Question details
The user needs to create a room booking PDF form that automatically calculates the total price based on hourly usage, applies conditional rates based on the duration, and sums the charges across multiple rooms.

- Product
- Device & OS
- not provided
- Scenario
- Creating an interactive room booking PDF form for clients to fill out.
- Observed behavior
- Needs dynamic cost calculation depending on the input hours (e.g., one rate for 6 hours or less, another for more than 6 hours) and a grand total field.
Ensure you have a professional PDF editor with form creation capabilities, as standard PDF viewers cannot embed complex JavaScript calculations.
Implement JavaScript for Conditional Hourly Rates
Use custom JavaScript in your PDF form field properties to evaluate the entered hours and apply the correct pricing tier.
Since standard PDF calculation tools only support basic arithmetic, you must use a custom calculation script to handle conditional logic, such as charging different rates depending on whether the booking is over or under six hours.
Open your PDF form in your advanced PDF editor and navigate to the 'Prepare Form' or 'Edit Form' tool.
Right-click the text field where the room cost will be displayed, select 'Properties,' and navigate to the 'Calculate' tab.
Select the 'Custom calculation script' option and click 'Edit'. Enter a JavaScript snippet that reads the hours field and applies your conditional pricing logic. For example: if the duration is <= 6, multiply by rate A; otherwise, multiply by rate B.
Add a new text field for the total cost. In its 'Calculate' tab, choose 'Value is the sum (+)' and select all individual room cost fields to add them together automatically.

Use Built-in Calculation for Simple Totals
Use the default calculation options for fields that just need basic arithmetic, like summing the grand total across multiple rooms.
Easily Manage and Fill Calculated PDF Forms with WPS Office
WPS PDF offers powerful form viewing, filling, and editing capabilities. It ensures seamless compatibility when distributing your calculated booking forms to clients, allowing JavaScript formulas to execute perfectly.
- 1. Download WPS Office: Download and install the free WPS Office suite on your device.
- 2. Open your form: Launch WPS PDF and open your interactive PDF room booking form.
- 3. Fill in the details: Enter the booking hours in the designated fields; the form will automatically calculate the costs based on the embedded scripts.
- 4. Save and share: Save or export the filled-out form to share the calculated invoice with your clients or administration.

Frequently Asked Questions
Why is my PDF form calculation not updating automatically?
The field calculation order might be incorrect. In your PDF form editor properties, ensure that the individual room costs are calculated before the grand total field. If the grand total calculates first, it won't include the newly generated room costs.
Can I calculate the hours automatically from start and end times in a PDF?
Yes, but standard PDF features cannot calculate time natively. It requires writing a custom JavaScript to parse the time strings (e.g., 09:00 to 14:00) into date objects, calculate the difference in milliseconds, and convert it back to hours.
How do I handle empty fields in PDF JavaScript calculations?
In your custom calculation script, add a condition to check if the input field is empty (for example: if (hours == "") { event.value = ""; }) so the cost field remains blank instead of defaulting to a zero or displaying an error.
Does WPS Office support PDF JavaScript calculations?
Yes, WPS PDF perfectly supports viewing, filling, and executing standard PDF JavaScript formulas. When users fill out interactive forms in WPS PDF, the calculations will trigger exactly as intended.




