how i find position of specific word in pdf file using itext7 ?
Itext7 find position on word
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.
how i find position of specific word in pdf file using itext7 ?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Tahir AnsariRank #366 · 6.8K reputationPosted Oct 9, 2023, 10:57 AM EDT
using IText7.IO.Source; using IText7.Text.Pdf; using IText7.Text.Pdf.TextExtraction; public class FindWordInPdf { public static void Main(string[] args) { // Create a new instance of the PdfDocument class. PdfDocument pdfDocument = new PdfDocument(new PdfReader("my_pdf_file.pdf")); // Create a new instance of the PdfTextExtractor class. PdfTextExtractor pdfTextExtractor = new PdfTextExtractor(pdfDocument); // Create a new instance of the TextExtractionStrategy class. TextExtractionStrategy textExtractionStrategy = new TextExtractionStrategy() { RenderText = (TextRenderInfo textRenderInfo) => { // Get the bounding rectangle of the word you are searching for. Rectangle boundingRectangle = textRenderInfo.GetBaseline().GetBoundingRectange(); // If the bounding rectangle is not empty, print the coordinates of the word. if (!boundingRectangle.IsEmpty) { Console.WriteLine("The word is located at: " + boundingRectangle); } } }; // Set the TextExtractionStrategy object on the PdfTextExtractor object. pdfTextExtractor.SetTextExtractionStrategy(textExtractionStrategy); // Extract the text from the PDF file. pdfTextExtractor.GetTextFromPage(1); // Close the PdfDocument object. pdfDocument.Close(); } }