0
我想突出顯示一組PDF文件中的幾個關鍵字。首先,我們必須識別單個單詞並將其與我的關鍵字進行匹配。我找到一個例子:itextsharp:在將文字拼接拆分爲單詞時文字被破壞
class MyLocationTextExtractionStrategy : LocationTextExtractionStrategy
{
//Hold each coordinate
public List<RectAndText> myPoints = new List<RectAndText>();
List<string> topicTerms;
public MyLocationTextExtractionStrategy(List<string> topicTerms)
{
this.topicTerms = topicTerms;
}
//Automatically called for each chunk of text in the PDF
public override void RenderText(TextRenderInfo renderInfo)
{
base.RenderText(renderInfo);
//Get the bounding box for the chunk of text
var bottomLeft = renderInfo.GetDescentLine().GetStartPoint();
var topRight = renderInfo.GetAscentLine().GetEndPoint();
//Create a rectangle from it
var rect = new iTextSharp.text.Rectangle(
bottomLeft[Vector.I1],
bottomLeft[Vector.I2],
topRight[Vector.I1],
topRight[Vector.I2]
);
//Add this to our main collection
//filter the meaingless words
string text = renderInfo.GetText();
this.myPoints.Add(new RectAndText(rect, renderInfo.GetText()));
但是,我發現這麼多的單詞都壞了。例如,「停止」將是「st」和「op」。有沒有其他方法可以識別單個單詞及其位置?
我總是可以發現我的舊[代碼](http://stackoverflow.com/a/23915452/231316)!無論如何,請參閱[mkl的答案](http://stackoverflow.com/a/20049810/231316)關於使用IsChunkAtWordBoundary()來判斷兩個「塊」是否應該是一個「單詞」。 –
感謝您的舊代碼。這確實有很大的幫助。我會在稍後嘗試你的建議。再次感謝。 –
無論如何,我發現收集單個單詞的更好方法是在GetResultantText()中,而不是RenderText()。 –