无法使用c#从word文件中读取符号

Not able to Read Symbol from word file using c#

我无法在 c# 中使用 Interop 从单词中获取 µ(alt+230, U+00B5) 符号。

当我尝试获取像 document.characters[1].text 这样的文本时,它显示“(”代替 µ 符号。 试图执行此操作:

for (var i = 1; i <= Document.Characters.Count; i++) 
{ 
  var chr = Document.Characters[i]; 
  var ascii = (int) chr.Text[0]; 
  Console.WriteLine(chr.Text);
}

有知道的请帮忙

谢谢

据我所知并能够研究,不可能从通过“插入符号”对话框插入的字符中获取实际值,如所解释的那样here

因为我怀疑互操作路线会给你那个角色我实现了 XML 路线(更好的可能是在这个阶段使用 OpenXML 但我只是使用了我已经拥有的互操作).

此代码示例为您提供了我处理的节点文档中的文本,在本例中为 <w:t>w:sym 节点。

var app = new Microsoft.Office.Interop.Word.Application();

var doc = app.Documents.Open(FileName: @"C:\Users\rschrieken\Downloads\character-safe.docx", Encoding: MsoEncoding.msoEncodingUSASCII);

// forget Interop, hello XML
var cd = XDocument.Parse(doc.WordOpenXML);

var w = (XNamespace)"http://schemas.openxmlformats.org/wordprocessingml/2006/main";

var sb = new StringBuilder();

foreach (var para in cd.Descendants(w + "p"))
{
    foreach (var node in para.Descendants())
    {
        if (node.Name.LocalName == "t")
        {
            Console.Write(node.Value);
            sb.Append(node.Value);
        }
        if (node.Name.LocalName == "sym")
        {
            var sym = node.Attribute(w + "char").Value;
            // this will convert the hex value
            var val = Convert.ToInt32(sym, 16);
            // depending on your requirements, you might have to re-map this
            // but I simply assume here that hex value is an valid Unicode char 
            Console.Write((char)val);
            sb.Append((char) val);
        }
    }
    Console.WriteLine();
    sb.AppendLine();
}
// sb.ToString() gives you the text from the document

您不会在这里看到 mu 字符,因为控制台使用的字体没有为该 char 值定义的字形。