【问题标题】:My font size set using openXML for a Powerpoint paragraph is not being used我使用 openXML 为 Powerpoint 段落设置的字体大小没有被使用
【发布时间】:2019-12-19 16:43:26
【问题描述】:

我正在尝试为 PowerPoint 幻灯片中的段落设置字体大小。在我的代码中,当我打印出字体大小时,它显示为 9,但是当我打开 PowerPoint 时,它设置为 12。

注意事项:

  • 我在我的电脑上没有管理员权限,因此我无法安装工具来帮助对某些东西进行逆向工程,正如我在几篇文章中所读到的那样。

  • _bulletParagraphOuterXML 是我在寻找如何通过代码设置子弹数小时后从预制段落中抓取的字符串。仍然不知道如何,学习如何是未来的另一篇文章。即使它在此字符串中显示 Arial,项目符号点文本也是 Times New Roman。

  • 在代码中它打印的字体大小为 9,但在 PowerPoint 中显示的字体大小为 12。

下面是我从用于修改和更新我的 PowerPoint 文档的类中提取的代码。

using System;
using System.Collections.Generic;
using System.Linq;
using DocumentFormat.OpenXml.Packaging;
using D = DocumentFormat.OpenXml.Drawing;
using P = DocumentFormat.OpenXml.Presentation;

static string _bulletParagraphOuterXML = "<a:pPr marL=\"285750\" indent=\"-285750\" xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\"><a:buFont typeface=\"Arial\" panose=\"020B0604020202020204\" pitchFamily=\"34\" charset=\"0\" /><a:buChar char=\"•\" /></a:pPr>";


public static void ReplaceShapeBulletsText(this SlidePart slidePart, string ShapeName, List<string> newTexts)
    {
        P.Shape shape = slidePart.GetShapeByName(ShapeName);

        D.Paragraph paragraph = shape.TextBody.Elements<D.Paragraph>().FirstOrDefault();
        shape.TextBody.RemoveAllChildren<D.Paragraph>();

        foreach (string text in newTexts)
        {
            shape.AddBulletParagraph(text);
        }
    }


    public static void AddBulletParagraph(this P.Shape shape, string NewText)
    {

        D.Paragraph p = new D.Paragraph();

        P.TextBody docBody = shape.TextBody;
        p.ParagraphProperties = new D.ParagraphProperties(_bulletParagraphOuterXML);
        D.Run run = new D.Run(new D.Text(NewText));
        D.RunProperties runProp = new D.RunProperties() { Language = "en-US", FontSize = 9, Dirty = false };
        run.AppendChild(runProp);
        Console.WriteLine("--------------------------------------------------------------");
        Console.WriteLine(runProp.FontSize.ToString());
        Console.WriteLine("--------------------------------------------------------------");
        p.Append(run);
        docBody.Append(p);

    }

【问题讨论】:

  • 如果您破解打开生成文件的 XML,您会看到什么?如果运行属性如下所示:,则幻灯片母版和布局正在控制文本大小。如果运行属性如下所示:,您已正确设置大小,但可能需要重置幻灯片(Home>重置)以更新其外观。

标签: c# powerpoint openxml


【解决方案1】:

openXML 中附加事项的顺序。在能够使字体大小不正确的文本和字体大小正确的文本以相同的形状出现后,我注意到只有一个区别。

a:rPra:t 出现的顺序。 a:rPr 需要在a:t 之前

了解这一事实也应该可以帮助我解决我一直遇到的许多其他问题。

正确的字体大小

<a:p>

    <a:pPr marL="171450" indent="-171450">

        <a:buFont typeface="Arial" charset="0" pitchFamily="34" panose="020B0604020202020204"/>    
        <a:buChar char="•"/>

    </a:pPr>
    <a:r>

        <a:rPr lang="en-US" sz="900"/>
        <a:t>Random Text</a:t>

    </a:r>
    <a:endParaRPr lang="en-US" dirty="0" sz="900"/>

</a:p>

字体大小不正确

<a:p>

    <a:pPr xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" marL="285750" indent="-285750">

        <a:buFont typeface="Arial" charset="0" pitchFamily="34" panose="020B0604020202020204"/>
        <a:buChar char="•"/>

    </a:pPr>
    <a:r>

        <a:t>Other Random Text</a:t>
        <a:rPr lang="en-US" dirty="0" sz="900"/>

    </a:r>
    <a:endParaRPr lang="en-US" dirty="0" sz="900"/>

</a:p>

将其与我的代码相关联。

这个代码块:

    p.ParagraphProperties = new D.ParagraphProperties(_bulletParagraphOuterXML);
    D.Run run = new D.Run(new D.Text(NewText));
    D.RunProperties runProp = new D.RunProperties() { Language = "en-US", FontSize = 9, Dirty = false };
    run.AppendChild(runProp);

已更新为

    p.ParagraphProperties = new D.ParagraphProperties(_bulletParagraphOuterXML);
    D.Run run = new D.Run();
    D.RunProperties runProp = new D.RunProperties() { Language = "en-US", FontSize = 900, Dirty = false  };
    run.AppendChild(runProp);
    D.Text newText = new D.Text(NewText);
    run.AppendChild(newText);

【讨论】:

    猜你喜欢
    • 2018-09-24
    • 2011-08-14
    • 2011-03-24
    • 2012-12-21
    • 2015-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多