PDF Editing Issues

How to Change a Selected Annotation's Fill Color with Acrobat JavaScript

Anum MushtaqAnum Mushtaq Nov 3, 2026 869 views

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.

How to Change a Selected Annotation's Fill Color with Acrobat JavaScript
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.
Before you start

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.

Solution 1Recommended

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.

1
Access the JavaScript Console

Open the JavaScript Debugger or Console in Adobe Acrobat to input your custom script.

2
Retrieve the selected annotations

Declare a variable to hold the selection by typing `var annots = this.selectedAnnots;`.

3
Verify the selection

Create a conditional statement to check if exactly one annotation is selected: `if (annots != null && annots.length == 1) { ... }`.

4
Change the fill color

Inside the conditional statement, assign a new color to the `fillColor` property. For example, to make it red, use `annots[0].fillColor = color.red;`.

5
Execute the script

Run the script to apply the red fill color to the currently selected annotation.

Handling Multiple Selections: If you want the script to modify the color of multiple selected annotations at once, replace the single conditional check with a `for` loop that iterates through the entire `annots` array.
WPS PDF Visual Editor

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. 1. Open your PDF: Launch WPS Office and open the PDF document containing your annotations.
  2. 2. Select the annotation: Click directly on the shape, highlight, or text box you wish to modify.
  3. 3. Access the properties: Right-click the selected annotation and choose 'Properties', or use the floating quick-access toolbar.
  4. 4. Apply a new color: Click the 'Fill Color' icon and select your desired shade from the color palette to apply it instantly.
Visually edit annotation colors and properties with just a few clicks, no coding required.Highly compatible with standard PDF formats and elements created in Adobe Acrobat.Lightweight performance ensures quick loading and smooth editing for large PDF files.
microsoft office alternative - wps office

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.