首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何逐行读取pdf

如何逐行读取pdf
EN

Stack Overflow用户
提问于 2017-07-18 13:01:28
回答 2查看 11.2K关注 0票数 2

我有一个名为example1.pdf的pdf

我想用line.let来读它,第一行是Hello my name is jhon。所以我想用一个名为line的字符串。我正在使用PDFTextStripper和pdfBox进行尝试,但没有找到任何方法。任何帮助都将不胜感激。

EN

回答 2

Stack Overflow用户

发布于 2017-08-11 04:06:10

代码语言:javascript
运行
复制
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
    }
}

参考- 从pdf中逐行提取文本

票数 4
EN

Stack Overflow用户

发布于 2019-01-22 07:30:34

这种方法容易得多。

代码语言:javascript
运行
复制
public static void main(String[] args) throws Exception, IOException 
{
    File file = new File("File.pdf"); 
    PDDocument document = PDDocument.load(file);
    PDFTextStripper pdfStripper = new PDFTextStripper();
    pdfStripper.setStartPage(1);
    pdfStripper.setEndPage(1);

    //load all lines into a string
    String pages = pdfStripper.getText(document);

    //split by detecting newline
    String[] lines = pages.split("\r\n|\r|\n");

    int count=1;   //Just to indicate line number
    for(String temp:lines)
    {
        System.out.println(count+" "+temp);
        count++;
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45167592

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档