如何更改打开 xml 的字体

发布于 2024-12-12 14:11:15 字数 2216 浏览 0 评论 0原文

如何通过 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();
    }             
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

疯狂的代价 2024-12-19 14:11:15

要使用特定字体设置文本样式,请按照下列步骤操作:

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

希望这会有所帮助。

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.

生寂 2024-12-19 14:11:15

如果您使用样式表,只需在字体初始化期间在适当的字体索引处添加 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();

If you are using Stylesheet just add an instance of FontName property at appropriate font index during Fonts initilaization.

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

Then use it in Workbook style part as below.

WorkbookStylesPart stylePart = workbookPart.AddNewPart<WorkbookStylesPart>();
stylePart.Stylesheet = GenerateStylesheet();
stylePart.Stylesheet.Save();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文