I would like to extract text from a given PDF file with Apache PDFBox.
I wrote this code:
PDFTextStripper pdfStripper = null;
PDDocument pdDoc = null
This works fine to extract data from a PDF file that has text content using pdfbox 2.0.6
import java.io.File;
import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.pdfbox.text.PDFTextStripperByArea;
public class PDFTextExtractor {
public static void main(String[] args) throws IOException {
System.out.println(readParaFromPDF("C:\\sample1.pdf",3, "Enter Start Text Here", "Enter Ending Text Here"));
//Enter FilePath, Page Number, StartsWith, EndsWith
}
public static String readParaFromPDF(String pdfPath, int pageNo, String strStartIndentifier, String strEndIdentifier) {
String returnString = "";
try {
PDDocument document = PDDocument.load(new File(pdfPath));
document.getClass();
if (!document.isEncrypted()) {
PDFTextStripperByArea stripper = new PDFTextStripperByArea();
stripper.setSortByPosition(true);
PDFTextStripper tStripper = new PDFTextStripper();
tStripper.setStartPage(pageNo);
tStripper.setEndPage(pageNo);
String pdfFileInText = tStripper.getText(document);
String strStart = strStartIndentifier;
String strEnd = strEndIdentifier;
int startInddex = pdfFileInText.indexOf(strStart);
int endInddex = pdfFileInText.indexOf(strEnd);
returnString = pdfFileInText.substring(startInddex, endInddex) + strEnd;
}
} catch (Exception e) {
returnString = "No ParaGraph Found";
}
return returnString;
}
}