Question

I'm using the openxml sdk 2.0 for generating some word files. My problem now is that for german vowel mutations (äöÄÖÜ) the font isn't applied.

Here's an example: enter image description here

Tried it with some other fonts but even with them it's not working, the font is always set to "Calibri".

Anyone knows some hints to get the style appended to these special german characters?

Thanks in advice.

.

This is my Method to create the character styles:

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);
}

and my code for writing text:

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);
Was it helpful?

Solution

I had the same problem with French accented chars.

You need to set these properties for the font to be applied to accented characters. For instance :

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

So for modify your code as the following :

    RunFonts font1 = new RunFonts() { 
        Ascii = "Gotham Narrow Medium", 
        HighAnsi = "Gotham Narrow Medium", 
        ComplexScript = "Gotham Narrow Medium" };
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top