问题
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));
}
}
回答1:
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)
回答2:
omg! Your code so big for that task. I can recommend much easy solution. Please look at following
//Open PDF document
using (var doc = PdfDocument.Load(@"d:\0\test_big.pdf"))
{
//Enumerate pages
foreach(var page in doc.Pages)
{
var found = page.Text.Find("text for search", FindFlags.MatchWholeWord, 0);
if (found == null)
return; //nothing found
do
{
var textInfo = found.FindedText;
foreach(var rect in textInfo.Rects)
{
float x = rect.left;
float y = rect.top;
//...
}
} while (found.FindNext());
page.Dispose();
}
}
来源:https://stackoverflow.com/questions/41574267/search-for-a-text-in-a-pdf-file-and-return-the-coordinates-if-the-text-exist