Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


Custom Field Format with a Superscript

Participant ,
May 17, 2021 May 17, 2021

Copy link to clipboard

Copied

I am trying to add custom formatting to a form field containing a superscript. Specifically, this form will have units of measurement tied to all its fields, and some are units squared. I have the following taken from another post for formatting in feet with 2 decimal places:

if((event.value == "") || (event.value == 0))
    event.value = "";
else
    event.value = util.printf("%,0.2f feet",event.value);

to be placed in the custom format script section on the format tab for a field. I do not have a full grasp of the printf syntax, but I think I can adapt it to my needs once I know how the supercript should work.

So I need help having a field display ft2 with the 2 shown as a superscript.

 

Thanks in advanced.

 

JM

TOPICS
Create PDFs, JavaScript, PDF forms

Views

630

Likes

Translate

Translate

Report

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

correct answers 2 Correct answers

Adobe Community Professional , May 17, 2021 May 17, 2021
Superscripts fall in to the category of Rich Text, i.e. marked up text. For that the field needs to be set to accecpt rich text and the content for the field is built with spans.   Here's the reference entry for spans, it includes an example. https://help.adobe.com/en_US/acrobat/acrobat_dc_sdk/2015/HTMLHelp/#t=Acro12_MasterBook%2FJS_API_AcroJS%2FSpan_properties.htm%23TOC_superscriptbc-8&rhtocid=_6_1_8_72_0_7 This is a bit more than just the normal text formatting.  

Likes

Translate

Translate
Adobe Community Professional , May 17, 2021 May 17, 2021
Thom's solution will work, but there's a much easier one, since there's actually a special Unicode character for a superscript 2, and the code for it can be added directly to your original script, like this:   if((event.value == "") || (event.value == 0)) event.value = ""; else event.value = util.printf("%,0.2f feet\u00B2",event.value);

Likes

Translate

Translate
Adobe Community Professional ,
May 17, 2021 May 17, 2021

Copy link to clipboard

Copied

Superscripts fall in to the category of Rich Text, i.e. marked up text. For that the field needs to be set to accecpt rich text and the content for the field is built with spans.   Here's the reference entry for spans, it includes an example.

https://help.adobe.com/en_US/acrobat/acrobat_dc_sdk/2015/HTMLHelp/#t=Acro12_MasterBook%2FJS_API_Acro...

 

This is a bit more than just the normal text formatting.  

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

Likes

Translate

Translate

Report

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
Participant ,
May 17, 2021 May 17, 2021

Copy link to clipboard

Copied

So if the field is formatted ahead of time to accept rich text, can I make use of spans in the custom format script? Can someone help me move forward in that direction?

 

I've just tried this, but it doesn't even accept it as I have it...

if((event.value == "") || (event.value == 0)){
    event.value = "";
}else{
	var spans new Array();
    spans[0] = new Object();
	spans[0].text = event.value;
	
	spans[1] = new Object();
	spans[1].text = " ft";
	
	spans[2] = new Object();
	spans[2].text = "2";
	spans[2].superscript = true;
	
	event.richValue = spans;
}

 This gives me this error:

SyntaxError: missing ; before statement
4:

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
May 17, 2021 May 17, 2021

Copy link to clipboard

Copied

This line is incorrect:

	var spans new Array();

 

needs to be

	var spans = new Array();

 

Looks like you are on the right track.

 

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

Likes

Translate

Translate

Report

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
Participant ,
May 17, 2021 May 17, 2021

Copy link to clipboard

Copied

Ah, the dreaded typo! Thank you, Thom. It is working for me now!

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
May 17, 2021 May 17, 2021

Copy link to clipboard

Copied

You can add the number formatting to the first span.

 

spans[0].text = util.printf("%,0.2f feet",event.value);

 

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

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
May 17, 2021 May 17, 2021

Copy link to clipboard

Copied

Thom's solution will work, but there's a much easier one, since there's actually a special Unicode character for a superscript 2, and the code for it can be added directly to your original script, like this:

 

if((event.value == "") || (event.value == 0))
    event.value = "";
else
    event.value = util.printf("%,0.2f feet\u00B2",event.value);

 

Likes

Translate

Translate

Report

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
Participant ,
May 18, 2021 May 18, 2021

Copy link to clipboard

Copied

LATEST

Ah, this is even cleaner. Thank you everyone for the help. Both solutions work, but I am marking this one as it is more in line with what I was seeking.

Likes

Translate

Translate

Report

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