openxml sdk 2.0 - 未在元音突变上设置字体

时间:2014-01-28 10:23:22

标签: c# ms-word ms-office openxml openxml-sdk

我正在使用openxml sdk 2.0生成一些word文件。我现在的问题是,对于德语元音突变(äöÄÖÜ),字体不适用。

这是一个例子: enter image description here

尝试使用其他一些字体,但即使使用它们也无效,字体总是设置为“Calibri”。

任何人都知道一些提示可以将这些特殊的德语字符附加到这个字体上吗?

感谢您的建议。

这是我创建角色样式的方法:

public static void CreateAndAddCharacterStyle(StyleDefinitionsPart styleDefinitionsPart,
    string styleid, string stylename, string aliases = "")
{
    Styles styles = styleDefinitionsPart.Styles;

    DocumentFormat.OpenXml.Wordprocessing.Style style = new DocumentFormat.OpenXml.Wordprocessing.Style()
    {
        Type = StyleValues.Character,
        StyleId = styleid,
        CustomStyle = true
    };

    Aliases aliases1 = new Aliases() { Val = aliases };
    StyleName styleName1 = new StyleName() { Val = stylename };
    LinkedStyle linkedStyle1 = new LinkedStyle() { Val = styleid + "Para" };
    if (aliases != "")
        style.Append(aliases1);
    style.Append(styleName1);
    style.Append(linkedStyle1);

    StyleRunProperties styleRunProperties1 = new StyleRunProperties();

    if (styleid == "textfett")
    {
        RunFonts font1 = new RunFonts() { Ascii = "Gotham Narrow Medium" };
        styleRunProperties1.Append(font1);
    }
    else
    {
        RunFonts font1 = new RunFonts() { Ascii = "Gotham Narrow Light" };
        styleRunProperties1.Append(font1);
    }

    style.Append(styleRunProperties1);

    styles.Append(style);
}

和我写文字的代码:

List<Run> runs = new List<Run>();

Run r = new Run(new Text(node.Attributes["titel"].InnerText) { Space = SpaceProcessingModeValues.Preserve });
r.RunProperties = new RunProperties();
r.RunProperties.RunStyle = new RunStyle();
r.RunProperties.RunStyle.Val = "textfett";
runs.Add(r);

r = new Run(new Break());
runs.Add(r);

foreach (System.Xml.XmlNode node2 in node.ChildNodes)
{
    if (node2.Name == "info")
    {
        r = new Run(new Text(node2.Attributes["name"].InnerText + ":") { Space = SpaceProcessingModeValues.Preserve });
        r.RunProperties = new RunProperties();
        r.RunProperties.RunStyle = new RunStyle();
        r.RunProperties.RunStyle.Val = "textfett";
        runs.Add(r);

        r = new Run(new Text(" " + node2.InnerText + " ") { Space = SpaceProcessingModeValues.Preserve });
        r.RunProperties = new RunProperties();
        r.RunProperties.RunStyle = new RunStyle();
        r.RunProperties.RunStyle.Val = "textnormal";
        runs.Add(r);
    }
    if (node2.Name == "ende")
    {
        //r = new Run(new Break());
        //runs.Add(r);

        r = new Run(new Text(node2.InnerText) { Space = SpaceProcessingModeValues.Preserve });
        r.RunProperties = new RunProperties();
        r.RunProperties.RunStyle = new RunStyle();
        r.RunProperties.RunStyle.Val = "textnormal";
        runs.Add(r);
    }
}

Paragraph p = new Paragraph();
foreach (Run run in runs)
{
    p.AppendChild<Run>(run);
}
doc.MainDocumentPart.Document.Body.AppendChild(p);

1 个答案:

答案 0 :(得分:1)

我对法语重音字符也有同样的问题。

您需要为要应用于重音字符的字体设置这些属性。例如:

RunFonts font = new RunFonts();
font.Ascii = font.HighAnsi = font.ComplexScript = @"Calibri";

所以要修改你的代码如下:

    RunFonts font1 = new RunFonts() { 
        Ascii = "Gotham Narrow Medium", 
        HighAnsi = "Gotham Narrow Medium", 
        ComplexScript = "Gotham Narrow Medium" };