从PDF解析对象时,出于某些原因忽略具有字节流的对象?

时间:2018-10-09 15:03:21

标签: java parsing pdf pdf-parsing bytestream

我当前的工作包括从pdf文件中取出所有对象,然后使用解析出的对象。但是我注意到一个问题,我的代码跳过了一些流对象。

我完全感到困惑,希望有人可以帮助指出这里出了什么问题。

这是主要的解析代码。

    void parseRawPDFFile() {
        //Transform the bytes obtained from the file into a byte character sequence. This byte character sequence
        //object is what allows us to use it in regex.
        ByteCharSequence byteCharSequence = new ByteCharSequence(bytesFromFile.toByteArray());
        byteCharSequence.getStringFromData();

        Pattern pattern = Pattern.compile(SINGLE_OBJECT_REGEX);
        Matcher matcher = pattern.matcher(byteCharSequence);

        //While we have a match (apparently only one match exists at a time) keep looping over the list.
        //When a match is found, get the starting and ending indices and manually cut these out char by char
        //and assemble them into a new "ByteArrayOutputStream".
        int counterOfDoom = 1;
        while (matcher.find() ) {
            for (int i = 0; i < matcher.groupCount(); i++) {
                ByteArrayOutputStream cutOutArray = cutOutByteArrayOutputStreamFromOriginal(matcher.start(), matcher.end());
                System.out.println("----------------------------------------------------");
                System.out.println(cutOutArray);
                //At this point we have cut out the object and can now send it for processing.
               createPDFObject(cutOutArray);

                System.out.println(counterOfDoom);
                System.out.println("----------------------------------------------------");
                counterOfDoom++;
            }
        }
    }

这是ByteCharSequence的代码 (此处提供此代码的核心代码:http://blog.sarah-happy.ca/2013/01/java-regular-expression-on-byte-array.html

public class ByteCharSequence implements CharSequence {

    private final byte[] data;
    private final int length;
    private final int offset;

    public ByteCharSequence(byte[] data) {
        this(data, 0, data.length);
    }

    public ByteCharSequence(byte[] data, int offset, int length) {
        this.data = data;
        this.offset = offset;
        this.length = length;
    }

    @Override
    public int length() {
        return this.length;
    }

    @Override
    public char charAt(int index) {
        return (char) (data[offset + index] & 0xff);
    }

    @Override
    public CharSequence subSequence(int start, int end) {
        return new ByteCharSequence(data, offset + start, end - start);
    }

    /**
     * Get the string from the ByteCharSequence data.
     * @return
     */
    public String getStringFromData() {
        //Load it into the method I know works to convert it to a string... Optimized? Probably not at all.
        //But it works...
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        for (byte individualByte: data
             ) {
            byteArrayOutputStream.write(individualByte);
        }

        return byteArrayOutputStream.toString();
    }
}

我目前正在处理的pdf数据:

10 0 obj
<</Filter/FlateDecode/Length 1040>>stream
(Bunch of bytes)
endstream
endobj


12 0 obj
<</Filter/FlateDecode/Length 2574/N 3>>stream
(Bunch of bytes)
endstream
endobj

一些我想研究的信息。

1:据我了解,对数据结构可以容纳多少没有限制。所以大小不应该成为问题吗??

1 个答案:

答案 0 :(得分:0)

DOTALL标志添加到模式编译调用中,以便您的模式与换行符=)