Road to verify PDF file text using java webdriver

In this post I will explain the procedure to verify PDF file content using java webdriver. As  some time we need to verify content of web application pdf file, opened in browser.
Use below code in your test scripts to get pdf file content.

 //get current urlpdf file url
 URL url = new URL(driver.getCurrentUrl());
 //create buffer reader object
 BufferedInputStream fileToParse = new BufferedInputStream(url.openStream());
 PDFParser pdfParser = new PDFParser(fileToParse);

 //save pdf text into strong variable
 String pdftxt = new PDFTextStripper().getText(pdfParser.getPDDocument());
 //close PDFParser object

After applying above code, you can store all pdf file content into “pdftxt” string variable. Now you can verify string by giving  in put.  As if you want to verify “Selenium Or Webdiver” text. Use below code.
Assert.assertTrue(pdftxt.contains(“Selenium Or Webdiver”))

Hope this post will help  to verify web application PDF content.


  1. eclipse is not identifying " PDFParser pdfParser = new PDFParser(fileToParse);"
    Do we need to import any special Jars for this.

    1. yes you can download pdfbox-app-1.7.1.jar file for this

  2. There is a problem in my side can u guess this ? Error: End-of-File, expected line
    at org.apache.pdfbox.pdfparser.BaseParser.readLine(
    at org.apache.pdfbox.pdfparser.PDFParser.parseHeader(
    at org.apache.pdfbox.pdfparser.PDFParser.parse(
    at com.popup.execute.ExecuteTest.testPdfVerification(
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(
    at java.lang.reflect.Method.invoke(

  3. I am also getting Error: End-of-File, expected line. Let me know if you have any solution.

  4. PDFParser pdfParser = new PDFParser(fileToParse) : I am getting compliation error for this line :
    The constructor PDFParser(BufferedInputStream) is undefined
    I have used this pdfbox-app-1.7.1.jar file .

  5. I am also getting 'End-of-File, expected line' error.

  6. I am also getting 'End-of-File, expected line' error.

  7. I am also getting 'End-of-File, expected line' error. Can someone tell me the solutions


Leave your comments, queries, suggestion I will try to provide solution