Monday, May 16, 2016

Working with pdf in selenium webdriver java project


In our test automation, sometimes we need to work with pdf file, iether verifying the text there or to copy some text and storing it to some other file or places. But in Selenium WebDriver, we don’t have any direct methods to do that. 
Don't worry, we can do this using Apache PDFBox. to use this we have to import pdfbox jar file to our selenium java project. This jar, we can get from apache website. or just click here download pdfbox.jar
 

Simply download the latest .jar file and add to your project build path in eclipse.

we are all set to work with pdf now.
Below lets write a simple program where we will read a pdf and print the number of pages in that pdf file. also we will print the content of pdf using stripper class.


package WedriverPackage;
import java.io.File;import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDDocument;
 

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

    {
      PDDocument A = PDDocument.load(new File("C:/Users/IBM_ADMIN/Desktop/Recognition_vishal.pdf"));
      System.out.println("total number of pages:" + A.getNumberOfPages());
      PDFTextStripper B = new PDFTextStripper();          
      System.out.println(B.getText(A));

    }
}

 

No comments :

Post a Comment