0

Can I use PDFBox within Eclipse?

I am using Eclipse Luna Service Release 2 (4.4.2) and Java on a MacBook Pro running OS X Yosemite 10.10. My Java program reads .txt files and searches via regular expressions for certain strings, outputs them in various formats to a database and/or a file. This works well with original Word files (converted to .txt), but not PDF files. The input files have many Unicode characters (e.g., a̐ ā̆ I ī u ū ṛ ṙ ṝ r̄̆ ḷ e o b y v r l m ṁ ṃ m̐ ñ ṅ n ṇ n j h g ḍ d k p c ṭ t ś ṣ ḥ ).

I have tried using some simple routines within the Eclipse framework, but the reading of an input file constantly fails every time that I try. I suspect that PDFBox and Eclipse have different I/O conventions.

The following simple model program does not work within Eclipse. I have tried various ways of getting it to load the file.

    import java.io.File;
    import java.io.IOException;


import org.apache.pdfbox.pdmodel.PDDocument; 
import org.apache.pdfbox.pdmodel.PDPage;
public class LoadingExistingDocument {

   public static void main(String args[]) throws IOException {

      //Loading an existing document 
      File file = new File("C:/PdfBox_Examples/sample.pdf"); 
      PDDocument document = PDDocument.load(file); 

      System.out.println("PDF loaded"); 

      //Adding a blank page to the document 
      document.addPage(new PDPage());  

      //Saving the document 
      document.save("C:/PdfBox_Examples/sample.pdf");

      //Closing the document  
      document.close(); 

   }  
}

Do I need to put PDFBox inside Eclipse as a plugin?

Some advice on using PDFBox within Eclipse would be greatly appreciated.

  • 1
    `C:/PdfBox_Examples/sample.pdf` is not a valid file path on macOS – greg-449 Apr 05 '17 at 12:31
  • 1
    "the reading of an input file constantly fails every time that I try" is not a good description of an error. What happens? What PDFBox version are you using? Why are you saving the file at the same location as you're loading it? Have you verified that your PDF file is valid? – Tilman Hausherr Apr 05 '17 at 12:45
  • Please ignore the code in my original message; I should never have included it. I deleted the little project I started because it was not working at all. Basic question: How do I get PDFBox in my Java/Eclipse project? via a plugin? – user2884703 Apr 05 '17 at 20:54
  • You include the jar files that you need into the project. There must be something like "add libraries" in eclipse, just google for it and you'll get results like https://stackoverflow.com/questions/2824515/how-to-add-external-library-properly-in-eclipse . The easiest is to use the pdfbox-app (it has most) from the download page. – Tilman Hausherr Apr 06 '17 at 07:58
  • If you're a newbie, then I suggest you watch a few videos about eclipse. There's more in youtube than cat videos. Or if allowed, try netbeans, this is better for newbies. – Tilman Hausherr Apr 06 '17 at 08:07

0 Answers0