how can I change the font open xml

廉价感情. 提交于 2019-12-02 20:33:10
Hans

In order to style your text with a specific font follow the steps listed below:

  1. Create an instance of the RunProperties class.
  2. Create an instance of the RunFont class. Set the Ascii property to the desired font familiy.
  3. Specify the size of your font (half-point font size) using the FontSize class.
  4. Prepend the RunProperties instance to your run containing the text to style.

Here is a small code example illustrating the steps described above:

private static void BuildDocument(string fileName, List<string> text)
{
    using (var wordDoc = WordprocessingDocument.Create(fileName, WordprocessingDocumentType.Document))
    {
        var mainPart = wordDoc.AddMainDocumentPart();
        mainPart.Document = new Document();

        var run = new Run();

        foreach (string currText in text)
        {
            run.AppendChild(new Text(currText));
            run.AppendChild(new CarriageReturn());
        }

        var paragraph = new Paragraph(run);
        var body = new Body(paragraph);

        mainPart.Document.Append(body);

        var runProp = new RunProperties();

        var runFont = new RunFonts { Ascii = "Arial" };

        // 48 half-point font size
        var size = new FontSize { Val = new StringValue("48") }; 

        runProp.Append(runFont);
        runProp.Append(size);

        run.PrependChild(runProp);

        mainPart.Document.Save();
        wordDoc.Close();
    }
}

Hope, this helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!