将 Javascript 放入 PDF 时出现语法错误
我正在尝试将一些 JavaScript 应用于 pdf,使其以静默方式打印。我找到了这段代码并将其粘贴进去,但出现以下错误。
语法错误:缺少;前 语句 1:第 2 行
这突出显示了下面代码的前 2 行:
Document document = new Document();
FileOutputStream fos = new FileOutputStream("APP_PERSONAL.pdf");
这是完整的代码:
Document document = new Document();
FileOutputStream fos = new FileOutputStream("APP_PERSONAL.pdf");
try {
PdfWriter writer = PdfWriter.getInstance(document, fos);
document.open();
write.addJavaScript("this.print({bUI: false, bSilent: true, bShrinkToFit: true});",false);
write.addJavaScript("this.closeDoc();");
document.add(new Chunk("Silent Auto Print"));
document.close();
} catch (DocumentException e) {
e.printStackTrace();
}
我还不太了解缺少的分号在哪里。这是否意味着该行开头的第二行代码缺少它?
I am trying to apply some javascript to a pdf to make it print silently. I found this chunk of code and pasted it in, but get the error below.
SyntaxError: missing ; before
statement 1: at line 2
This highlights the first 2 lines of the code below:
Document document = new Document();
FileOutputStream fos = new FileOutputStream("APP_PERSONAL.pdf");
Here's the full code:
Document document = new Document();
FileOutputStream fos = new FileOutputStream("APP_PERSONAL.pdf");
try {
PdfWriter writer = PdfWriter.getInstance(document, fos);
document.open();
write.addJavaScript("this.print({bUI: false, bSilent: true, bShrinkToFit: true});",false);
write.addJavaScript("this.closeDoc();");
document.add(new Chunk("Silent Auto Print"));
document.close();
} catch (DocumentException e) {
e.printStackTrace();
}
I don't know enough yet to understand where the missing semi-colon is. Does it mean it's missing on the second line of code at the start of that line?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那是Java,不是Javascript。
此 Java 代码会将以下 Java脚本 代码添加到 PDF 文件中:
That's Java, not Javascript.
This Java code will add the following Javascript code to a PDF file:
实际上并没有缺少分号。只是 javascript 解释器试图将您的 Java 解释为 Javascript,但无法理解它。具体来说,它无法找出“Document document”的有效语法树,因此它似乎决定“Document”本身就是一个完整的语句,并希望您用分号将其与以下语句分开。
正如 SLaks 所说,尝试仅粘贴两行 javascript(this.print()... 等),看看是否有效。
There's not really a missing semicolon. It's just that the javascript interpreter is trying to interpret your Java as Javascript and can't make sense out of it. Specifically it can't figure out a valid syntax tree for "Document document", so it seems to be deciding that "Document" is a complete statement in itself, and wants you to separate it from the following statement with a semicolon.
As SLaks said, try pasting in just the two lines of javascript (this.print()... etc.) and see if that works.