How can I read text from PDFs files using pentaho?
Is there any solution using only built-in java libraries?
Just add a step JavaScript Modified Values
with the following code:
var reader = new com.lowagie.text.pdf.PdfReader("c:\\temp\\mypdf.pdf") // OR JUST PUT THE COLUMN NAME IN THE FLOW;
var pdfTE = new com.lowagie.text.pdf.parser.PdfTextExtractor(reader);
var noOfPages = reader.getNumberOfPages();
var textPDF = "";
for (var i = 1; i <= noOfPages; i++) {
textPDF += pdfTE.getTextFromPage(i);
}
I followed these steps:
2.a. Class code
import java.io.IOException;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;
//String firstnameField;
//String lastnameField;
String nameField;
//https://www.w3schools.blog/itext-read-pdf-file-in-java
public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException
{
// First, get a row from the default input hop
//
Object[] r = getRow();
// If the row object is null, we are done processing.
//
if (r == null) {
setOutputDone();
return false;
}
if (first) {
//firstnameField = getParameter("FIRSTNAME_FIELD");
//lastnameField = getParameter("LASTNAME_FIELD");
nameField = getParameter("NAME_FIELD");
first=false;
}
// It is always safest to call createOutputRow() to ensure that your output row's Object[] is large
// enough to handle any new fields you are creating in this step.
//
Object[] outputRow = createOutputRow(r, data.outputRowMeta.size());
//String firstname = get(Fields.In, firstnameField).getString(r);
//String lastname = get(Fields.In, lastnameField).getString(r);
String pageContent = "";
try {
//Create PdfReader instance.
String path = "C:\\Users\\myusername\\Downloads\\myPDF.pdf";
path = path.replace("\\", "/");
PdfReader pdfReader = new PdfReader(path);
//Get the number of pages in pdf.
int pages = pdfReader.getNumberOfPages();
//Iterate the pdf through pages.
for(int i=1; i<=pages; i++) {
//Extract the page content using PdfTextExtractor.
pageContent =
PdfTextExtractor.getTextFromPage(pdfReader, i);
//Print the page content on console.
System.out.println("Content on Page "
+ i + ": " + pageContent);
}
//Close the PdfReader.
pdfReader.close();
// OR JUST PUT THE COLUMN NAME IN THE FLOW;
} catch (Exception e) {
e.printStackTrace();
}
// Set the value in the output field
//
get(Fields.Out, nameField).setValue(outputRow, pageContent);
// putRow will send the row on to the default output hop.
//
putRow(data.outputRowMeta, outputRow);
return true;
}
The PDF content will be in the resulting field pageContent
My env:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.