【发布时间】:2018-05-21 18:31:21
【问题描述】:
我正在尝试使用以下代码获取文本详细信息,例如坐标、宽度和高度(从here 获取此解决方案),但输出仅是最后一页中的文本。
代码
public static void main( String[] args ) throws IOException {
PDDocument document = null;
String fileName = "apache.pdf"
PDFParser parser = new PDFParser(new FileInputStream(fileName));
parser.parse();
StringWriter outString = new StringWriter();
CustomPDFTextStripper stripper = new CustomPDFTextStripper();
stripper.writeText(parser.getPDDocument(), outString);
Vector<List<TextPosition>> vectorlistoftps = stripper.getCharactersByArticle();
for (int i = 0; i < vectorlistoftps.size(); i++) {
List<TextPosition> tplist = vectorlistoftps.get(i);
for (int j = 0; j < tplist.size(); j++) {
TextPosition text = tplist.get(j);
System.out.println(" String "
+ "[x: " + text.getXDirAdj() + ", y: "
+ text.getY() + ", height:" + text.getHeightDir()
+ ", space: " + text.getWidthOfSpace() + ", width: "
+ text.getWidthDirAdj() + ", yScale: " + text.getYScale() + "]"
+ text.getCharacter() +" Font "+ text.getFont().getBaseFont() + " PageNUm "+ (i+1));
}
}
}
CustomPDFTextStripper 类:
class CustomPDFTextStripper extends PDFTextStripper
{
//Vector<Vector<List<TextPosition>>> data = new Vector<Vector<List<TextPosition>>>();
public CustomPDFTextStripper() throws IOException {
super();
}
public Vector<List<TextPosition>> getCharactersByArticle(){
// data.add(charactersByArticle);
return charactersByArticle;
}
}
我试图将向量添加到列表中,但是当调用 stripper() 时,它会遍历所有页面,并且最后一页的详细信息存储在 charactersByArticle 中向量,因此返回相同。如何获取所有页面的信息??
【问题讨论】:
-
你试过
stripper.setStartPage()和stripper.setEndPage()吗? -
嗨@TilmanHausherr,我试过了,但我只获得了我在stripper.setEndPage()中设置的页面的文本信息。因此,我通过迭代 pdf 中的页数并为每次迭代更改 setEndPage() 中的值来临时修复它。我正在寻找比这更好的解决方案。谢谢。