0

在我目前的工作中,我需要从.docx文件中读取内容并将其内容写入纯文本文件,该文件将作为其他任务的输入。

下面是我的示例代码,它适用于大多数语言,但对于阿拉伯语我遇到了一个问题,因为阿拉伯语是从右到左的。

StringBuilder docText = new StringBuilder();
Document currentDoc = word.Documents.Open(file.FullName);
foreach (Paragraph p in currentDoc.Paragraphs)
{
    string paraText = p.Range.Text;
    docText.AppendLine(paraText);
}
currentDoc.Close(false);
if (docText.Length > 0)
{
    string outputTxtpath = output + @"\" + Path.GetFileNameWithoutExtension(file.Name) + ".txt";
    File.WriteAllText(outputTxtpath, docText.ToString(), Encoding.UTF8);
}

对于阿拉伯语,我面临两个问题:

  1. txt 文件中的文本在左缩进中。
  2. 如果段落以数字开头(图 1),则该数字位于文本文件的左侧(图 2),而其他阿拉伯字符序列则保持原样,这是我的第二个工具的问题。

样本输入(.docx文件)和输出(.txt文件)上传到以下位置。为这个问题搜索了很多,但我到处都得到了如何阅读阿拉伯文本文件的解决方案。

Docx 文件:https
://1drv.ms/w/s!Ah-Jh2Ok5SuHgQEl90D9NXlM0CJw 文本文件:https ://1drv.ms/t/s!Ah-Jh2Ok5SuHgQAEPZur1A3379mr

DOCX 文件中的原始文本
从 TXT 文件转换的文本

4

1 回答 1

0

我提出了一种稍微不同的方法来保存Word.Document文件,使用内部Document.SaveAs2方法,使用编码文本作为输出格式。

这允许您使用Microsoft Office Unicode 编码,它应该涵盖所有可能的语言编码。

它还允许您指定应如何处理双向标记。如果此选项设置为true,则保留双向原始设置。

Microsoft.Office.Interop.Word.Application WordApplication = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document DefaultDocument;

DefaultDocument = WordApplication.Documents.Open(FilePath);
CultureInfo culture = new CultureInfo((int)DefaultDocument.Content.LanguageID);

string SaveFileName = Path.Combine(Path.GetDirectoryName(FilePath), 
                                   Path.GetFileNameWithoutExtension(FilePath));
DefaultDocument.SaveAs2(SaveFileName, WdSaveFormat.wdFormatEncodedText, Type.Missing, 
                        Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, 
                        Type.Missing, Type.Missing, Type.Missing, 
                        MsoEncoding.msoEncodingUnicodeLittleEndian, Type.Missing, Type.Missing, 
                        Type.Missing, true); //<- BiDi Marks

//Release the Document
foreach (Document WDocument in WordApplication.Documents)
{
    WDocument.Close(WdSaveOptions.wdDoNotSaveChanges);
    Marshal.ReleaseComObject(WDocument);
}

//Release the WINWORD Process
WordApplication.Quit(WdSaveOptions.wdDoNotSaveChanges);
Marshal.ReleaseComObject(WordApplication);
Marshal.CleanupUnusedObjectsInCurrentContext();

//Test the results using a RichTextBox
richTextBox1.RightToLeft = (culture.TextInfo.IsRightToLeft) ? RightToLeft.Yes : RightToLeft.No;
richTextBox1.ImeMode = ImeMode.On;
richTextBox1.Text = File.ReadAllText(SaveFileName + ".txt");

这是 RichTextBox 控件中显示的结果。

在此处输入图像描述

作为 的替代方法,您可以从Document.LanguageID数值Document.SaveAs2派生编码,该数值引用文化标识符 (LCID)。 (在前面的代码中已经定义了)CultureInfo
culture

Encoding encoding = (culture.TextInfo.IsRightToLeft)
                    ? Encoding.GetEncoding(culture.TextInfo.ANSICodePage)
                    : Encoding.Unicode;

using (MemoryStream MemStream = new MemoryStream(encoding.GetBytes(DefaultDocument.Content.Text)))
using (StreamReader reader = new StreamReader(MemStream, encoding))
    File.WriteAllText(SaveFileName + ".txt", reader.ReadToEnd());
于 2018-05-15T16:43:35.310 回答