Java 使用PDFBox 2.0从PDF中提取文本

Java 使用PDFBox 2.0从PDF中提取文本,java,pdfbox,Java,Pdfbox,我正在尝试使用PDFBOX2.0进行文本提取。我想获得有关特定字符的字体大小以及该字符在页面上的位置矩形的信息。 我已经在PDFBox 1.6中使用PDFTextStripper实现了这一点: PDFParser解析器=新的PDFParser(is); 试一试{ parser.parse(); }捕获(IOE异常){ } COSDocument cosDoc=parser.getDocument(); PDDocument pdd=新的PDDocument(cosDoc); final Stri

我正在尝试使用PDFBOX2.0进行文本提取。我想获得有关特定字符的字体大小以及该字符在页面上的位置矩形的信息。 我已经在PDFBox 1.6中使用PDFTextStripper实现了这一点:

PDFParser解析器=新的PDFParser(is);
试一试{
parser.parse();
}捕获(IOE异常){
}
COSDocument cosDoc=parser.getDocument();
PDDocument pdd=新的PDDocument(cosDoc);
final StringBuffer extractedText=新StringBuffer();
PDFTextStripper textStripper=新的PDFTextStripper(){
@凌驾
受保护的无效processTextPosition(TextPosition文本){
extractedText.append(text.getCharacter());
logger.debug(“文本位置:+text.toString());
}
};
textStripper.setSuppressDuplicateOverlappingText(false);
对于(int pageNum=0;pageNum,由@tilmanhausherr建议的方法为我的问题提供了解决方案

使用以下代码启动解析器(inputStream
是来自PDF文件URL的输入流):


下面是一个使用@tilmanhausherr建议的实现:

import java.io.ByteArrayOutputStream;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.OutputStreamWriter;
导入java.io.Writer;
导入java.util.List;
导入org.apache.pdfbox.pdmodel.PDDocument;
导入org.apache.pdfbox.text.PDFTextStripper;
导入org.apache.pdfbox.text.TextPosition;
类PDFParserTextStripper扩展了PDFTextStripper
{
公共PDFParserTextStripper(PDDocument pdd)引发IOException
{  
超级();
文件=pdd;
}
public void stripPage(int pageNr)引发IOException
{
本.设置起始页(第n+1页);
此.setEndPage(第1页+1页);
Writer dummy=newoutputstreamwriter(newbytearrayoutputstream());
writeText(document,dummy);//此调用启动解析过程并重复调用writeString。
}
@凌驾
受保护的void writeString(字符串、列表textPositions)引发IOException
{
用于(文本位置文本:文本位置){
System.out.println(“String[”+text.getXDirAdj()+”,“+text.getYDirAdj()+”fs=“+text.getFontSizeInPt()+”xscale=“+text.getXScale()+”height=“+text.getHeightDir()+”space=“+text.getWidthOfSpace()+”width=“+text.getWidthDirAdj()+”)”+text.getUnicode());
}
}
公共静态void提取器文本(InputStream InputStream)
{
pdd文件pdd=null;
尝试
{
pdd=PDDocument.load(inputStream);
PDFParserTextStripper剥离器=新PDFParserTextStripper(pdd);
脱扣器。设置端口BYPOSITION(真);

对于(int i=0;i请查看源代码中的DrawPrintTextLocations示例,它完成了您显然想要做的事情call.谢谢,这个例子正是我想要的。这很好,谢谢。为什么PDFRenderer和PDPage会反对?@Darajan你是对的。它们可能是早期尝试的残余。我将从答案中删除。@Dieudonné你能指导我吗?“PDFdocument”在哪里类?@HamzaHafeez这只是一个自定义类,它不是PDFBox的一部分。好的,你能看到我的查询吗?我正面临这个问题。如果你知道如何解决这个问题,请指导我。
    PDDocument pdd = null;
    try {
        pdd = PDDocument.load(inputStream);
        PDFParserTextStripper stripper = new PDFParserTextStripper(PDFdocument,pdd);
        stripper.setSortByPosition(true);
        for (int i=0;i<pdd.getNumberOfPages();i++){
            stripper.stripPage(i);
        }
    } catch (IOException e) {
        // throw error
    } finally {
        if (pdd!=null) {
            try {
                pdd.close();
            } catch (IOException e) {

            }
        }
    }
class PDFParserTextStripper extends PDFTextStripper {

    public PDFParserTextStripper() throws IOException {
        super();
    }


    public void stripPage(int pageNr) throws IOException {
        this.setStartPage(pageNr+1);
        this.setEndPage(pageNr+1);
        Writer dummy = new OutputStreamWriter(new ByteArrayOutputStream());
        writeText(document,dummy); // This call starts the parsing process and calls writeString repeatedly.
    }



    @Override
    protected void writeString(String string,List<TextPosition> textPositions) throws IOException {
        for (TextPosition text : textPositions) {
            System.out.println("String[" + text.getXDirAdj()+","+text.getYDirAdj()+" fs="+text.getFontSizeInPt()+" xscale="+text.getXScale()+" height="+text.getHeightDir()+" space="+text.getWidthOfSpace()+" width="+text.getWidthDirAdj()+" ] "+text.getUnicode());
        }
    }

}
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.List;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.pdfbox.text.TextPosition;

class PDFParserTextStripper extends PDFTextStripper 
{
    public PDFParserTextStripper(PDDocument pdd) throws IOException 
    {  
        super();
        document = pdd;
    }

    public void stripPage(int pageNr) throws IOException 
    {
        this.setStartPage(pageNr+1);
        this.setEndPage(pageNr+1);
        Writer dummy = new OutputStreamWriter(new ByteArrayOutputStream());
        writeText(document,dummy); // This call starts the parsing process and calls writeString repeatedly.
    }

    @Override
    protected void writeString(String string,List<TextPosition> textPositions) throws IOException 
    {
        for (TextPosition text : textPositions) {
            System.out.println("String[" + text.getXDirAdj()+","+text.getYDirAdj()+" fs="+text.getFontSizeInPt()+" xscale="+text.getXScale()+" height="+text.getHeightDir()+" space="+text.getWidthOfSpace()+" width="+text.getWidthDirAdj()+" ] "+text.getUnicode());
        }
    }

    public static void extractText(InputStream inputStream)
    {
        PDDocument pdd = null;

        try 
        {
            pdd = PDDocument.load(inputStream);
            PDFParserTextStripper stripper = new PDFParserTextStripper(pdd);
            stripper.setSortByPosition(true);
            for (int i=0; i<pdd.getNumberOfPages(); i++)
            {
                stripper.stripPage(i);
            }
        } 
        catch (IOException e) 
        {
            // throw error
        } 
        finally 
        {
            if (pdd != null) 
            {
                try 
                {
                    pdd.close();
                } 
                catch (IOException e) 
                {

                }
            }
        }
    }

    public static void main(String[] args) throws IOException
    {
        File f = new File("C:\\PathToYourPDF\\pdfFile.pdf");
        FileInputStream fis = null;

        try 
        {
            fis = new FileInputStream(f);
            extractText(fis);
        } 
        catch(IOException e) 
        {
            e.printStackTrace();
        } 
        finally 
        {
            try 
            {
                if(fis != null)
                    fis.close();
            } 
            catch(IOException ex)
            {
                ex.printStackTrace();
            }
        }
    }
}