2016-11-14 74 views
2

enter image description here我有这段代码,我正在使用它来获取PDF文本。这对使用英文的PDF格式非常有用,但是当我尝试用阿拉伯语提取文本时,它会显示出类似这样的内容。使用itextsharp在c#中提取阿拉伯语文本

“)+ 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); 
    } 
+0

这看起来像PDF不包含根据pdf规范提取文本所需的信息。 – mkl

+0

你试过这个http://stackoverflow.com/questions/35436158/itextsharp-cant-extract-pdf-unicode-content-in-c-sharp? – KMoussa

+0

没有有很多的话,但iTextSharp的代码用阿拉伯文写着 –

回答

2

我不得不改变这样

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

战略,此功能可扭转阿拉伯语单词和保持英语

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()); 
     } 
+0

您的问题中输出的任何字符都不显示在“IsArabic”测试的范围内。因此,如果你的答案中的代码确实有帮助,那么你没有提供你真正提取的数据在你的问题中...... – mkl

+0

实际上它发生的一些特别是当它是旧版本:) –

+0

好的,谢谢你分享这个码。我相信这对其他人也有帮助。 –