Tuesday, 14 July 2015

Verify Content in PDF-Page wise (Core JAVA)


If you are using maven then add dependency for itextpdf from below link or add itextpdf jars to your project:
Maven Dependency itextpdf

Download sample pdf from below link and and save it in your d drive as examplePDF.pdf
Download Sample PDF (examplePDF.pdf)

Output is :
Page Number: 1 Fail
Page Number: 2 Pass
Page Number: 3 Fail
Page Number: 4 Fail



package automation.prac;

import java.io.IOException;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfReaderContentParser;
import com.itextpdf.text.pdf.parser.SimpleTextExtractionStrategy;
import com.itextpdf.text.pdf.parser.TextExtractionStrategy;

public class getContentPdf {
 
 private static String INPUTFILE = "d:\\examplePDF.pdf";
 
 public void parsePdf(String pdf) throws IOException {
  
        PdfReader reader = new PdfReader(pdf);
        PdfReaderContentParser parser = new PdfReaderContentParser(reader);
        TextExtractionStrategy strategy;
        
        for (int i = 1; i <= reader.getNumberOfPages(); i++) {
            strategy = parser.processContent(i, new SimpleTextExtractionStrategy());
            if(strategy.getResultantText().contains("Gurdial Singh")){
             System.out.print("Page Number: "+i);
             System.out.println(" Pass");
            }
            else{
             System.out.print("Page Number: "+i);
             System.out.println(" Fail");
            }
        }
      }

 public static void main(String[] args) throws IOException {
  
  new getContentPdf().parsePdf(INPUTFILE);
 }

}

No comments:

Post a Comment