Search code examples
javapdfbox

Can't get a image from resources


I have problem to get an image from another package of my Eclipse project! I saw this post here My code is this CreateTableOnPDF.java:

288    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
289    InputStream input = classLoader.getResourceAsStream("images/sun.png");
290    PDJpeg img = new PDJpeg(doc, input);

I getting this exception:

Exception in thread "main" java.lang.IllegalStateException: 
at org.apache.pdfbox.pdmodel.graphics.xobject.PDJpeg.setPropertiesFromAWT(PDJpeg.java:132)
at org.apache.pdfbox.pdmodel.graphics.xobject.PDJpeg.<init>(PDJpeg.java:113)
at MainClasses.CreateTableOnPDF.main(CreateTableOnPDF.java:290)

I don't know what I am doing wrong? Maybe is the a lot hour a have spend already infront of my monitor.

Here is my project structure: enter image description here

Thanks for your attention and time!


Solution

  • Please check the API docs, you should put JPEG data, not PNG data.

    public PDJpeg(PDDocument doc, InputStream is) throws IOException
    

    Parameters:

    • doc - The document to create the image as part of.
    • is - The stream that contains the JPEG data.

    Throws:

    • IOException - If there is an error reading the JPEG data.