从pdf中提取阿拉伯语到字符串c# [英] extract arabic from pdf to string c#
问题描述
好的,我知道这个问题重复了太多次.但直到现在我还没有找到解决方案.我正在使用 iTextSharp 从 pdf 中提取.对于英语它工作得很好,但对于阿拉伯语它总是显示???????"在控制台和 .txt 中的Ó å æ á É"这是我的代码
ok i know this question is repeated too many times. but until now i didn't find a solution. I'm using iTextSharp to extract from a pdf. for english it works good, but for Arabic it always shows "???????" in the console and "Ó å æ á É" in the .txt this is my code
private static string ReadPdfFile(string fileName)
{
StringBuilder text = new StringBuilder();
if (File.Exists(fileName))
{
PdfReader pdfReader = new PdfReader(fileName);
ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy();
string currentText = PdfTextExtractor.GetTextFromPage(pdfReader, 1, strategy);
//currentText = Encoding.UTF8.GetString(UTF8Encoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(currentText)));
text.Append(currentText);
}
return text.ToString();
}
更新:
问题通过使用UTF8作为输出文件解决现在我还有两个问题
the problem is solved by using the UTF8 for the output file now i have 2 more problems
它显示ا ﻟ ﺘ ﻘ ﺪ ﻳ ﻢ ﺗ ﻢ"而不是تم التقديم"所以:
it shows "ا ﻟ ﺘ ﻘ ﺪ ﻳ ﻢ ﺗ ﻢ" instead of "تم التقديم" so:
我需要删除字母之间的空格.
i need to remove the spaces between letters.
单词顺序颠倒了.
推荐答案
尝试以下代码将您的Ó å æ á É"转换为سه و ل ة"
Try the following code to convert your "Ó å æ á É" to "س ه و ل ة"
public static string Arabic1256ToUtf8(string data)
{
var latin = Encoding.GetEncoding("ISO-8859-1");
var bytes = latin.GetBytes(data); // get the bytes for your ANSI string
var arabic = Encoding.GetEncoding("Windows-1256"); // decode it using the correct encoding
return arabic.GetString(bytes);
}
这篇关于从pdf中提取阿拉伯语到字符串c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!