3

getting-coordinates-of-string-using-itextextractionstrategy-and-locationtextextr

i am using this code. as per sample this code work absolutely fine. but when i am using in my pdf it not detect string coordinates.every time var t is empty in my pdf. my pdf contains English and one other language.

var t = new MyLocationTextExtractionStrategy("Address");

i am lookng for address coordinate. but every time i run this code it not detect any of coordinate.

in MyLocationTextExtractionStrategy Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iTextSharp.text.pdf.parser;

namespace adharCardByMYR
{
    #region string location

    /*
     //Our test file
    var testFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test.pdf");

    //Create our test file, nothing special
    using (var fs = new FileStream(testFile, FileMode.Create, FileAccess.Write, FileShare.None)) {
        using (var doc = new Document()) {
            using (var writer = PdfWriter.GetInstance(doc, fs)) {
                doc.Open();
                doc.Add(new Paragraph("This is my sample file"));
                doc.Close();
            }
        }
    }

    //Create an instance of our strategy
    var t = new MyLocationTextExtractionStrategy("sample");

    //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));
    }
     */


    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));
        }

    }

    //Helper class that stores our rectangle and text
    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;
        }
    }
    #endregion
}

in my main form

string address = "Address:";
                //address = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(address)));
                var t = new MyLocationTextExtractionStrategy(address);

                ////Parse page 1 of the document above
                ////using (var r = new PdfReader(testFile))
                ////{
                    var ex = iTextSharp.text.pdf.parser.PdfTextExtractor.GetTextFromPage(reader, 1, t);
                ////}
                string abc = "";
                foreach (var p in t.myPoints)
                {
                    abc += string.Format("Found text {0} at {1}x{2}", p.Text, p.Rect.Left, p.Rect.Bottom);
                }
                label1.Text = abc;
Veverke
  • 9,208
  • 4
  • 51
  • 95
  • Please post more code as well as desired outcome. Other than that. Maybe have a look here https://pastebin.com/LqDRDRd9. It's a program that uses itextsharp. You might be doing something wrong so make reference. – fluffy Jun 07 '17 at 06:44
  • @fluffy i updated my code. i see pastebi.com/LqDRDd9 but can not find way to use that code. – Sagar Rathod Jun 07 '17 at 07:02
  • Your code assumes that the `TextToSearchFor` is completely contained in the `TextRenderInfo` argument of a single `RenderText` call. While that can happen, there is no need for that. You might want to try the `LocationTextExtractionStrategyEx` from [Ivan's answer](https://stackoverflow.com/a/33014401/1729265) to the [question you refer to](https://stackoverflow.com/q/23909893/1729265). – mkl Jun 07 '17 at 09:49
  • i check [Ivan's answer](https://stackoverflow.com/questions/23909893/getting-coordinates-of-string-using-itextextractionstrategy-and-locationtextextr/33014401#33014401) it work and get x y of my given string. thank u so much @mkl – Sagar Rathod Jun 08 '17 at 07:53

0 Answers0