Skip to main content
Known Participant
February 28, 2016
Answered

addAnnot problem with Acrobat DC on Mac

  • February 28, 2016
  • 2 replies
  • 2120 views

G'day

I'm having awful problems trying to get the following JavaScript to work on my mac, running El Capitan.

Using an old 'Add Watermark' Java script, the free barcode font Code 128 draws as the font Arial. So I'm trying to use an Annotation, with no success after two days trying.

I get an error that the Java Script does not understand the 'do script' message.

Any guidance, please.

Regards

Santa

set theNewString to ((character id 209) & "this is a test" & (character id 211) as text)

# values are Text, Font, Font Size, Start Page, End Page, Text type?, Horizontal Alignment, Vertical Offset, Horizontal Offset, Vertical Height, Rotation, Current Page

set theResult1 to my Add_WaterMarkText(theNewString, "Code 128", 48, 1, 1, 1, 0, 3, 10, 0, 0, 1)

# This handler uses Java Script

#

on Add_WaterMarkText(cText, cFont, nFontSize, nS, nE, nTA, nHorizontalAlignment, nVerticalAlignment, nHorizontalValue, nVerticalValue, nR, currentPage)

  set Params to ""

  set Params to Params & "cPage:" & currentPage - 1 & ", " & return

  set Params to Params & "cType:Text, " & return

  set Params to Params & "cContents:" & cText & ", " & return

  set Params to Params & "cTextFont:" & cFont & ", " & return

  set Params to Params & "cTextSize:40, " & return

  set Params to Params & "cpoint:[300, 100], " & return

  set Params to Params & "cWidth:0, " & return

  set Params to Params & "cAlignment:0, " & return

  tell application "Adobe Acrobat"

  activate

  tell document 1

  do script "this.addAnnot({" & Params & "});"

  end tell

  end tell

end Add_WaterMarkText

This topic has been closed for replies.
Correct answer try67

Thank you both once again. sorry about the tardiness of my reply, by we in Oz had to sleep.

Try67, I modified my script, but still get an error 'Can't understand script message', and a debug error (below)

Test Screen Name, here's the result of my Debug. I don't know what the missing ';' really wants? I've tried putting a ';' & return in front, and now the debugger does not throw an error, but it also doesn't work. The 'run' button stays red

var f = this.addField("Text1","Ñthis is a testÓ", [0, 1298, 144, 1262], 0);

f.richText:true;

f.textSize:16;

f.alignment:"Left";

f.multiline:false;

f.lineWidth:0;

f.display:display.visible;

f.textColor:color.black;

SyntaxError: missing ; before statement

1:

SyntaxError: missing ; before statement

1:

My Code...

tell application "Adobe Acrobat"

  activate

  set p to 1

  tell document 1

  tell page 1

  set cbox to {}

  if cbox = {} then set cbox to crop box

  if cbox = {} then set cbox to bounds

  if cbox = {} then set cbox to media box

  if cbox = {} then set cbox to art box

  if cbox = {} then set cbox to trim box

  if cbox = {} then set cbox to bleed box

  set p to 4

  set pageTop to (item 2 of cbox) + 36 as integer

  set p to 5

  set item 2 of cbox to pageTop

  set p to 6

  set pageBottom to (item 4 of cbox)

  set p to 7

  set item 4 of cbox to pageBottom

  set p to 8

  set bounds to cbox

  set p to 9

  set media box to cbox

  set p to 10

  set crop box to cbox

  set p to 11

  set trim box to cbox

  set p to 12

  set bleed box to cbox

  set p to 13

  set art box to cbox

  set p to 14

  set theNewString to ((character id 209) & "this is a test" & (character id 211) as text)

  #

  # values are Text, Font, Font Size, Start Page, End Page, Text type?, Horizontal Alignment, Vertical Offset, Horizontal Offset, Vertical Height, Rotation, Current Page

  set p to 15

  set theResult1 to my Add_TextBoxText(theNewString, "MRVCode128bXSA", 16, 1, 1, 1, 0, pageTop, 144, 36, 0, 1)

  try

  do script theResult1

  on error errmsg

  display dialog errmsg

  end try

  end tell

  end tell

end tell

# This handler uses Java Script

#

