使用 pdfbox 创建 pdf 时出错:此标记后应有标识符
Error while using pdfbox to create pdf : Identifier expected after this token
我正在使用 jre6/jdk6 作为 runtime/compiler 在 eclipse juno 中开发一个普通的 Java 项目。我希望使用 apache pdfbox 生成一些 pdf。我有 downloaded and added pdfbox 1.8.9 to my build path. now i took a code sample from here,并在我的应用程序中使用了它,但它给了我多个错误,我认为这与某些环境问题有关。
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 语言的语法规则。您可以阅读有关 class here
结构的更多信息
我正在使用 jre6/jdk6 作为 runtime/compiler 在 eclipse juno 中开发一个普通的 Java 项目。我希望使用 apache pdfbox 生成一些 pdf。我有 downloaded and added pdfbox 1.8.9 to my build path. now i took a code sample from here,并在我的应用程序中使用了它,但它给了我多个错误,我认为这与某些环境问题有关。
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 语言的语法规则。您可以阅读有关 class here
结构的更多信息