【发布时间】:2020-06-10 18:37:31
【问题描述】:
我在 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 您可以设置编码,然后将文本解析为行和字段以填充您的表单。
-
@joelgeraci 非常感谢您的建议。我会关注并支持它。我也会阅读文档。但如果你有经验,你能举个例子吗?
-
我将整理一个示例并将其托管在某个地方。
标签: javascript utf-8 adobe acrobat