on Add_TextBoxText(cText, cFont, nFontSize, nS, nE, nTA, nHorizontalAlignment, nVerticalAlignment, nHorizontalValue, nVerticalValue, nR, currentPage)

  set Params to "var f = this.addField(\"" & "Text1" & "\",\"" & cText & "\", [" & nHorizontalAlignment & ", " & nVerticalAlignment & ", " & nHorizontalValue & ", " & nVerticalAlignment - nVerticalValue & "], " & currentPage - 1 & ");" & return

  set Params to Params & "f.richText:true;" & return

  # set Params to Params & "f.textFont:font.\"" & cFont & "\";" & return

  set Params to Params & "f.textSize:" & nFontSize & ";" & return

  set Params to Params & "f.alignment:\"" & "Left" & "\";" & return

  set Params to Params & "f.multiline:false;" & return

  set Params to Params & "f.lineWidth:0;" & return

  set Params to Params & "f.display:display.visible;" & return

  set Params to Params & "f.textColor:color.black;" & return

  set javaScript to Params

  set the clipboard to javaScript

  return javaScript

end Add_TextBoxText

Added this to stop debugger throwing an error, but does not do anything now.

set Params to ";" & return

  set Params to Params & "var f = this.addField(\"" & "Text1" & "\",\"" & cText & "\", [" & nHorizontalAlignment & ", " & nVerticalAlignment & ", " & nHorizontalValue & ", " & nVerticalAlignment - nVerticalValue & "], " & currentPage - 1 & ");" & return



Replace all colons with equals signs (=).

Also, double-check the order of the parameters of addField.

2 replies

Legend
February 28, 2016

‌I'm sure you understand this but to avoid confusing people, what you posted isn't a JavaScript, it's an AppleScript, run from the Finder. It creates a single line that contains JavaScript. The first example tries to use do script to run it in Acrobat.

YOu ask whether the c is needed in cPage etc. Check the JavaScript API reference for the correct names, but a c like this is Not unusual. Lrt's simplify to eliminate AppleScript issues. You build a command. does it work if you paste into the JavaScript console?

OzSanta2Author
Known Participant
February 28, 2016

I'm really up the proverbial creek without a paddle.

I've tried modifying the script to the following, and  have no idea if a 'c' is necessary before each item, as in my first attempt. All I know is that the clipboard contains exactly the script in the form it's supposed to be in, as far as I can tell, the script is NOT crashing, and nothings happening, no text boxes are being drawn. Do I need some different code to draw a text box rather than an Annotation?.

Please, any advice?

tell application "Adobe Acrobat"

  activate

  set p to 1

  tell document 1

  tell page 1

  set cbox to {}

  if cbox = {} then set cbox to crop box

  if cbox = {} then set cbox to bounds

  if cbox = {} then set cbox to media box

  if cbox = {} then set cbox to art box

  if cbox = {} then set cbox to trim box

  if cbox = {} then set cbox to bleed box

  set p to 4

  set pageTop to (item 2 of cbox) + 36

  set p to 5

  set item 2 of cbox to pageTop

  set p to 6

  set pageBottom to (item 4 of cbox)

  set p to 7

  set item 4 of cbox to pageBottom

  set p to 8

  set bounds to cbox

  set p to 9

  set media box to cbox

  set p to 10

  set crop box to cbox

  set p to 11

  set trim box to cbox

  set p to 12

  set bleed box to cbox

  set p to 13

  set art box to cbox

  set p to 14

  set theNewString to ((character id 209) & "this is a test" & (character id 211) as text)

  #

  # values are Text, Font, Font Size, Start Page, End Page, Text type?, Horizontal Alignment, Vertical Offset, Horizontal Offset, Vertical Height, Rotation, Current Page

  set p to 15

  set theResult1 to my Add_WaterMarkText(theNewString, "Times", 16, 1, 1, 1, 0, 3, 10, 0, 0, 1)

  try

  do script theResult1

  on error errmsg

  display dialog errmsg

  end try

  set theResult1 to my Add_WaterMarkText(theNewString, "MRV Code128bXSA", 16, 1, 1, 1, 0, 3, 10, 0, 0, 1)

  try

  do script theResult1

  on error errmsg

  display dialog errmsg

  end try

  end tell

  end tell

end tell

# This handler uses Java Script

#

on Add_WaterMarkText(cText, cFont, nFontSize, nS, nE, nTA, nHorizontalAlignment, nVerticalAlignment, nHorizontalValue, nVerticalValue, nR, currentPage)

  # set theType to "Text" as text

  set Params to "page:" & currentPage - 1 & ", " & return

  set Params to Params & "type:\"" & "Text" & "\", " & return

  set Params to Params & "contents:\"" & cText & "\", " & return

  set Params to Params & "textFont:\"" & cFont & "\", " & return

  set Params to Params & "textSize:" & nFontSize & ", " & return

  set Params to Params & "rect:[1, 1, 300, 100], " & return

  set Params to Params & "width:0, " & return

  set Params to Params & "alignment:0, " & return

  set Params to Params & "hidden:false, " & return

  set Params to Params & "textColor:color.black," & return

  set Params to Params & "opacity:1," & return

  set javaScript to "this.addAnnot({" & return & Params & "});"

  set the clipboard to javaScript

  return javaScript

