我如何改变字体打开xml

本文关键字:字体 xml 改变 何改变 | 更新日期: 2023-09-27 18:12:47

如何通过OpenXml更改文档的字体族?我尝试了一些方法,但是,当我打开文档时,它总是在Calibri

遵循我的代码和我所尝试的。

标题生成器我认为是无用的张贴

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

我如何改变字体打开xml

按照下面列出的步骤为您的文本设置特定的字体:

  1. 创建RunProperties类的实例
  2. 创建RunFont类的实例。设置Ascii属性为所需的字体族。
  3. 使用FontSize类指定你的字体大小(半点字体大小)
  4. 将RunProperties实例添加到包含要样式的文本的运行中。

下面是一个小代码示例,说明上面描述的步骤:

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

希望有帮助。

如果你正在使用样式表,只需在字体初始化过程中在适当的字体索引处添加一个FontName属性的实例。

    private Stylesheet GenerateStylesheet()
    {
        Stylesheet styleSheet = null;
        Fonts fonts = new Fonts(
            new Font( // Index 0 - default
                new FontSize() { Val = 8 },
                new FontName() { Val = "Arial"} //i.e. or any other font name as string
            );
        Fills fills = new Fills( new Fill(new PatternFill() { PatternType = PatternValues.None }));
        Borders borders = new Borders( new Border() );
        CellFormats cellFormats = new CellFormats( new CellFormat () );
        styleSheet = new Stylesheet(fonts, fills, borders, cellFormats);
        return styleSheet;
    }

然后在工作簿样式部分使用,如下所示。

WorkbookStylesPart stylePart = workbookPart.AddNewPart<WorkbookStylesPart>();
stylePart.Stylesheet = GenerateStylesheet();
stylePart.Stylesheet.Save();