Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
732 views
in Technique[技术] by (71.8m points)

itext - extracting Arabic text in c# by using itextsharp

enter image description hereI have this code and I'm using it to take the text of a PDF. It's great for a PDF in English but when I'm trying to extract the text in Arabic it shows me something like this.

") + n 9 n <+, + )+ $ # $ +$ F% 9& .< $ : ;"

using (PdfReader reader = new PdfReader(path))
{
     ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy();
     String text = "";
     for (int i = 1; i <= reader.NumberOfPages; i++)
     {
          text = PdfTextExtractor.GetTextFromPage(reader, i,strategy);
     }

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I had to change the strategy like this

var t = PdfTextExtractor.GetTextFromPage(reader, i, new LocationTextExtractionStrategy());
var te = Convert(t);

and this function to reverse the Arabic words and keep the English

  private string Convert(string source)
  {
       string arabicWord = string.Empty;
       StringBuilder sbDestination = new StringBuilder();

       foreach (var ch in source)
       {
           if (IsArabic(ch))
               arabicWord += ch;
           else
           {
               if (arabicWord != string.Empty)
                    sbDestination.Append(Reverse(arabicWord));

               sbDestination.Append(ch);
               arabicWord = string.Empty;
            }
        }

        // if the last word was arabic    
        if (arabicWord != string.Empty)
            sbDestination.Append(Reverse(arabicWord));

        return sbDestination.ToString();
     }


     private bool IsArabic(char character)
     {
         if (character >= 0x600 && character <= 0x6ff)
             return true;

         if (character >= 0x750 && character <= 0x77f)
             return true;

         if (character >= 0xfb50 && character <= 0xfc3f)
             return true;

         if (character >= 0xfe70 && character <= 0xfefc)
             return true;

         return false;
     }

     // Reverse the characters of string
     string Reverse(string source)
     {
          return new string(source.ToCharArray().Reverse().ToArray());
     }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...