繁体   English   中英

使用pdf-小丑注释或突出显示两列pdf

[英]comment or highlight two-column pdf using pdf-clown

我通过谷歌搜索/ so / forums搜索pdfClown / pdfbox并在SO中发布问题来寻找可能的解决方案。

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

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

请为pdf小丑提出解决方案(如果有)。 (或)任何其他能够突出显示pdf中多行文本且具有JAVA兼容性的工具。

我无法理解类似问题的答案,但是iText有帮助吗?: iText的多行标记注释

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

自动多列布局检测是可能的,但有些棘手 ,因为PDF本质上是(您知道)一种非结构化的图形格式。 尽管如此, 我正在考虑对此进行试验 ,以便至少处理最常见的情况。

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

可以使用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());
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM