Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

New Here ,
Jul 28, 2025 Jul 28, 2025

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.

 

AdobeAcrobatPro.png

 

Maybe one of you could help me with my problem.

 

Regards Norbert

TOPICS
JavaScript , PDF , Standards and accessibility
193
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 28, 2025 Jul 28, 2025

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 28, 2025 Jul 28, 2025

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 28, 2025 Jul 28, 2025

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 28, 2025 Jul 28, 2025
LATEST

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 PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines