Search code examples
javascriptadobeacrobat

How to make part of annotation text bold using javascript in Adobe Acrobat Pro


I would like to add inline bold style inside the contents. How, can I achieve that? The below code output bold tag markup instead of applying the bold formatting.

var annot = this.addAnnot({
    page: 0,
    type: "FreeText",
    rect: [50, 50, 300, 100],    // location and size of text field in points
    contents: "<b>Links to</b>: http://www.example.com",
});

Solution

  • The text field properties needs to be set to allow rich text formatting. Then you need to create an array of Span objects that contain the text with the formatting you want.

    var spans = new Array();
    spans[0] = new Object();
    spans[0].text = "Attention:\r";
    spans[0].textStyle = "bold";
    spans[0].textSize = 18;
    
    spans[1] = new Object();
    spans[1].text = "Adobe Acrobat DC\r";
    spans[1].textColor = color.red;
    spans[1].textSize = 20;
    spans[1].alignment = "center";
    
    spans[2] = new Object();
    spans[2].text = "will soon be here!";
    spans[2].textColor = color.green;
    spans[2].fontStyle = "italic";
    spans[2].underline = true;
    spans[2].alignment = "right";
    
    // Now create the annot with spans as it's richContents
    var annot = this.addAnnot({
    page: 0,
    type: "FreeText",
    rect: [50, 50, 300, 100], 
    richContents: spans
    

    });