These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

How to use util.stringFromStream() method or convert strings to utf-8?

tom502
Registered: Jun 11 2008
Posts: 3

I want to convert all strings on my form to utf-8 encoding. I want to be sure that all strings are in utf-8 encoding before I will call applyXSL method. I tried something like this but its not working. Any ideas?

var xmlData = xfa.data;
var xmlStr = util.stringFromStream(xmlData, "utf-8");
var xfa = XMLData.parse(xmlStr,false);

thomp
Expert
Registered: Feb 15 2006
Posts: 4411
LiveCycle really really likes UTF-16 so you might have some issues here you can't control.

First, "xfa.data" is an XML node not a stream. Specifically it's already an XMLData node so there is no need to reparse it. And second, even if it was a stream, the "utf-8" input argument to "stringFromStream" indicates the encoding of the data in the stream, not a conversion.

To get a string out "xfa.data" use:

xfa.data.saveXML();

You can convert it to a stream with UTF-8 encoding, but this won't help. All strings in JavaScript are encoded with "UTF-16", it's automatic.

You also cannot overwrite, or redefine the xfa node. This is a non-generic parameter, meaning that its connected to data underneath the covers in Acrobat.

What is it that you are trying to accomplish?

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

tom502
Registered: Jun 11 2008
Posts: 3
I am trying to use applyxsl on nodes contains Polish language specific characters. For example - ĘĄŚŹ. But after use of applyxsl on that node Polish characters disappear. I've got a sample code in which all strings in xml and xsl are converted to utf-8 encoding before calling applyXSL. This function should works fine then.

var xmlData = doc.getDataObjectContents(xml);
var xmlStr = util.stringFromStream(xmlData, "utf-8");
var xfa = XMLData.parse(xmlStr,false);

var xslData = doc.getDataObjectContents(xsl);
var xslStr = util.stringFromStream(xslData, "utf-8");
var res = xfa.applyXSL(xslStr);


But I'm not able to use it. I don't need to call getDataObjectContents() because my form doesn't get data from a file and then I don't know what should be in var xmlData. I just want to be sure the xslstr is an utf-8 encoded string.
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
In the example, the contents of the attachement are already in utf-8. They are not being converted. Try it out. Create an XML file encoded in UTF-16, attach it to a PDF and then use the code in your example to read it out.

Try something this, If it works then you can apply your XSL.

var strXFAData = xfa.data.saveXML();
// Force UTF-8 conversion
var stmXFAData = util.streamFromString(strXFAData,"utf-8");
var str8XFAData = util.stringFromStream(stmXFAData,"utf-8");
// Parse back into an XML document
var xmlXFAData = XMLData.parse(str8XFAData, false);
// Load it back into the datasets
xfa.data.loadXML(xmlXFAData ,1,1)

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

Pallavi_Gondkar
Registered: Oct 2 2008
Posts: 22
Hi,

Is it possible to read binary files like excel, doc in ADOBE?
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Yes and no.

If a binary file is a file attachment to the PDF, then yes. IN Acrobat JavaScript file attachments are called Data Objects, and there is a document funciton for reading bytes out of the attached file, "doc.getDataObjectContents()".

Other than this there is no general way to get binary data out of a file using Acrobat JavaScript.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/]http://www.adobe.com/devnet/acrobat/[/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

cyprd
Registered: Oct 21 2008
Posts: 49
Hi,

If I use that function on attached binary file to PDF, how do I get the actual content? The result is a stream right, and if I use stringfromstream function, I am not able to see content of a doc file for instance.
Thanks

Adobe LiveCycle ES 8.2.1 (JBoss & Win)

thomp
Expert
Registered: Feb 15 2006
Posts: 4411
If the data is binary then the "stringFromStream()" function isn't going to do any good. That only works for streams where the underlying data is text.

In Acrobat JavaScript, a stream object has a "read()" function, so you can read the contents of a stream directly. The output of the read function is a Hexidecimal encoded string. JavaScript is, for the most part, a string handling language. Core JavaScript doesn't contain anything for handling real binary data. Acrobat's work-around is to encode the binary data in hexidecimal.

The API for the stream object is pretty limited. You can't find out the length of the stream or the current read postion. You simply call the read function until it returns null. Once a stream is read it's dead. There's nothing you can do with it beyond this point.

For example:

var binData = doc.getDataObjectContents("MyFile");
// Read 4 bytes from the file
var str4Bytes = binData.read(4);

This code returns 8 hex characters. Every 2 characters is a byte. If those 4 bytes are an integer then you have to do the conversion to a number like this:

var nVal = parseInt(str4Bytes,16);

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/]http://www.adobe.com/devnet/acrobat/[/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

cyprd
Registered: Oct 21 2008
Posts: 49
This would do, if I can read the entire file as HEX, than I should be able to convert it back to binary in the textfield and bind that to the XML structure and than submit it over HTTP request.
Will try that, if that works, than it will be super cool :-)

Adobe LiveCycle ES 8.2.1 (JBoss & Win)

thomp
Expert
Registered: Feb 15 2006
Posts: 4411
I doubt you'll be able to successfully maintain the fidelity of the binary data by writing it into a text field.

You're best bet is to write it directly into a CDATA section of the data model right before it's submitted. I don't know if this will work but it's worth a try. You also might consider creating your own custom XML for submit using the Acroform "doc.submitForm()" fucntion.

However, I'm not real sure you be able to do either of these things. JavaScript just isn't very good with binary data. Be careful. I suspect you'll hit a point were you can't move the data from one place to another in binary.

Good Luck,
Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/]http://www.adobe.com/devnet/acrobat/[/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

cyprd
Registered: Oct 21 2008
Posts: 49
Hi Thom,

looks like it. Currently I have working e-form system that uses some other software for processing. I would like to migrate that to LiveCycle ES, however, it can not be done in one turn. I need to replace those e-forms with acrobat forms first and make sure, that the backend gets the data in its own current way, which is XML file that contains all the form fields but apart form that, the XML contains base64 encoded attachments (word, zip, cad drawings etc)
And for this period before the livecycle is implemented, I need to figure this out, how to get those attachments from adobe pdf form to xml data.

Have a good one

Jan

Adobe LiveCycle ES 8.2.1 (JBoss & Win)

thomp
Expert
Registered: Feb 15 2006
Posts: 4411
I've given a quick look through the methods availible for manipulating XMLData node (i.e., an XML node in LC) and they all use strings to handle data. But there are some parts of the XFA that do handle binary data. I don't know if you could be sneaky and use one of these to fake out construction of the XML. For example, the image object uses binary data. If you could write your binary file data into this, then copy the underlying image data node into the dataset for the submit. Have no idea if this is possible, but it's all I can think of.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/]http://www.adobe.com/devnet/acrobat/[/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script