我正在使用PDFBox从pdf中提取信息,我当前试图找到的信息与该行中第一个字符的x位置有关.我找不到任何与如何获取该信息有关的内容.我知道pdfbox有一个叫做TextPosition的类,但我也找不到如何从PDDocument中获取TextPosition对象.如何从pdf获取一行文本的位置信息?
mkl*_*mkl 14
要使用PDFBox提取文本(使用或不使用位置,颜色等额外信息),您可以实例化一个PDFTextStripper或从中派生的类,并使用它:
PDFTextStripper stripper = new PDFTextStripper();
String text = stripper.getText(document);
Run Code Online (Sandbox Code Playgroud)
(有许多PDFTextStripper属性允许您限制从中提取文本的页面.)
在执行所getText讨论的页面的内容流(以及从那些页面引用的形式的xObject的那些)的过程中,解析并处理文本绘制命令.
如果要更改文本提取行为,则必须通过重写此方法来更改此文本绘制命令处理,这通常应该执行此操作:
/**
* Write a Java string to the output stream. The default implementation will ignore the <code>textPositions</code>
* and just calls {@link #writeString(String)}.
*
* @param text The text to write to the stream.
* @param textPositions The TextPositions belonging to the text.
* @throws IOException If there is an error when writing the text.
*/
protected void writeString(String text, List<TextPosition> textPositions) throws IOException
{
writeString(text);
}
Run Code Online (Sandbox Code Playgroud)
如果您还需要知道新行何时开始,您可能还想覆盖
/**
* Write the line separator value to the output stream.
* @throws IOException
* If there is a problem writing out the lineseparator to the document.
*/
protected void writeLineSeparator( ) throws IOException
{
output.write(getLineSeparator());
}
Run Code Online (Sandbox Code Playgroud)
writeString可以重写以将文本信息引导到单独的成员中(例如,如果您可能希望结果采用比仅仅更简单的结构化格式String),或者可以覆盖它以简单地在结果中添加一些额外信息String.
writeLineSeparator 可以重写以触发行之间的某些特定输出.
有更多的方法可以被覆盖但是你不太可能需要它们.
我正在使用PDFBox从pdf中提取信息,我当前试图找到的信息与该行中第一个字符的x位置有关.
这可以通过以下方式实现(只需在每行的开头添加信息):
PDFTextStripper stripper = new PDFTextStripper()
{
@Override
protected void startPage(PDPage page) throws IOException
{
startOfLine = true;
super.startPage(page);
}
@Override
protected void writeLineSeparator() throws IOException
{
startOfLine = true;
super.writeLineSeparator();
}
@Override
protected void writeString(String text, List<TextPosition> textPositions) throws IOException
{
if (startOfLine)
{
TextPosition firstProsition = textPositions.get(0);
writeString(String.format("[%s]", firstProsition.getXDirAdj()));
startOfLine = false;
}
super.writeString(text, textPositions);
}
boolean startOfLine = true;
};
text = stripper.getText(document);
Run Code Online (Sandbox Code Playgroud)
(经过测试的ExtractText.java方法)extractLineStarttestExtractLineStartFromSampleFile