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);
 pdfParser.parse();

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

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.

13 comments:

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

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

      Delete
  2. There is a problem in my side can u guess this ?

    java.io.IOException: Error: End-of-File, expected line
    at org.apache.pdfbox.pdfparser.BaseParser.readLine(BaseParser.java:1475)
    at org.apache.pdfbox.pdfparser.PDFParser.parseHeader(PDFParser.java:308)
    at org.apache.pdfbox.pdfparser.PDFParser.parse(PDFParser.java:172)
    at com.popup.execute.ExecuteTest.testPdfVerification(ExecuteTest.java:109)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)

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

    ReplyDelete
  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 .

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

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

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

    ReplyDelete
  8. Any help would be apprecaited

    ReplyDelete

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