簡體   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