繁体   English   中英

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

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

我正在使用jre6 / jdk6作为运行时/编译器在eclipse juno中开发一个普通的Java项目。 我希望使用apache pdfbox生成一些pdf。 我已经下载并添加了pdfbox 1.8.9到我的构建路径。 现在我从这里拿了一个代码示例,并在我的应用程序中使用它,但它给了我多个错误,我认为这与一些环境问题有关。

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

这些是我得到的错误:

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

您应该创建一个方法并移动方法中的一些代码:

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

您在问题中发布的代码违反了Java语言的语法规则。 您可以在此处阅读有关课程结构的更多信息

暂无
暂无

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

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