0

需要获取 PDF 中每一页的偏移量和字节长度。例如,第一页偏移量值为 0,长度为页面的字节长度。

我需要将 PDF 的索引文件传递给 IBM Ondemand 工具,它是一个 PDF 存储库。我需要合并单个 PDF 文件,计算每个 PDF 的偏移量和长度,使用这两个参数创建一个索引文件并将其传递给工具。

该工具将利用索引文件根据作为属性传递的偏移量和长度(索引文件)拆分 PDF(将多个 PDF 合并为单个)。

我使用 itext 通过书签获取页面的开头和结尾。需要计算每页字节的偏移量和长度。

建议是否有任何方法可以以字节为单位获取索引(页面的开头)和页面的结尾。

任何帮助,将不胜感激

4

2 回答 2

0

这个问题需要在 IBM Ondemand 论坛上提问。我想我可以利用 Itext 来破解它。正如 David 所提到的,我们无法使用 Itext 处理这些非结构化 PDF。下面给出了解决问题的代码片段。

两个 PDF 都使用纯 java 合并。合并后的文件将有两个 EOF、header 和 trailing 信息。

当您在 Acrobat 中打开时,它将读取最后的文档信息并显示。当我们将长度和索引传递给按需时,它将拆分 PDF 并按预期显示。

public static void main(String[] args) throws IOException {
    String sourceFile1Path = "C:\\sample1.PDF";
    String sourceFile2Path = "C:\\sample1.PDF";

    String mergedFilePath = "C:\\merged.PDF";

    File[] files = new File[2];
    files[0] = new File(sourceFile1Path);
    files[1] = new File(sourceFile2Path);

    File mergedFile = new File(mergedFilePath);
    for (File file : files) {
        FileWriter fstream = null;
        BufferedWriter out = null;
        fstream = new FileWriter(mergedFile, true);
        out = new BufferedWriter(fstream);

        FileInputStream fis = new FileInputStream(file);
        BufferedReader in = new BufferedReader(new InputStreamReader(fis));

        String aLine;
        while ((aLine = in.readLine()) != null) {
            out.write(aLine);
            out.newLine();
        }
        out.close();
        fstream.close();
        fis.close();
        in.close();

        System.out.println("File Length: " + file.getName() + " : " + new File(mergedFilePath).length());
    }
}
于 2016-03-21T19:38:22.807 回答
0

你不能以任何方式做到这一点。请阅读 PDF 文件格式规范(此处包括http://www.adobe.com/devnet/pdf/pdf_reference.html)。

PDF 文件包含“对象”,并且页面具有记录在流对象中的页面描述,并且可以(并且大部分将)使用很可能分散在文件周围的各种其他对象。

您误解了 PDF 文件是如何构建的,并且在开始尝试实现它之前需要了解,否则您将浪费大量时间。

于 2016-03-21T15:50:53.163 回答