在pdf文件中搜索文本,如果文本存在则返回坐标 [英] Search for a text in a pdf file and return the coordinates if the text exist
问题描述
我正在尝试在pdf文件中搜索文本,如果文本存在则返回坐标.我正在研究网络,发现可以使用itextsharp库来完成.
I am trying to search for a text in a pdf file and return the coordinates if the text exist. I was researching the net and find out that this can be done with the itextsharp library.
我找到了此代码,并试图对其进行修改以满足我的要求.如何将文件传递给此类.
I found this code and I am trying to modify it to meet my requirements. How can I pass my file to this class.
class Program
{
static void Main(string[] args)
{
var testFile = @"test.pdf";
//Create an instance of our strategy
var t = new MyLocationTextExtractionStrategy("test");
//Parse page 1 of the document above
using (var r = new PdfReader(testFile))
{
var ex = PdfTextExtractor.GetTextFromPage(r, 1, t);
}
//Loop through each chunk found
foreach (var p in t.myPoints)
{
Console.WriteLine(string.Format("Found text {0} at {1}x{2}", p.Text, p.Rect.Left, p.Rect.Bottom));
}
Console.Read();
}
public class RectAndText
{
public iTextSharp.text.Rectangle Rect;
public String Text;
public RectAndText(iTextSharp.text.Rectangle rect, String text)
{
this.Rect = rect;
this.Text = text;
}
}
public class MyLocationTextExtractionStrategy : LocationTextExtractionStrategy
{
//Hold each coordinate
public List<RectAndText> myPoints = new List<RectAndText>();
//The string that we're searching for
public String TextToSearchFor { get; set; }
//How to compare strings
public System.Globalization.CompareOptions CompareOptions { get; set; }
public MyLocationTextExtractionStrategy(String textToSearchFor, System.Globalization.CompareOptions compareOptions = System.Globalization.CompareOptions.None)
{
this.TextToSearchFor = textToSearchFor;
this.CompareOptions = compareOptions;
}
//Automatically called for each chunk of text in the PDF
public override void RenderText(TextRenderInfo renderInfo)
{
base.RenderText(renderInfo);
//See if the current chunk contains the text
var startPosition = System.Globalization.CultureInfo.CurrentCulture.CompareInfo.IndexOf(renderInfo.GetText(), this.TextToSearchFor, this.CompareOptions);
//If not found bail
if (startPosition < 0)
{
return;
}
//Grab the individual characters
var chars = renderInfo.GetCharacterRenderInfos().Skip(startPosition).Take(this.TextToSearchFor.Length).ToList();
//Grab the first and last character
var firstChar = chars.First();
var lastChar = chars.Last();
//Get the bounding box for the chunk of text
var bottomLeft = firstChar.GetDescentLine().GetStartPoint();
var topRight = lastChar.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
this.myPoints.Add(new RectAndText(rect, this.TextToSearchFor));
}
}
推荐答案
在我看来,PdfReader接受一个字符串,该字符串是您要读取的文件的路径.所以只需更改
Looks to me like PdfReader accepts a string which is a path to the file you want to read. So simply change
var testFile = @"test.pdf";
指向要使用的文件. (如果该路径不在应用程序的工作文件夹中,则可能需要添加完整路径)
to point to the file you want to use. (you may have to add a full path if it is not in the application's working folder)
这篇关于在pdf文件中搜索文本,如果文本存在则返回坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!