Skip to content

Set the font for a text run is not working #1039

@Munavvar-Husein

Description

@Munavvar-Husein

Hi,

We want to change font and font size programmatically in a Word document using c#. To do this we have used DocumentFormate.OpenXML and apply the below code.

// Set the font for a text run.
public static void SetRunFont(string fileName)
{
// Open a Wordprocessing document for editing.
using (WordprocessingDocument package = WordprocessingDocument.Open(fileName, true))
{
// Set the font to Arial to the first Run.
// Use an object initializer for RunProperties and rPr.
RunProperties rPr = new RunProperties(
new RunFonts()
{
Ascii = "Arial"
});

Run r = package.MainDocumentPart.Document.Descendants<Run>().First();
r.PrependChild<RunProperties>(rPr);

// Save changes to the MainDocumentPart part.
package.MainDocumentPart.Document.Save();
}
}

We have picked this code from the Microsoft site i.e. https://docs.microsoft.com/en-us/office/open-xml/how-to-set-the-font-for-a-text-run but it is not working.

Any help would be greatly appreciated.

Thank You.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions