How can I change the font family of the document via OpenXml ? I tried some ways but, when I open the document, it's always in Calibri
Follow my code, and what I tried.
The Header Builder I think is useless to post
private static void BuildDocument(string fileName, List<string> lista, string tipo) { using (var w = WordprocessingDocument.Create(fileName, WordprocessingDocumentType.Document)) { var mp = w.AddMainDocumentPart(); var d = new DocumentFormat.OpenXml.Wordprocessing.Document(); var b = new Body(); var p = new DocumentFormat.OpenXml.Wordprocessing.Paragraph(); var r = new Run(); // Get and format the text. for (int i = 0; i < lista.Count; i++) { Text t = new Text(); t.Text = lista[i]; if (t.Text == " ") { r.Append(new CarriageReturn()); } else { r.Append(t); r.Append(new CarriageReturn()); } } // What I tried var rPr = new RunProperties(new RunFonts() { Ascii = "Arial" }); lista.Clear(); p.Append(r); b.Append(p); var hp = mp.AddNewPart<HeaderPart>(); string headerRelationshipID = mp.GetIdOfPart(hp); var sectPr = new SectionProperties(); var headerReference = new HeaderReference(); headerReference.Id = headerRelationshipID; headerReference.Type = HeaderFooterValues.Default; sectPr.Append(headerReference); b.Append(sectPr); d.Append(b); // Customize the header. if (tipo == "alugar") { hp.Header = BuildHeader(hp, "Anúncio Aluguel de Imóvel"); } else if (tipo == "vender") { hp.Header = BuildHeader(hp, "Anúncio Venda de Imóvel"); } else { hp.Header = BuildHeader(hp, "Aluguel/Venda de Imóvel"); } hp.Header.Save(); mp.Document = d; mp.Document.Save(); w.Close(); } }
Go to Format > Font > Font. + D to open the Font dialog box. Select the font and size you want to use. Select Default, and then select Yes.
The Open XML SDK provides tools for working with Office Word, Excel, and PowerPoint documents. It supports scenarios such as: - High-performance generation of word-processing documents, spreadsheets, and presentations. - Populating content in Word files from an XML data source.
In order to style your text with a specific font follow the steps listed below:
RunProperties
class.RunFont
class. Set the Ascii
property to the desired font familiy.FontSize
class.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With