0

I don't want to use large programs like Netbeans Eclipse and so on and should only do a small thing with pdfbox (https://pdfbox.apache.org/) but it is not working. Do I have to use maven etc?

I have downloaded pdfbox-2.0.20.jar and pdfbox-app-2.0.20.jar

Added PATH to environment variable in Windows.

Created this (Document_Creation.java) in Notepad++

import java.io.IOException; 
import org.apache.pdfbox.pdmodel.PDDocument;
  
public class Document_Creation {
    
   public static void main (String args[]) throws IOException {
       
      //Creating PDF document object 
      PDDocument document = new PDDocument();    
       
      //Saving the document
      document.save("C:/folder/my_doc.pdf");
      System.out.println("PDF created");  
    
      //Closing the document  
      document.close();

   }  
}

Open Windows CDM and type: javac Document_Creation.java

But get this error

Document_Creation.java:2: error: package org.apache.pdfbox.pdmodel does not exist
import org.apache.pdfbox.pdmodel.PDDocument;
                                ^
Document_Creation.java:9: error: cannot find symbol
      PDDocument document = new PDDocument();
      ^
  symbol:   class PDDocument
  location: class Document_Creation
Document_Creation.java:9: error: cannot find symbol
      PDDocument document = new PDDocument();
                                ^
  symbol:   class PDDocument
  location: class Document_Creation
3 errors

Looks like I can't access pdfbox with import org.apache.pdfbox.pdmodel.PDDocument;

How can I make this code work with latest java (working, tested with println without pdfbox), Notepad++ and Windows CMD?

Xtreme
  • 1,601
  • 7
  • 27
  • 59
  • You need to add the classpath for the pdfbox jar files, see e.g. https://stackoverflow.com/questions/2096283 (btw you only need pdfbox-app-2.0.20.jar ), so it would be "javac Document_Creation.java -cp pdfbox-app-2.0.20.jar" – Tilman Hausherr Aug 19 '20 at 17:58
  • I still have problems with this. I can compile and get a class but when I try to run the program with "java -jar pdfbox-app-2.0.20.jar Document_Creation" I get the error "Usage: java -jar pdfbox-app-x.y.z.jar Possible commands are: Decrypt Encrypt ExtractText ExtractImages ..." – Xtreme Aug 20 '20 at 12:25
  • IIRC you need to do "java Document_Creation -cp pdfbox-app-2.0.20.jar" or possibly "java Document_Creation.class -cp pdfbox-app-2.0.20.jar" – Tilman Hausherr Aug 20 '20 at 17:55

1 Answers1

0

As noted here, the solution is in the syntax order, requiring classpath items before the class:

java -cp .;"C:\Program Files\Java\pdfBox\pdfbox-app-2.0.24.jar" Document_Creation

I also put in the full path where I have pdfBox jar located.

---TLDR---

I've used pdfbox years ago as command line tool format with success, but wanted to step up to using it in a java application. After cleaning up stuff (deleting old java installations [to get java and javac on same version], cleaning up paths, figuring out how to create a macro in notepad++ that allowed for compilation and running of the application, and restarting notepad++) to get to the point of this question, I was almost ready to give up until my search result pulled up the solution. Now I feel ready to dive deeper and hopefully get to use pdfBox for my actual application.

gns100
  • 101
  • 1