Skip to main content
Participant
August 23, 2024
Answered

JavaScript for Acrobat -- Appying text font and size changes to auto-generated text boxes

  • August 23, 2024
  • 1 reply
  • 1275 views

I use multiple tools in Acrobat which generate text box "stamps" onto one or pages of a PDF document when certain conditions are met.  These tools are mostly functional, however I am struggling to apply the correct formatting to the text boxes.  Certain properties such as text and fill color function correctly, however despite my attempts to code font and size properties into these auto-generated boxes, they will always ignore the coded properties and instead revert to the default font settings.

 

In this code, for example, none of the text properties are able to overwrite the default document settings.  I have other tools which help me to retroactively apply standard color and text size rules to the entire document to address the color issue, however I still have to manually change the font.  I would like to be able to have all the correct properties appear as soon as the box is generated:

 

var stampSample = this.addAnnot({
page: 0,
type: "FreeText",
rect: [250, 25, 560, 100],
contents: "sample text",
fillColor: color.white,
strokeColor: color.white,
textColor: color.black,
textSize: 10,
textFont: "Times-Roman",
readonly: false

 

I've tried variations on the formatting of the text font name (font.Times, "Times Roman", etc.) with no success.  I've had success with color formatting by modifying the richContents of boxes after they are created, however I haven't found a solution to apply that method here.

This topic has been closed for replies.
Correct answer PDF Automation Station

You have to do it by creating rich contents objects like this:

var stampSample = this.addAnnot({
page: 0,
type: "FreeText",
rect: [250, 25, 560, 100],
fillColor: color.white,
strokeColor: color.white,
readonly: false
});

var spans=[];
spans[0]={};
spans[0].textColor=color.black;
spans[0].textSize=10;
spans[0].text="SAMPLE TEXT";
spans[0].textFont="Times-Roman";
stampSample.richContents=spans;

1 reply

PDF Automation Station
Community Expert
Community Expert
August 23, 2024

You have to do it by creating rich contents objects like this:

var stampSample = this.addAnnot({
page: 0,
type: "FreeText",
rect: [250, 25, 560, 100],
fillColor: color.white,
strokeColor: color.white,
readonly: false
});

var spans=[];
spans[0]={};
spans[0].textColor=color.black;
spans[0].textSize=10;
spans[0].text="SAMPLE TEXT";
spans[0].textFont="Times-Roman";
stampSample.richContents=spans;

Participant
August 23, 2024

Thanks! That fixed all but the Font Style, which still reverts back to the document default rather than updating to the Times Roman.

PDF Automation Station
Community Expert
Community Expert
August 23, 2024

I tested it before I posted it.  Did you remove the annotation and copy the script before running it?