views:

15

answers:

2

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?

A: 

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();
SLaks
Oh I see, thanks SLaks. I wasn't sure about the differences between the two. I have to do some reading :)
drew
You're welcome.
SLaks
A: 

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.

LarsH