Java - pdfbox无法导入jar?

时间:2011-09-15 18:47:25

标签: java pdf netbeans pdfbox bluej

您好,有人可以帮我解决这个简单的问题我相信...我已经在java聊天网站上询问过8位专家但是没有人能帮助我:( 。 我从中下载了jar文件 http://pdfbox.apache.org/download.html。 我打开了blueJ IDE并加载了jar。当我输入

import org.apache.pdfbox.*; 
import org.apache.pdfbox.pdmodel; 
import org.apache.pdfbox.pdmodel.PDPage; 

我收到错误消息:

error has occured cannot find org.apache.pdfbox

我也试过netbeans并且去了项目属性并添加了jar,我也去了netbeans的侧边菜单并尝试了这种方式。我仍然得到同样的错误。有人可以帮忙吗?我在3台不同的电脑上试过这个。

好的家伙给我更多信息。我下载了罐子并将它们放在blueJ的文件夹中我去了选项并选择了他们说'加载'的jar文件。我也在Netbeans中做了同样的事情,我已经展示了IDE,其中Jars仍然不起作用,这里是完整的代码,它只是从我正在尝试的PDFBOX网站上获取的示例代码。

import org.apache.pdfbox.exceptions.*;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;

/**
 * This will create a blank PDF and write the contents to a file.
  */
public class CreateBlankPDF
{

/**
 * This will create a blank PDF and write the contents to a file.
 *
 * @param file The name of the file to write to.
 *
 * @throws IOException If there is an error writing the data.
 * @throws COSVisitorException If there is an error while generating the document.
 */
public void create( String file ) throws IOException, COSVisitorException
{
    PDDocument document = null;
    try
    {
        document = new PDDocument();
        //Every document requires at least one page, so we will add one
        //blank page.
        PDPage blankPage = new PDPage();
        document.addPage( blankPage );
        document.save( file );
    }
    finally
    {
        if( document != null )
        {
            document.close();
        }
    }
}

/**
 * This will create a blank document.
 *
 * @param args The command line arguments.
 *
 * @throws IOException If there is an error writing the document data.
 * @throws COSVisitorException If there is an error generating the data.
 */
public static void main( String[] args ) throws IOException, COSVisitorException
{
    if( args.length != 1 )
    {
        usage();
    }
    else
    {
        CreateBlankPDF creator = new CreateBlankPDF();
        creator.create( args[0] );
    }
}

/**
 * This will print the usage of this class.
 */
private static void usage()
{
    System.err.println( "usage: java org.apache.pdfbox.examples.pdmodel.CreateBlankPDF <outputfile.pdf>" );
}

}

3 个答案:

答案 0 :(得分:1)

这是排序的。我正在下载JAR文件错误。我检查了文件大小,注意到它只有20kb,当它意味着超过9mb。谢谢大家!

答案 1 :(得分:0)

下载后,您对这些jar文件做了什么?你是如何将它们添加到项目中的? Netbeans无法猜出你的计算机上的罐子位于哪里,这就是为什么它在导入时不起作用....将罐子添加到你的Netbeans项目中:

假设jar文件位于c:\ downloads

在netbeans中选择项目后,转到Properties-&gt; sources并选择Compile Tab,然后转到jar所在的位置并添加它们。现在应该清除导入错误。

答案 2 :(得分:0)

我找不到这个“Pdfbox”产品的Javadocs,但我确实找到了一些示例代码,但它们似乎都没有使用org.apache.pdfbox中的任何类,而是使用org.apache.pdfbox.pdmodel之类的子包。现在,知道这一点,我可以在import语句中看到两个错误:如果org.apache.pdfbox中实际上没有类并且您不需要导入该包,则第一行将显示您显示的错误;第二行会出错,因为`org.apache.pdfbox.pdmodel本身就是一个包,但你试图导入它就像它是一个类一样。我确信这两个问题中的一个 - 或两者 - 都是你的实际问题。