使用pdf-clown评论或突出显示两栏pdf

时间:2014-03-27 20:20:31

标签: pdf highlight pdfbox annotate pdfclown

我已经通过googling / so / forums搜索pdfClown / pdfbox的可能解决方案,并在SO上发布问题。

问题:我一直在努力寻找突出显示文字的解决方案,该文字跨越pdf文档中的多行。 pdf可以有一个/两个页面的页面。

通过使用pdf-clown,只有当所有单词出现在同一行时,我才能突出显示短语。 pdfBox为单个单词创建了XML,我找不到短语/行的解决方案。

如果有的话,请建议pdf-clown的解决方案。 (或)能够突出显示pdf中多行文本的任何其他工具,兼容JAVA。

我无法理解答案类似的问题,但iText,任何帮助?: Multiline markup annotations with iText

2 个答案:

答案 0 :(得分:0)

目前多列文本(PDF Clown 0.1.2)不支持提取:当前算法收集放置在同一水平基线上的文本,而不评估列之间可能存在的间隙。

自动多列布局检测可能有些棘手,因为PDF基本上(你知道)是一种非结构化的图形格式。尽管如此,我正在考虑尝试一些关于的内容,以便至少处理最常见的场景。

与此同时,我建议您尝试一种有效的解决方法(这意味着您要处理其列位于可预测区域的文档):每个列的单独执行文本提取,指示TextExtractor查看相应的页面区域,然后将所有这些部分提取结果放在一起并应用您的过滤器。

答案 1 :(得分:-1)

可以使用pdfbox获取pdf文档中每个单词的坐标,以下是代码:

import java.io.*;
import org.apache.pdfbox.exceptions.InvalidPasswordException;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDStream;
import org.apache.pdfbox.util.PDFTextStripper;
import org.apache.pdfbox.util.TextPosition;

import java.io.IOException;
import java.util.List;

public class PrintTextLocations extends PDFTextStripper {

    public PrintTextLocations() throws IOException {
        super.setSortByPosition(true);
    }

    public static void main(String[] args) throws Exception {

        PDDocument document = null;
        try {
            File input = new File("C:\\path\\to\\PDF.pdf");
            document = PDDocument.load(input);
            if (document.isEncrypted()) {
                try {
                    document.decrypt("");
                } catch (InvalidPasswordException e) {
                    System.err.println("Error: Document is encrypted with a password.");
                    System.exit(1);
                }
            }
            PrintTextLocations printer = new PrintTextLocations();
            List allPages = document.getDocumentCatalog().getAllPages();
            for (int i = 0; i < allPages.size(); i++) {
                PDPage page = (PDPage) allPages.get(i);
                System.out.println("Processing page: " + i);
                PDStream contents = page.getContents();
                if (contents != null) {
                    printer.processStream(page, page.findResources(), page.getContents().getStream());
                }
            }
        } finally {
            if (document != null) {
                document.close();
            }
        }
    }

    protected void processTextPosition(TextPosition text) {
        System.out.println("String[" + text.getXDirAdj() + ","
                + text.getYDirAdj() + " fs=" + text.getFontSize() + " xscale="
                + text.getXScale() + " height=" + text.getHeightDir() + " space="
                + text.getWidthOfSpace() + " width="
                + text.getWidthDirAdj() + "]" + text.getCharacter());
    }
}