开发者

SyntaxError while placing Javascript in PDF

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 write开发者_StackOverflow中文版r = 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?


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();


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