简体   繁体   中英

How can I define the encoding of strings in JavaScript script in Adobe Acrobat DC

I use JavaScript in Adobe Acrobat DC to batch fill a fillable PDF form and make copies for each entry in a tab delimited file.

The file is in UTF-8 And the characters (Czech) č ř and š Are visible when opening the text file.

Also when I manually copy and paste the characters in the PDF form I can see the characters.

However when I run the JavaScript action these characters are not inserted correctly. There are some weird characters instead.

The JavaScript is this:

var fileName = "/Users/username/tmp/data.txt";  // the tab delimited text file containing the data
var outputDir = "/Users/username/tmp/";    // make sure this ends with a '/'

var err = 0;
var idx = 0;
while (err == 0) {
    err = this.importTextData(fileName, idx); // imports the next record
    if (err == -1)
        app.alert("Error: Cannot Open File");
    else if (err == -2)
        app.alert("Error: Cannot Load Data");
    // else if (err == -3)
        // We are not reporting this error because it does
        // indicate the end of our data table: We've exhausted
        // all rows in the data file and therefore are done with
        // processing the file. Time to exit this loop. 
        // app.alert("Error: Invalid Row");
    else if (err == 1)
        app.alert("Warning: User Cancelled File Select");
    else if (err == 2)
        app.alert("Warning: User Cancelled Row Select");
    else if (err == 3)
        app.alert("Warning: Missing Data");
    else if (err == 0) {
        this.saveAs(outputDir + this.getField("Text1").value + "_" + this.getField("Text2").value + ".pdf"); // saves the file
        idx++;
    }
}```

Please note that credit for this JavaScript goes to Karl Heinz Kremer from http://khkonsulting.com/2015/10/batch-import-excel-data-into-pdf-forms/


I'm really not a fan of the Doc.importTextData functionality for a number of reasons. No control over the encoding is just one of them. Instead, use Util.readFileIntoStream() then Util.stringFromStream() where you can set the encoding then parse the text into rows and then fields to populate your form

Due to security restrictions for saving files using a path, this script must be run from the Acrobat JavaScript console. Column names in the XLS and field names in the PDF must match exactly. Export to the XLS to CSV UTF-8. Field names are case sensitive. Columns that do not have a corresponding field in the PDF will be ignored.

Use: Open your form template, then run this code from the console.

console.clear();
var baseFileName = this.documentFileName.replace(".pdf", "");
var fileStream = util.readFileIntoStream();
var fileString = util.stringFromStream(fileStream, "utf-8");

var rows = fileString.split("\n");
var columns = rows[0].split(",");

for (var i = 1; i < rows.length; i++) {
    var row = rows[i].split(",");
    for (var j = 0; j < columns.length; j++) {
        var fieldName = columns[j].replace(/[^\x00-\x7F]/g, "");
        var value = row[j];
        console.println(fieldName+": "+value)
        try {
            var field = this.getField(fieldName);
            field.value = value;
        }
        catch (err) { }
    }
    // Customize this area for your own needs. This area builds the output filename.
    var outputFileName = this.getField("last_name").value + "_" + this.getField("first_name").value;
    // Save the file as a copy so that the template can be reused 
    this.saveAs({
        cPath: outputFileName+ ".pdf",
        bCopy: true
    })
}
this.resetForm();

I posted a working example set of files here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM