How to Change a Selected Annotation's Fill Color with Acrobat JavaScript
Question details
The user wants to use a JavaScript script to dynamically identify and modify the fill color of a currently selected PDF annotation without needing its specific name.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- Automating PDF document editing and formatting by using scripts to quickly change the properties of user-selected elements.
- Observed behavior
- The user needs a programmatic way to fetch the active selection and apply a new fill color using Acrobat's JavaScript API.
Ensure you have a PDF document open with at least one annotation added, and verify that your PDF editor permits the execution of custom JavaScript.
Use the selectedAnnots Property in Acrobat JavaScript
Apply a custom script that retrieves the currently selected annotation array and modifies the fill color of the active selection.
Acrobat JavaScript provides the `selectedAnnots` property to access annotations that a user has actively selected. This avoids the need to know the annotation's exact name beforehand.
It is important to check the length of the returned array to ensure you are modifying the correct number of annotations and to prevent errors when no annotations are selected.
Open the JavaScript Debugger or Console in Adobe Acrobat to input your custom script.
Declare a variable to hold the selection by typing `var annots = this.selectedAnnots;`.
Create a conditional statement to check if exactly one annotation is selected: `if (annots != null && annots.length == 1) { ... }`.
Inside the conditional statement, assign a new color to the `fillColor` property. For example, to make it red, use `annots[0].fillColor = color.red;`.
Run the script to apply the red fill color to the currently selected annotation.
Change PDF Annotation Colors Instantly with WPS PDF
Instead of writing complex JavaScript code, you can easily manage and change annotation fill colors visually using WPS PDF. It provides a user-friendly interface to customize highlights, shapes, and comments instantly.
- 1. Open your PDF: Launch WPS Office and open the PDF document containing your annotations.
- 2. Select the annotation: Click directly on the shape, highlight, or text box you wish to modify.
- 3. Access the properties: Right-click the selected annotation and choose 'Properties', or use the floating quick-access toolbar.
- 4. Apply a new color: Click the 'Fill Color' icon and select your desired shade from the color palette to apply it instantly.

Frequently Asked Questions
Can I change the outline color of the selected annotation as well?
Yes, you can modify the outline or stroke color by targeting the `strokeColor` property of the annotation object in your JavaScript code, similar to how you change the `fillColor`.
Why does the selectedAnnots property return null?
The `selectedAnnots` property returns null if there are no annotations actively selected in the document when the script is executed. Always include a null check in your script to avoid execution errors.
What color formats are supported in Acrobat JavaScript?
Acrobat JavaScript supports predefined color arrays like `color.red`, `color.blue`, and `color.transparent`. You can also define custom RGB, CMYK, or Grayscale colors using format arrays such as `["RGB", 1, 0, 0]` for red.
Do I need to assign names to annotations to change their color?
No. By using the `selectedAnnots` property, you dynamically fetch the annotations based on the user's active selection, which bypasses the requirement of knowing or querying specific annotation names.




