Open XML: Set text font

In Open XML you can set the font for a text by using a RunFonts element. You just have to create the element, set the font name and append the created font to the Run element. The following source code shows an accordingly example.

Run run = null;
Paragraph paragraph = null;
Body body = new Body();

//file name            
string folder = @"<my folder>";
string fileName = folder + @"\Test.docx";

//font
var font = new RunFonts();
font.Ascii = "Arial";
            
run = new Run();
run.AppendChild(font);
run.AppendChild(new Text("Hello world"));

paragraph = new Paragraph(run);
body.AppendChild(paragraph);

//create document
var document = new Document(body);

//create file
using (var file = WordprocessingDocument.Create(
    fileName, WordprocessingDocumentType.Document))
{
    file.AddMainDocumentPart();
    file.MainDocumentPart.Document = document;
    file.MainDocumentPart.Document.Save();
}
Werbung
Dieser Beitrag wurde unter .NET, C#, Open XML veröffentlicht. Setze ein Lesezeichen auf den Permalink.

Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit deinem WordPress.com-Konto. Abmelden /  Ändern )

Facebook-Foto

Du kommentierst mit deinem Facebook-Konto. Abmelden /  Ändern )

Verbinde mit %s