Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
232 views
in Technique[技术] by (71.8m points)

c# - how can I change the font open xml

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();
    }             
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...