开发者

Java - pdfbox cannot import jar?

Hi there Please can someone help me with this simple problem I believe... I've already asked over 8 experts on a java chat website yet no-one can seem to help me :(. I have downloaded the jar files from http://pdfbox.apache.org/download.html. I have opened blueJ IDE and loaded the jars. When I type in

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

I get an error message:

error has occured cannot find org.apache.pdfbox

I have tried netbeans also and gone to project properties and added the jar, I've also gone to the side menu on netbeans and tried that way. I still get the same error. Can someone please help? I've tried this on 3 different pc's.

okay guys to give me more info. I downloaded the jars and put them in a folder in blueJ i went to options and selected the jar files they say 'loaded'. I also did the same in Netbeans, I've shown the IDE where the Jars are it still does not work here is the full code, its just a sample code taken from the PDFBOX website I am trying.

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 sta开发者_如何学Ctic 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>" );
}

}


This is sorted. I was downloading the JAR file wrong. I checked the file size and noticed it was only 20kb when it was meant to be over 9mb. Thank you to everyone !


What did you do with those jar files once you downloaded them? How did you add them to your project? Netbeans cannot guess where on your computer the jars are located, that's why it doens't work when you do import.... Add the jars to your Netbeans project:

Let's say the jar files are in c:\downloads

With the project selected in netbeans, go to Properties->sources and select Compile Tab, then anvigate to where the jars are and add them. Now your import error should be cleared.


I can't find the Javadocs for this "Pdfbox" product, but I did find some sample code, and none of it seemed to use any classes in org.apache.pdfbox, but rather subpackages like org.apache.pdfbox.pdmodel. Now, knowing this, I can see two things wrong in your import statements: the first line will give the error you show if there are in fact no classes in org.apache.pdfbox and you don't need to import that package; the second line will give an error because `org.apache.pdfbox.pdmodel is itself a package, but you're trying to import it as if it's a class. I am sure one of these two issues -- or both -- are your actual problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