Open-XML-SDK icon indicating copy to clipboard operation
Open-XML-SDK copied to clipboard

Set the font for a text run is not working

Open Munavvar-Husein opened this issue 4 years ago • 7 comments
trafficstars

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.

Munavvar-Husein avatar Oct 13 '21 13:10 Munavvar-Husein

Hi @Munavvar-Husein, I just ran the code from that help topic and it works perfectly. You have to have a document with at least one paragraph and run in it for the code to work. Also, when you submit an issue please fill out the template here fill out the template here and add it to your issue. It would help to know what the result of your test was. An exception? An error?

tomjebo avatar Oct 13 '21 17:10 tomjebo

Hi @tomjebo, Please check the error that we are receiving:

image

MicrosoftTeams-image

Please check below code we had used:

static void Main(string[] args) { string fileName = @"E:\Test.docx";

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

}

Please check below attach Test file in that we want to change the font: Test.docx

Munavvar-Husein avatar Oct 14 '21 10:10 Munavvar-Husein

@Munavvar-Husein I just ran the following code from the help article on your Test.docx and it ran perfectly:

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

As you can see, it changed the first run to Arial:

Screenshot 2021-10-14 093109

Not sure what the problem is but perhaps you're not actually opening the same file? Also, make sure you are including information about what version of the SDK, .net target, platform, etc... when you submit these issues.

tomjebo avatar Oct 14 '21 16:10 tomjebo

Hello @tomjebo Thanks for the response.

We are using .Net Framework 4.7.2, Visual Studio 2019, Windows 10 64 bit, and DocumentFormat.OpenXml Nuget package (Both the versions 2.5, 2.13.1).

Munavvar-Husein avatar Oct 15 '21 06:10 Munavvar-Husein

Hello @tomjebo

Did you get a chance to validate the information that we have provided above? Also, Could you please share your framework details with us? So, that we can validate the same from our side as well.

Munavvar-Husein avatar Oct 21 '21 07:10 Munavvar-Husein

Hello @tomjebo,

Thanks for your support. Please note: The error we faced recently has been resolved. There was a minor "using namespaces" issue was there. However, we found that, when the document is opened, at that time it shows changed font "Arial" but when we click on content, at that time it changes the font to "Times New Roman".

Please find attached screenshots for better clarity.

Could you please revert us on this as soon as possible?

Any help would much be appreciated.

Thank You.

Font Problem 1

Font Problem 2

Munavvar-Husein avatar Oct 25 '21 07:10 Munavvar-Husein