简体   繁体   English

使用pdfbox创建pdf时出错:此令牌后预期的标识符

[英]Error while using pdfbox to create pdf : Identifier expected after this token

I am working on a plain Java project in eclipse juno using jre6/jdk6 as runtime/compiler. 我正在使用jre6 / jdk6作为运行时/编译器在eclipse juno中开发一个普通的Java项目。 I wish to use apache pdfbox to generate some pdfs. 我希望使用apache pdfbox生成一些pdf。 i have downloaded and added pdfbox 1.8.9 to my build path. 我已经下载并添加了pdfbox 1.8.9到我的构建路径。 now i took a code sample from here , and used it in my application, but it is giving me multiple error which i think is related to some environment problems. 现在我从这里拿了一个代码示例,并在我的应用程序中使用它,但它给了我多个错误,我认为这与一些环境问题有关。

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
public class TestPdf {

PDDocument document = new PDDocument();
// Create a new blank page and add it to the document
PDPage blankPage = new PDPage();
document.addPage( blankPage );
// Save the newly created document
document.save("BlankPage.pdf");
// finally make sure that the document is properly
// closed.
document.close();
}

These are the errors i am getting : 这些是我得到的错误:

Syntax error on token "blankPage", VariableDeclaratorId expected after this token
Syntax error on token ""BlankPage.pdf"", delete this token
Syntax error on token "close", Identifier expected after this token

You should create a method and move some of the code inside the method : 您应该创建一个方法并移动方法中的一些代码:

public class TestPdf {

    PDDocument document = new PDDocument();
    // Create a new blank page and add it to the document
    PDPage blankPage = new PDPage();

    public void createDocument()throws Exception {
        document.addPage(blankPage);
        // Save the newly created document
        document.save("BlankPage.pdf");
        // finally make sure that the document is properly
        // closed.
        document.close();
    }
}

The code that you posted in your question is against the syntax rules of the Java language. 您在问题中发布的代码违反了Java语言的语法规则。 You can read more about the structure of a class here 您可以在此处阅读有关课程结构的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM