【问题标题】:pdfbox getcharacterbyarticle() rendering the vector for last pagepdfbox getcharacterbyarticle() 渲染最后一页的矢量
【发布时间】: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() 中的值来临时修复它。我正在寻找比这更好的解决方案。谢谢。

标签: java pdf pdfbox


【解决方案1】:

临时修复:

更改了main方法,将当前页面设置为结束页面并获取文本信息。不过这不是一个好主意。

 for (int page = 0; page < pageCount; page++)
                    {
        stripper.setStartPage(0);
        stripper.setEndPage(page + 1);
        stripper.writeText(parser.getPDDocument(), outString);
        Vector vectorlistoftps = stripper.getCharactersByArticle();
        PDPage thisPage = stripper.getCurrentPage();
        for (int i = 0; i < vectorlistoftps.size(); i++) {
                List<TextPosition> tplist = vectorlistoftps.get(i);
        }
    }

【讨论】:

    猜你喜欢
    • 2014-04-15
    • 1970-01-01
    • 1970-01-01
    • 2016-01-01
    • 2012-06-28
    • 2012-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多