What is the easiest way to extract data from a PDF?

Viewed 21971

I need to extract data from some PDF documents (using Java). I need to know what would be the easiest way to do it.

I tried iText. It's fairly complicated for my needs. Besides I guess it is not available for free for commercial projects. So it is not an option. I also gave a try to PDFBox, and ran into various NoClassDefFoundError errors.

I googled and came across several other options such as PDF Clown, jPod, but I do not have time to experiment with all of these libraries. I am relying on community's experience with PDF reading thru Java.

Note that I do not need to create or manipulate PDF documents. I just need to exrtract textual data from PDF documents with moderate level layout complexity.

Please suggest the quickest and easiest way to extract text from PDF documents. Thanks.

5 Answers

Import this Classes and add Jar Files 1.- pdfbox-app- 2.0.

   import org.openqa.selenium.WebDriver;
   import org.openqa.selenium.WebElement;
   import org.openqa.selenium.support.FindBy;
   import org.testng.Assert;
   import org.testng.annotations.Test;

   import java.io.File;
   import java.io.IOException;
   import java.text.ParseException;
   import java.util.List;

   import org.apache.log4j.Logger;
   import org.apache.log4j.PropertyConfigurator;
   import org.apache.pdfbox.pdmodel.PDDocument;
   import org.apache.pdfbox.text.PDFTextStripper;
   import org.openqa.selenium.By;
   import org.openqa.selenium.chrome.ChromeDriver;


   import com.coencorp.selenium.framework.BasePage;
   import com.coencorp.selenium.framework.ExcelReadWrite;
   import com.relevantcodes.extentreports.LogStatus;

Add this code inside the class.

   public void showList() throws InterruptedException, IOException {

   showInspectionsLink.click();
   waitForElement(hideInspectionsLink);
   printButton.click();
   Thread.sleep(10000);
   String downloadPath = "C:\\Users\\Updoer\\Downloads";
   File getLatestFile = getLatestFilefromDir(downloadPath);
   String fileName = getLatestFile.getName();
   Assert.assertTrue(fileName.equals("Inspections.pdf"), "Downloaded file name is not 
   matching with expected file name");
   Thread.sleep(10000);
   //testVerifyPDFInURL();
   PDDocument pd;
   pd= PDDocument.load(new File("C:\\Users\\Updoer\\Downloads\\Inspections.pdf"));
   System.out.println("Total Pages:"+ pd.getNumberOfPages());
   PDFTextStripper pdf=new PDFTextStripper();
   System.out.println(pdf.getText(pd));

Add this Method in same class.

   public void testVerifyPDFInURL() {
   WebDriver driver = new ChromeDriver();
   driver.get("C:\\Users\\Updoer\\Downloads\\Inspections.pdf");
   driver.findElement(By.linkText("Adeeb Khan")).click();
   String getURL = driver.getCurrentUrl();
   Assert.assertTrue(getURL.contains(".pdf"));
   }

   private File getLatestFilefromDir(String dirPath){
   File dir = new File(dirPath);
   File[] files = dir.listFiles();
   if (files == null || files.length == 0) {
        return null;
   }

   File lastModifiedFile = files[0];
   for (int i = 1; i < files.length; i++) {
   if (lastModifiedFile.lastModified() < files[i].lastModified()) {
   lastModifiedFile = files[i];
   }
   }
   return lastModifiedFile;
   }
Related