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

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

New Here ,
Aug 23, 2024 Aug 23, 2024

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.

TOPICS
JavaScript
666
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
1 ACCEPTED SOLUTION
Community Expert ,
Aug 23, 2024 Aug 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;

View solution in original post

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 ,
Aug 23, 2024 Aug 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;

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
New Here ,
Aug 23, 2024 Aug 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.

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 ,
Aug 23, 2024 Aug 23, 2024

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

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
New Here ,
Aug 23, 2024 Aug 23, 2024
LATEST

I did.  I'm testing if the new text boxes reflect the desired changes by changing the default text size, font, and color for text boxes before executing the code.  The size and color are corrected to the desired properties, but the font is not.

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