Using Special Characters
Organize data with line breaks and tabs by using special characters in text strings.
When writing JavaScript for Acrobat programs and forms there are special characters that can be inserted into text strings to separate and organize parts of the string. These characters must be "escaped" in the string, that is, preceded by a backslash. The backslash lets the JavaScript engine know that the next character is a special character. In this post I will be examining three of these special characters.
\r - A backslash followed by a lower case r signifies a carriage return (a new line).
\n - A backslash followed by a lower case n signifies a new line. It is very similar to the carriage return but does not act exactly the same.
\t - A backslash followed by a lower case t signifies a tab.
Try running these scripts in the console:
//Script 1
"The quick brown fox\rjumped over the lazy dogs."
//Script 2
"The quick brown fox\njumped over the lazy dogs."
//Script 3
"The\tquick\tbrown\tfox"
//Script 4
"The\tquick\tbrown\nfox\tjumped\tover\rthe\tlazy\tdogs."
Excel Spreadsheet
The spreadsheet displayed in the image above was created by running Script 4 above in the console, selecting & copying the text it returned, and pasting it into cell A1 of an Excel worksheet. In Excel, tabs are cell column separators. New lines, or carriage returns, are row separators. These special characters can be used to build a block of text and pass it into the createDataObject method to create a spreadsheet. The data object created is a PDF attachment. If you run the following script in the console you will see the spreadsheet in the attachment panel of the PDF:
var data="The\tquick\tbrown\nfox\tjumped\tover\rthe\tlazy\tdogs."
this.createDataObject("MyData.xls",data);
Spreadsheet data can also be used in the PDF by creating a popup dialog with one multiline field, and pasting the Excel clipboard into that field.
Dialog Articles
I used this method in the dropdown filler tool I created to paste an Excel column or columns into the dialog window, then split the text by lines to automate the process of filling dropdown fields or list boxes with entries.
Email Button - Email Body
\r or \n can be used in the cMsg parameter (email body) of the mailDoc method to create line breaks. These special characters should be doubled up in order to create extra lines between paragraphs. To keep the text organized it's helpful to put line breaks in the script where the actual line breaks will be. If you use line breaks in the script, remember to close the string with a quotation, concatenate the text string with a plus sign at the end of the line, and open the next line with a quote:
var body = "Hi Mr. Smith,\r\r"+
"I'm sure your schedule is very busy, so this email is simply to remind you of your upcoming interview with Jack Jones, who is a candidate for electrician supervisor.\r\r"+
"The interview will be at 2:00 pm this Friday.\r\r"+
"Please let me know if there's anything I can help you with to prepare to interview this candidate.\r\r"+
"Best,\r\r"+
"Richard Thompson"
this.mailDoc({bUI:true, cTo:"ISmith@abccorp.com",cMsg:body});