Skip to main content
Robin Müller
Participating Frequently
February 5, 2023
Question

textFamily property of span problem

  • February 5, 2023
  • 4 replies
  • 717 views

Hello,
My problem is that I don't understand why the fontFamily property of the SPAN object doesn't accept a method that returns an array, but if the fontFamily property is passed an array directly, everything works. To make a long story short. Here are two cases:

objA = {
	text: "Hello, World",
	alignment: "left",
	fontFamily: ["Helvetica", "sans-serif"]
};
getField("myTextFieldName").richValue = [objA] // WORKS!


objA = {
	text: "Hello, World",
	alignment: "left",
	fontFamily: function(){return ["Helvetica", "sans-serif"]}
};
getField("myTextFieldName").richValue = [objA] // NOT WORKING!

I need to randomly generate a font type in a form based on a button press. Therefore I need to pass the fontFamily property the result of the function, unfortunately the console doesn't tell me anything and I can't find why it doesn't work. The text appears, other properties like alignment and textSize.. too, but fontFamily does not.

 

var objSpan = {
	text: "Hello, World",
	alignment: "left",
	textColor: ["RGB", 0/255, 0/255, 0/255],
	textSize: 12,
	linespacing: 20,
	fontFamily: 	function(){
					var randomFont = [
						["Calibri", "sans-serif"], //1
						["Helvetica", "sans-serif"], //2
						["Myriad Pro", "sans-serif"], //3
						["Adobe Caslon Pro", "serif"], //4
						["Adobe Garamond Pro", "serif"], //5
						["Arno Pro", "serif"], //6
						["Arno Pro SmText", "serif"], //7
						["Century", "serif"], //8
						["Corbel", "sans-serif"], //9
						["Georgia", "serif"], //10
						["Gill Sans MT", "sans-serif"], //11
						["Chaparral Pro", "serif"], //12
						["Segoe UI", "sans-serif"], //13
						["Times New Roman", "serif"] //14
					];
					var fo1 = Math.random()*(randomFont.length);//fo1 je náhodné reálné číslo v intervalu <0;14)
					var fo2 = Math.floor(fo1);// fo2 is a natural number from the interval <0;13>
					return randomFont[fo1]// returns the 0th to 13th element of the array randomFont
					},

	fontWeight: 400,
	fontStyle: "normal",
	fontStretch: "normal"
};
getField("myTextFieldName").richValue = [objSpan];

Thanks to much!

This topic has been closed for replies.

4 replies

Bernd Alheit
Community Expert
Community Expert
February 6, 2023

You defines a function but doesn't use the function.

Robin Müller
Participating Frequently
February 5, 2023
objA = {
	text: "Hello, World",
	alignment: "left",
	fontFamily: ["Helvetica", "sans-serif"]
};
getField("myTextFieldName").richValue = [objA] // WORKS!


objA = {
	text: "Hello, World",
	alignment: "left",
	fontFamily: function(){return ["Helvetica", "sans-serif"]}
};
getField("myTextFieldName").richValue = [objA] // NOT WORKING!

objA = {
	text: "Hello, World",
	alignment: "left",
	fontFamily: myRandomFunction() // called function WORKS!
};
getField("myTextFieldName").richValue = [objA] // NOT WORKING!

 

Thanks, I got it sorted. You need to call the random font selection function. I made the fontFamily method, which is wrong. Thanks

Thom Parker
Community Expert
Community Expert
February 6, 2023

To elaborate on Bernds comments. Your code assigns a function to the font family property. But what you really want is the return value from the function. 

Like this:

 

 

objA = {
	text: "Hello, World",
	alignment: "left",
	fontFamily: function(){return ["Helvetica", "sans-serif"]}()
};

 

 

 

Note the parentheses "()" at the end of the function definition.  These force the function to be executed.  

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Robin Müller
Participating Frequently
February 6, 2023

wow, I'm still learning javascript, and this is a cool feature. Execute the function directly after the definition is awesome

Legend
February 5, 2023

ps nothing in console because some contexts simply treat "key: object of wrong type" as if the "key" is completely missing. 

Robin Müller
Participating Frequently
February 5, 2023

show the code how I can implement the result of the function in fontFamily. Thanks

Legend
February 5, 2023

You are assuming that "function-returning-type" is the same as "type". Not at all. If the context needs a value of a specified type, that's what it has to be, not a function. This isn't an Acrobat specific thing. You can use it when building the object because the function was already called.