将 Javascript 放入 PDF 时出现语法错误

发布于 2024-09-16 21:46:58 字数 822 浏览 2 评论 0原文

我正在尝试将一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

五里雾 2024-09-23 21:46:58

那是Java,不是Javascript。

此 Java 代码会将以下 Java脚本 代码添加到 PDF 文件中:

this.print({bUI: false, bSilent: true, bShrinkToFit: true});
this.closeDoc();

That's Java, not Javascript.

This Java code will add the following Javascript code to a PDF file:

this.print({bUI: false, bSilent: true, bShrinkToFit: true});
this.closeDoc();
始终不够 2024-09-23 21:46:58

实际上并没有缺少分号。只是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文