Expand my Community achievements bar.

XML with Textfield values

Avatar

Level 5

I would like to edit a textfield with default text in it. For example textfield would look like:

"Blue Car's are faster because they run on a special fuel. Jim bought the Blue car model [textfield.rawValue]"

I'd like to add a textfield value in this text. However this gets complicated mixing xml and javascript. Is there a way to do this in the xml? If I try and script this senetence it in javascript the rich text gets lost. Any ideas? My actual use is more complicated than this so I have tried to simplify it. Thanks.

To clarify.

I either want to add textfield values to be included in an xml script thus giving me a Rich Text sentance with textfield values in the sentence where I add them.

or

I want javascript to be able to script the text into rich features such as Bold and italics. With javascript I can easily add a textfield value.

8 Replies

Avatar

Level 10

Hi,

To update a rich text value in JavaScript you need to use loadXML on the fields value property and pass in a string with the rich text markup.  In JavaScript you might find it easier to define the rich text as an E4X string, for example;

var defaultText =

<body xmlns="http://www.w3.org/1999/xhtml" xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">

  <p style="text-decoration:none;letter-spacing:0in">

    <span style="color:#0000ff;font-weight:bold">Blue</span>

        Car's are faster because they run on a special fuel. Jim bought the Blue car model {textfield.rawValue}

    </p>

</body>;

TextField1.value.exData.loadXML(defaultText.toXMLString(), false, true);

This way you can reference the value of other fields by putting them in braces, such as {textfield.rawValue}. 

If you put this code in the exit event of textfield and have a field called TextField1 then that will get updated with the rich text value.

Is that the sort of thing you are after, or have I misunderstood?

Bruce

Avatar

Level 5

Bruce,

This is a great help, thank you. However I must be missing something. I get an error in the console that says:

GeneralError: Operation failed. XFAObject.exData:5:XFA:form1[0]:Container[0]:Button1[0]:click Invalid property get operation; value doesn't have property 'exData'

Linked is my test file:

https://workspaces.acrobat.com/?d=pKNzY07VHK24sDmEwDdVlg

Avatar

Level 10

Hi,

You just need to set your text field to "Rich Text" on the Field tab of the object palette and then it will be ok.

Bruce

Capture.PNG

Avatar

Level 2

May I ask how you managed to have the variable defaultText accept an XML string?

var defaultText

When I copy you sample code and go to the PDF preview pane(LC Designer v11),

a) it doesn't work;

b) I get the following when I return to the XML view pane:

 

     var defaultText = ;

     TextField1.value.exData.loadXML(defaultText.toXMLString(), false, true);

 

          <body xmlns="http://www.w3.org/1999/xhtml" xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">...

The variable is always empty (only the quotes would show) after a PDF preview and the XML string is returned as a child of the script element.  

Avatar

Level 10

Hi Diane,

We can use XML in JavaScript code because the JavaScript that Adobe Reader uses is SpiderMonkey which is one of the few that supports E4X, https://developer.mozilla.org/en/docs/E4X, so JavaScript has a XML type ... if you did a typeof(defaultText) you would get "xml".

It might be this forum doing some formatting on your post, but in the XML Source view you should see the code but with the XML special characters escaped, so something like;

            <event activity="click" name="event__click">

               <script contentType="application/x-javascript">var defaultText =

&lt;body xmlns="http://www.w3.org/1999/xhtml" xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/"&gt;

  &lt;p style="text-decoration:none;letter-spacing:0in"&gt;

    &lt;span style="color:#0000ff;font-weight:bold"&gt;Blue&lt;/span&gt;

        Car's are faster because they run on a special fuel. Jim bought the Blue car model {textfield.rawValue}

    &lt;/p&gt;

&lt;/body&gt;;

TextField1.value.exData.loadXML(defaultText.toXMLString(), false, true);

</script>

            </event>

I'm happy to look at your form if you can post a link to it in this thread.  Are you getting any error message?

Regards

Bruce

Avatar

Level 2

Thanks Bruce,

Making the change you suggested, I was able to get my script to work. 

When the location is selected from the drop-down list,  

the court_website_copy textfield is automatically updated with the new website HTML anchor value .

var location= $.boundItem(xfa.event.newText);

var address='';

var phone='';

var URL='';

//var URLsuffix = translate(lower-case(location),' ','-');

if (location == "Kamloops") {

address = " B.C. Court of Appeal \u000A 223 – 455 Columbia Street \u000A Kamloops, BC \u000A V2C 6K4";

phone = "2508284344";

URL=http://www.provincialcourt.bc.ca/court-location/kamloops;

}

var website = '&lt;body xmlns="http://www.w3.org/1999/xhtml" xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/"&gt;&lt;p&gt;&lt;a href="'+URL+'"&gt;'+location+'&lt;/a&gt;&lt;/p&gt;&lt;/body&gt;';

this.resolveNode("Page1.court.court_address_copy").rawValue = address;

this.resolveNode("Page1.court.court_phone_copy").rawValue = phone;

this.resolveNode("Page1.court.court_website_copy").value.exData.loadXML(website, false, true);

Now I need to figure out why the string functions are not working.

E.g. var URLsuffix = translate(lower-case(location),' ','-');

Instead of keeping these addresses and phone numbers in my script, I should  be able to load them from an attached XML.

As anyone done this yet?  Is it supported?

Avatar

Level 10

Hi,

Not sure what the translate and lower-case functions.  I think what you want is;

var URLsuffix = location.replace(/ /g,"-").toLowerCase();

How you handle storing the addresses and phone numbers depends on how often they are likely to change and how you are delivering your form to your users.  If you have access to some of the Adobe server products or not and how you are submitting the form.

You can definitely add them to your form either as xml or as JavaScript values before the form goes out.

Regards

Bruce

Avatar

Level 5

I used Script Object's to run functions for me and enter data into my XML string based on information entered in another field. Here are couple of examples if this helps.

//Script for entering the text for the subject

var subjectText =<body xmlns="http://www.w3.org/1999/xhtml" xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/"><p style="text-decoration:none;letter-spacing:0in">

    Regulatory Department   Disclosure Report (DR), "{form1.VDR.Content.OnethruTen.OneThruFour.threeSubform.Subject.rawValue}",  Item {form1.MP.MPVDR.Number.rawValue}.</p>

    </body>;

form1.Letter.Content.letterSubjectsubform.letterSubject.value.exData.loadXML(subjectText.toXMLString(), false, true);

//Script for formating the OAP Letter body content

    var bodyText =<body xmlns="http://www.w3.org/1999/xhtml" xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/"><p style="text-decoration:none;letter-spacing:0in">

    This letter formally submits the enclosed DR which includes: description of the apparent violation, how the violation(s) was discovered, description of immediate action(s) taken, and the person responsible for the root cause/corrective action plan (CP). The CP will be submitted to.

    <br /><br />

    Please send any inquiry or request for assistance to Regulatory Department and Enforcement {FOCALTITLE.value}, {FOCALFULLNAME.value} at {form1.Content.caFaainfo.focalInfo.focalTelephone.rawValue}.

    <br /><br />The information being forwarded to the IAP by or with this correspondence is considered proprietary to Lorax Company and/or its suppliers, and is provided on a confidential basis.

    <br /><br />

    Sincerely,</p>

    </body>;

form1.Letter.Content.letterBodysubform.letterBody.value.exData.loadXML(bodyText.toXMLString(), false, true);

.