[JAVA] PDFBOX reading sample
import java.io.FileInputStream;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.pdfbox.tools.ExtractText;
public class PDFsample {
static String pdfFile = "C:\\Temp\\sample-1.pdf";
static String textFile = "C:\\Temp\\sample.txt";
public static void main(String[] args) {
//Export PDF to text file
test1();
//Output PDF to console separated by words
test2();
}
/**
*Output PDF to text file
*/
public static void test1(){
try
{
//Output PDF to text file
ExtractText.main(new String[]{pdfFile, textFile});
}
catch( Exception e )
{
e.printStackTrace();
}
}
/**
*Output PDF to console with word breaks (tabs)
*/
public static void test2(){
try{
PDDocument document = PDDocument.load(new FileInputStream(pdfFile) );
PDFTextStripper s = new PDFTextStripper();
//Set word delimiter on tab
s.setWordSeparator("\t");
//Read PDF text
String content = s.getText(document);
//Output the read result to the console
System.out.println( content );
} catch(Exception e){
e.printStackTrace();
}
}
}