如何在 Adobe Acrobat DC 的 JavaScript 脚本中定义字符串编码
How can I define the encoding of strings in JavaScript script in Adobe Acrobat DC
我在 Adobe Acrobat DC 中使用 JavaScript 批量填写可填写的 PDF 表单,并为制表符分隔文件中的每个条目制作副本。
文件是 UTF-8 并且字符(捷克语)č ř 和 š 在打开文本文件时可见。
另外,当我手动复制和粘贴 PDF 表单中的字符时,我可以看到字符。
然而,当我 运行 执行 JavaScript 操作时,这些字符没有正确插入。相反,有一些奇怪的字符。
JavaScript是这样的:
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/
出于多种原因,我真的不喜欢 Doc.importTextData 功能。无法控制编码只是其中之一。相反,使用 Util.readFileIntoStream() 然后 Util.stringFromStream() 在这里你可以设置编码然后将文本解析为行然后字段来填充你的表单
由于使用路径保存文件的安全限制,此脚本必须 运行 来自 Acrobat JavaScript 控制台。 XLS 中的列名称和 PDF 中的字段名称必须完全匹配。将 XLS 导出为 CSV UTF-8。字段名称区分大小写。 PDF 中没有相应字段的列将被忽略。
使用:打开您的表单模板,然后从控制台运行此代码。
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();
我发布了一组工作示例文件here
我在 Adobe Acrobat DC 中使用 JavaScript 批量填写可填写的 PDF 表单,并为制表符分隔文件中的每个条目制作副本。
文件是 UTF-8 并且字符(捷克语)č ř 和 š 在打开文本文件时可见。
另外,当我手动复制和粘贴 PDF 表单中的字符时,我可以看到字符。
然而,当我 运行 执行 JavaScript 操作时,这些字符没有正确插入。相反,有一些奇怪的字符。
JavaScript是这样的:
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/
出于多种原因,我真的不喜欢 Doc.importTextData 功能。无法控制编码只是其中之一。相反,使用 Util.readFileIntoStream() 然后 Util.stringFromStream() 在这里你可以设置编码然后将文本解析为行然后字段来填充你的表单
由于使用路径保存文件的安全限制,此脚本必须 运行 来自 Acrobat JavaScript 控制台。 XLS 中的列名称和 PDF 中的字段名称必须完全匹配。将 XLS 导出为 CSV UTF-8。字段名称区分大小写。 PDF 中没有相应字段的列将被忽略。
使用:打开您的表单模板,然后从控制台运行此代码。
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();
我发布了一组工作示例文件here