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

Java script help - create and autoformat text callout

New Here ,
Dec 06, 2023 Dec 06, 2023

I'm trying to run this code in the Action Wizard but it's not executing. Can anyone spot what's wrong with it? The purpose to to create a text callout and autoformat any text entered as 10 pt, Helvetica, red font.

TOPICS
JavaScript
877
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 ,
Dec 06, 2023 Dec 06, 2023

Sorry, here's the code:

 

// Function to create text callout and text field
function createTextCalloutAndField() {
// Get the active document
var doc = this;

// Create a text callout annotation
var callout = doc.createAnnot({
type: "Annot",
subtype: "TextCallout",
rect: [226.09500122070312, 1091.8299560546875, 484.81298828125, 1167.5699462890625],
fillColor: 0x00000000, // Transparent background
lineWidth: 1,
strokeColor: 0x000000, // Black border
fontSize: 10,
font: "Helvetica",
textColor: 0xFF0000, // Red color
});

// Set annotation properties
callout.name = "Text Callout";
callout.flags = 4; // Read-only

// Create a text field inside the callout
var field = doc.createAnnot({
type: "Annot",
subtype: "Text",
parent: callout,
rect: [5, 5, callout.rect[2] - callout.rect[0] - 10, callout.rect[3] - callout.rect[1] - 10],
borderStyle: "Solid",
borderColor: 0x000000, // Black border
fillColor: 0xFFFFFF, // White background
font: "Helvetica",
fontSize: 10
});

// Add annotations to the document
doc.addField(callout);
doc.addField(field);
}

// Create the text callout and text field on document open
this.addField("Open", "doc.createTextCalloutAndField()");

// Function to format pasted text
function formatPastedText() {
// Get the active document and text field
var doc = this;
var field = app.activeField;

// Check if a text field is active
if (field && field.type === "text") {
field.setFont("Helvetica");
field.setFontSize(10);
field.setTextColor(0xFF0000); // Red color
} else {
app.alert("Please select the text field in the callout to paste into.");
}
}

// Add a custom paste event handler
app.addPasteHandler(formatPastedText);

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 ,
Dec 06, 2023 Dec 06, 2023

There are a LOT of errors in this code. Did you get it from ChatGPT, by any chance?

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 ,
Dec 06, 2023 Dec 06, 2023
LATEST

You're code makes no sense, you can't just make things up. To begin, there's no such thing as a "TextCallout" annotation. There is a "FreeText" annotation that can include a callout arrow.  The "addAnnot" function only needs to be called once And the information about where the callout arrow points needs to be included. 

As pointed out by Try67, there are many other errors in the code. Perhaps you should start by looking up the actual information on how to do this? 

Here's the JavaScript reference entry for the annotation object. 

https://opensource.adobe.com/dc-acrobat-sdk-docs/library/jsapiref/JS_API_AcroJS.html#annotation

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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