簡體   English   中英

使用itextsharp檢索頁面上所有單詞的相應坐標

[英]Retrieve the respective coordinates of all words on the page with itextsharp

我的目的是檢索頁面上所有單詞的相應坐標,我所做的是

PdfReader reader = new PdfReader("cde.pdf");
TextWithPositionExtractionStategy S = new TextWithPositionExtractionStategy();
PdfTextExtractor.GetTextFromPage(reader,1,S);

Vector curBaseline = renderInfo.GetDescentLine().GetStartPoint();
Vector topRight = renderInfo.GetAscentLine().GetEndPoint();

iTextSharp.text.Rectangle rect = new iTextSharp.text.Rectangle(curBaseline[Vector.I1], curBaseline[Vector.I2], topRight[Vector.I1], topRight[Vector.I2]);
string x1 = curBaseline[Vector.I1].ToString();
string x2 = curBaseline[Vector.I2].ToString();
string x3 = topRight[Vector.I1].ToString();
string x4 = topRight[Vector.I2].ToString();

但是,我得到的是一個字符串的坐標,它包含一行的所有單詞,而不是一個單詞。例如,pdf的內容是“我是一個女孩”,我得到的是“我是一個”的坐標女孩“,但不是”我“”是“”“”女孩“的坐標。如何修改代碼,以便我可以得到單詞坐標。 謝謝。

(我主要使用Java庫iText,而不是使用.Net庫iTextSharp;因此,請忽略一些Java-isms,一切都應該很容易翻譯。)

要使用iText(Sharp)提取頁面內容,您可以使用解析器包中的類,在對您選擇的RenderListener進行一些預處理后將其提供給它。

在您只對文本感興趣的上下文中,最常使用從RenderListener派生的TextExtractionStrategy ,並添加單個方法getResultantText以從頁面中檢索聚合文本。

由於iText中文本解析的初始目的是實現此用例,因此大多數現有的RenderListener示例都是TextExtractionStrategy實現,並且只使文本可用。

因此,你必須實現自己的RenderListener您已經似乎已經christianed TextWithPositionExtractionStategy

就像有一個SimpleTextExtractionStrategy (通過對頁面內容運算符結構的一些假設實現)和一個LocationTextExtractionStrategy (它沒有相同的假設,但有點復雜),你可能想要從一個實現開始做出一些假設。

因此,就像SimpleTextExtractionStrategy的情況一樣,在第一個簡單的實現中,您希望轉發給偵聽器的文本呈現事件逐行到達,並從左到右在同一行上。 這樣,只要找到水平間隙或標點符號,就會知道當前的單詞已完成,您可以對其進行處理。

與文本提取策略相比,您不需要StringBuffer成員來收集結果,而是需要一些“帶位置的單詞”結構的列表。 此外,您需要一些成員變量來保存您已為此頁面收集的TextRenderInfo事件,但無法最終處理(您可以在幾個單獨的事件中檢索一個單詞)。

一旦為新的TextRenderInfo對象調用(即你的renderText方法),你應該像這樣操作(偽代碼):

if (unprocessedTextRenderInfos not empty)
{
    if (isNewLine // Check this like the simple text extraction strategy checks for hardReturn
     || isGapFromPrevious) // Check this like the simple text extraction strategy checks whether to insert a space
    {
        process(unprocessedTextRenderInfos);
        unprocessedTextRenderInfos.clear();
    }
}

split new TextRenderInfo using its getCharacterRenderInfos() method;
while (characterRenderInfos contain word end)
{
    add characterRenderInfos up to excluding the white space/punctuation to unprocessedTextRenderInfos;
    process(unprocessedTextRenderInfos);
    unprocessedTextRenderInfos.clear();
    remove used render infos from characterRenderInfos;
}
add remaining characterRenderInfos to unprocessedTextRenderInfos;

process(unprocessedTextRenderInfos)您從unprocessedTextRenderInfos中提取所需的信息; 將單個文本內容連接到一個單詞並獲取所需的坐標; 如果你只想要開始坐標,你可以從第一個未經處理的TextRenderInfos中獲取。 如果您需要更多數據,還可以使用其他TextRenderInfos中的數據。 使用這些數據,您可以填寫“帶位置的單詞”結構並將其添加到結果列表中。

頁面處理完成后,您必須再次調用進程(unprocessedTextRenderInfos)和unprocessedTextRenderInfos.clear(); 或者你可以在endTextBlock方法中做到這endTextBlock

完成此操作后,您可能已准備好實現稍微復雜的變體,該變體對頁面內容結構沒有相同的假設。 ;)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM