Skip to main content
Participant
July 30, 2024
Answered

Place a caret before strikeout and copy contents

  • July 30, 2024
  • 2 replies
  • 2341 views

I've done some digging in these forums and found out it is possible to change an annotation type from highlight to strikeout. Is there a code that places a caret right before the strikeout and copies its contents? I'm very new to javascript, haven't been able to find a solution yet

Correct answer tobias.wantzen

I managed to solve this by my own. For anyone in need of a starting point for such a script topic here's my solution:

 

 

/**
 * @description Convert all non-empty StrikeOut (cross-out) annotations into StrikeOutTextEdit (true replace) annotations for Adobe Acrobat
 *  Tobias Wantzen
 * .0
 */
try {
    this.syncAnnotScan();
    for (var nPage = 0; nPage < this.numPages; nPage++) {
        /* get all annotations on the page */
        var Annots = this.getAnnots({
            nPage: nPage
        });
        /* process each annotation */
        if (Annots != null) {
            for (var i = 0; i < Annots.length; i++) {
                if (Annots[i].type == "StrikeOut" && Annots[i].contents != "") {
                    /* get position of strikethrough annotation */
                    var AnnotsRect = Annots[i].rect;
                    /* create caret annotation */
                    var newAnnot = this.addAnnot({
                        page: nPage,
                        type: "Caret",
                        author: Annots[i].author,
                        intent: "Replace", 
                        refType: "R", 
                        rect: [ AnnotsRect[2], AnnotsRect[1], AnnotsRect[2] - 10, AnnotsRect[1] + 10 ],
                        richContents: Annots[i].richContents
                    });
                    /* group with strikethrough annotation */
                    Annots[i].richContents = "";
                    Annots[i].intent = "StrikeOutTextEdit";
                    Annots[i].inReplyTo = newAnnot.name;
                    Annots[i].refType = "Group";
                }
            }
        }
    }
} catch (e) {
    app.alert(e);
}

 

 

I'm not quite sure about the caret annotation's rect definition I found by try & error. Will this still work on very small or very big font sizes of the text in the PDF? I don't know ... If there is a better solution for positioning the caret annotation please let me know.

 

Thanks, Tobias

2 replies

JR Boulay
Community Expert
Community Expert
February 26, 2025

[MOVED TO THE ACROBAT DISCUSSIONS]

Acrobate du PDF, InDesigner et Photoshopographe
try67
Community Expert
Community Expert
July 30, 2024

A caret is just another type of annotation, which you can add using the addAnnot method.

However, if you want to create a real Replace Text-type comment, like Acrobat does, you need to set the StrikeOut annotation to be a reply to the Caret one. They will then be grouped by the application to a single "Replace Text" annotation.

Participant
July 30, 2024

Yes, I'm familiar with the addAnnot method. What I haven't been able to work out yet is how to add a Caret right before the StrikeOut. Would I have to use quads for this or is there another way?

I actually don't want to create that replace comment, I need to keep both annots separate, just next to each other, but it's good to know how it should be coded.

try67
Community Expert
Community Expert
July 30, 2024

Yes, you need to use the quads array to set the location of the caret.