end Add_WaterMarkText

The ClipBoard...

this.addAnnot({

page:0,

type:"Text",

contents:"Ñthis is a testÓ",

textFont:"MRV Code128bXSA",

textSize:16,

rect:[1, 1, 300, 100],

width:0,

alignment:0,

hidden:false,

textColor:color.black,

opacity:1,

});

try67
Community Expert
Community Expert
February 28, 2016

If you're trying to add a barcode that can be expressed using a font, use a text field instead of an annotation or watermark.

OzSanta2Author
Known Participant
February 28, 2016

Thank you both.

I  finally got the posted script to run, only to find it created a 'dialog balloon' icon, that when clicked on, revealed the text in Arial format. So I've decided to try the text box approach.

The script I've come up with fails with the same error as before, doesn't understand the 'do script' message.

I've pasted the Clipboard into the debugger, and added 'var f ' in front, and the error I eventually get is that it needs a Doc set, which I don't know how to do, despite researching 'Doc'. I'm presuming that means the script should be OK, but why don't it work? I'm hopeless at JavaScript, so any advice is greatly appreciated.

I would also like  to make sure I'm using the correct font name, by following this procedure below, but can't work out how to name a text box 'Text1'. Any help on this as well, please? I've tried all sorts of things, but no explanation tells me how to name a text box in Acrobat Pro DC! Is it possible to address the text field by a number?

Thanks in advance,

Santa

To find the PDSysFont font name of a font:

1.Create a text field in a PDF document. Using the UI, set the text font for this field to the desired font.

2.Open the JavaScript Debugger Console and execute the script

      this.getField("Text1").textFont

The above code assumes the name of the field is Text1.

3.The string returned to the console is the font name needed to programmatically set the text font.

tell application "Adobe Acrobat"

  activate

  set p to 1

  tell document 1

  tell page 1

  set cbox to {}

  if cbox = {} then set cbox to crop box

  if cbox = {} then set cbox to bounds

  if cbox = {} then set cbox to media box

  if cbox = {} then set cbox to art box

  if cbox = {} then set cbox to trim box

  if cbox = {} then set cbox to bleed box

  set p to 4

  set pageTop to (item 2 of cbox) + 36

  set p to 5

  set item 2 of cbox to pageTop

  set p to 6

  set pageBottom to (item 4 of cbox)

  set p to 7

  set item 4 of cbox to pageBottom

  set p to 8

  set bounds to cbox

  set p to 9

  set media box to cbox

  set p to 10

  set crop box to cbox

  set p to 11

  set trim box to cbox

  set p to 12

  set bleed box to cbox

  set p to 13

  set art box to cbox

  set p to 14

  set theNewString to ((character id 209) & "this is a test" & (character id 211) as text)

  #

  # values are Text, Font, Font Size, Start Page, End Page, Text type?, Horizontal Alignment, Vertical Offset, Horizontal Offset, Vertical Height, Rotation, Current Page

  set p to 15

  set theResult1 to my Add_WaterMarkText(theNewString, "MRVCode128bXSA", 16, 1, 1, 1, 0, 3, 10, 0, 0, 1)

  try

  do script theResult1

  on error errmsg

  display dialog errmsg

  end try

  end tell

  end tell

end tell

# This handler uses Java Script

#

on Add_WaterMarkText(cText, cFont, nFontSize, nS, nE, nTA, nHorizontalAlignment, nVerticalAlignment, nHorizontalValue, nVerticalValue, nR, currentPage)

  set Params to "page:" & currentPage - 1 & ", " & return

  # set Params to Params & "type:\"" & "Text" & "\"," & return

  set Params to Params & "richText:true," & return

  set Params to Params & "value:\"" & cText & "\"," & return

  set Params to Params & "textFont:\"" & cFont & "\"," & return

  set Params to Params & "textSize:" & nFontSize & "," & return

  set Params to Params & "rect:[100, 472, 272, 400]," & return

  set Params to Params & "alignment:\"" & "Left" & "\"," & return

  set Params to Params & "UserName:\"" & "Text1" & "\"," & return

  set Params to Params & "multiline:false," & return

  set Params to Params & "lineWidth:0," & return

  # set Params to Params & "display:display.visible," & return

  set Params to Params & "textColor:color.black," & return

  set javaScript to "this.addField({" & Params & "});"

  set the clipboard to javaScript

  return javaScript

end Add_WaterMarkText

The Cipboard...

this.addField({page:0,

richText:true,

value:"Ñthis is a testÓ",

textFont:"MRV Code128bXSA regular",

textSize:16,

rect:[100, 472, 272, 400],

alignment:"Left",

UserName:"Text1",

multiline:false,

lineWidth:0,

textColor:color.black,

});