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) {
//PDFをテキストファイルに書き出す
test1();
//PDFを単語区切りでコンソールに出力する
test2();
}
/**
* PDFをテキストファイルに出力します
*/
public static void test1(){
try
{
//PDFをテキストファイルに出力
ExtractText.main(new String[]{pdfFile, textFile});
}
catch( Exception e )
{
e.printStackTrace();
}
}
/**
* PDFを単語区切り(タブ)でコンソールに出力する
*/
public static void test2(){
try{
PDDocument document = PDDocument.load(new FileInputStream(pdfFile) );
PDFTextStripper s = new PDFTextStripper();
//単語区切り文字をタブにセットする
s.setWordSeparator("\t");
//PDFテキストを読み取る
String content = s.getText(document);
//読み取り結果をコンソール出力する
System.out.println( content );
} catch(Exception e){
e.printStackTrace();
}
}
}
More than 5 years have passed since last update.
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme