Skip to main content
Participating Frequently
December 6, 2023
Answered

Document title can't show a 0 / function fillin

  • December 6, 2023
  • 4 replies
  • 961 views

Hi All,

 

I have a document that's used for user agreements that has JavaScript in it to concatenate strings for a document title.

That works great but we've only used it for tagnames sofar, ex: XX123456.

 

Now we also want to use the same document for other tags numbers that start with 0. ex. 012345.

Problem is that the generated documenttitle never shows the 0, and i'm not an expert on this so I really don't know how to solve this.

 

Anyone here got the golden tip for me? 🙂

 

p.s. for some reason the forum says I'm using a bad word in the script so I've uploaded it in a text document.

 

 

 

This topic has been closed for replies.
Correct answer Bernd Alheit

"None" 


Get the value of this field with valueAsString.

4 replies

Thom Parker
Community Expert
Community Expert
December 6, 2023

The thing about numbers is that they don't start with 0. Only strings can start with a "0" character.  A number is just a number. So you have to be careful about how you think about number representation and how strings are built from numbers. 

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Participating Frequently
December 7, 2023

Yeah I understand.

The thing is that Dutch mobile numbers start with 0. But if it's impossible we'll have to live with it 😉

Below is what's happening now. Left is the field we fill in, right is the generated title. 

 

Participating Frequently
December 8, 2023

Get the value of this field with valueAsString.


That did the job! Thank you. 

Bernd Alheit
Community Expert
Community Expert
December 6, 2023

How does you create the document title?

Participating Frequently
December 6, 2023

function fillin(s1, s2, s3, sep = "") {

/*

Purpose: concatenate up to 3 strings with an optional separator

inputs:

s1: required input string text or empty string

s2: required input string text or empty string

s3: required input string text or empty string

sep: optional separator sting

returns:

sResult concatenated string

*/

// variable to determine how to concatenate the strings

var test = 0; // all strings null

var sResult; // reslut string to return

// force any number string to a character string for input variables

s1 = s1.toString();

s2 = s2.toString();

s3 = s3.toString();

//if(sep.toString() == undefined) sep = ''; // if sep is undefined force to null

/*

assign a binary value for each string present

so the computed value of the strings will indicate which strings are present

when converted to a binary value

*/

if (s1 != "") test += 1; // string 1 present add binary value: 001

if (s2 != "") test += 2; // string 2 present add binary value: 010

if (s3 != "") test += 4; // string 3 present add binary value: 100

/* return appropriate string combination based on

calculated test value as a binary value

*/

switch (test.toString(2)) {

case "0": // no non-empty strings passed - binary 0

sResult = "";

break;

case "1": // only string 1 present - binary 1

sResult = "Bruikleen" + sep + s1;

break;

case "10": // only string 2 present - binary 10

sResult = "Bruikleen" + sep + s2;

break;

case "11": // string 1 and 2 present - binary 10 + 1

sResult = "Bruikleen" + sep + s1 + sep + s2;

break;

case "100": // only string 3 present - binary 100

sResult = "Bruikleen" + sep + s3;

break;

case "101": // string 1 and 3 - binary 100 + 001

sResult = "Bruikleen" + sep + s1 + sep + s3;

break;

case "110": // string 2 and 3 - binary 100 + 010

sResult = "Bruikleen" + sep + s2 + sep + s3;

break;

case "111": // all 3 strings - binary 100 + 010 + 001

sResult = "Bruikleen" + sep + s1 + sep + s2 + sep + s3;

break;

default: // any missed combinations

sResult = "";

break;

}

return sResult;

}

Participating Frequently
December 6, 2023

the script