Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I change the font open xml

Tags:

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();     }              } 
like image 445
Lucas_Santos Avatar asked Oct 31 '11 16:10

Lucas_Santos


People also ask

How do I change the default font size and style?

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.

What is Documentformat Openxml?

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.


1 Answers

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.

like image 67
Hans Avatar answered Sep 19 '22 15:09

Hans