Skip to main content
Participant
July 28, 2025
Question

Mark more than 1000 elements in a PDF document as decorative using JavaScript

  • July 28, 2025
  • 3 replies
  • 264 views

Hi all,

I have to mark more than 1000 elements in an PDF as decorative. I try a JavaScript printed here:

/*
 * This script iterates through all annotations in a tagged PDF document.
 * It checks whether annotations lack alternative text and marks them as decorative.
 * This script is designed to run in the Adobe Acrobat Pro debugger.
 */

// Check if the document has a tag tree
var doc = this;
if (!doc.catalog.spans) {
    console.println("This document does not have a tag tree. Please ensure the document is properly tagged before running this script.");
} else {
    // Get the total number of annotations in the document
    var numAnnotations = doc.numAnnots;
    if (numAnnotations === 0) {
        console.println("No annotations are found in this document.");
    } else {
        console.println("Processing annotations...");

        for (var i = 0; i < numAnnotations; i++) {
            // Access each annotation
            var annotation = doc.getAnnot(i);

            if (annotation !== null) {
                // Check if the annotation contains an 'Alt' key (alternative text)
                if (!annotation.alt || annotation.alt.trim() === "") {
                    // If no alternative text, mark it as decorative
                    annotation.setProps({
                        Contents: "Marking as decorative",
                        alt: "", // Ensure Alt is empty
                        flags: annotation.flags | 0x10 // Add the decorative flag (bitmask)
                    });

                    console.println("Annotation on page " + (annotation.page + 1) + " marked as decorative.");
                } else {
                    console.println("Annotation on page " + (annotation.page + 1) + " already has alternative text.");
                }
            }
        }

        console.println("Processing completed.");
    }
}

 

 

 

But the output is: 

This document does not have a tag tree. Please ensure the document is properly tagged before running this script.

true

 

In the folowing picture you can see that I'm using Adobe Acrobat Pro and that my Document has also a Tag-Tree. Of course it is not the document with more than 1000 elements but the script should also work with this small document.

 

 

Maybe one of you could help me with my problem.

 

Regards Norbert

3 replies

try67
Community Expert
Community Expert
July 28, 2025

Tags can't be accessed using a script. This is probably some AI hallucination code.

Thom Parker
Community Expert
Community Expert
July 28, 2025

I believe that Try67 is correct. You have used an AI to generate nonsensical code and are now asking us to fix it.

Besides the incorrect item mentioned by Bernd, there are several non-existant properties used in the code.  And the stated purpose of the code makes no sense. This code is truely a meaningless pile of crap.  

 

I would suggest you ask the AI to fix it. 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
PDF Automation Station
Community Expert
Community Expert
July 28, 2025

There is no numAnnots document property.  Try doc.getAnnots().length;

Bernd Alheit
Community Expert
Community Expert
July 28, 2025

this.catalog gives you the access of the search catalog. This has nothing to do with the tag tree.