0
私は "example1.pdf"という名前のpdfを持っています。私は行ごとにそれを読んでいます。最初の行は "Hello my name is jhon"です。だから私はラインと呼ばれる文字列でこれをします。 私はpdfTextStripperとpdfBoxを使って試していますが、それを行う方法はありません。 すべてのヘルプはPDFで行を読む方法
私は "example1.pdf"という名前のpdfを持っています。私は行ごとにそれを読んでいます。最初の行は "Hello my name is jhon"です。だから私はラインと呼ばれる文字列でこれをします。 私はpdfTextStripperとpdfBoxを使って試していますが、それを行う方法はありません。 すべてのヘルプはPDFで行を読む方法
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.pdfbox.text.TextPosition;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
/**
* This is an example on how to extract text line by line from pdf document
*/
public class GetLinesFromPDF extends PDFTextStripper {
static List<String> lines = new ArrayList<String>();
public GetLinesFromPDF() throws IOException {
}
/**
* @throws IOException If there is an error parsing the document.
*/
public static void main(String[] args) throws IOException {
PDDocument document = null;
String fileName = "example1.pdf";
try {
document = PDDocument.load(new File(fileName));
PDFTextStripper stripper = new GetLinesFromPDF();
stripper.setSortByPosition(true);
stripper.setStartPage(0);
stripper.setEndPage(document.getNumberOfPages());
Writer dummy = new OutputStreamWriter(new ByteArrayOutputStream());
stripper.writeText(document, dummy);
// print lines
for(String line:lines){
System.out.println(line);
}
}
finally {
if(document != null) {
document.close();
}
}
}
/**
* Override the default functionality of PDFTextStripper.writeString()
*/
@Override
protected void writeString(String str, List<TextPosition> textPositions) throws IOException {
lines.add(str);
// you may process the line here itself, as and when it is obtained
}
}
参考にappriciatedされます - あなたの質問に関連するコードを追加しますextract text line by line from pdf
を。また、PDFにはパターンがないことを知っておく必要があります。そのため、汎用ソリューションを作成するのは簡単ではありません。 –
あなたはpdf文書 – simar
からすべての文字列を抽出する方法を尋ねる必要があるかもしれませんsimar私はpdf doc上のすべての文字列を単一の文字列として抽出できます。 – jagga