【问题标题】:Using C# & Powerpoint OpenXML, is it possible to change the font size and color of text使用 C# 和 Powerpoint OpenXML,是否可以更改文本的字体大小和颜色
【发布时间】:2011-03-24 22:34:30
【问题描述】:

我正在使用 openXML 和 C# 生成幻灯片,但我似乎无法弄清楚如何更改/设置文本大小和颜色。这可能吗?有没有什么例子,我似乎无法通过谷歌搜索找到任何例子?

我正在构建一个表格(类似于:http://blogs.msdn.com/b/brian_jones/archive/2009/08/13/adding-repeating-data-to-powerpoint.aspx),我想更改每个单元格中的一些内容(字体大小、字体颜色、单元格的背景颜色)。

【问题讨论】:

  • @DustinDavis:除了无用的噪音,这条评论是怎么回事?这不是 SO 的工作方式,你应该知道得更好;这不是你第一次来这里。
  • 不确定这整个@DustinDavis 是关于什么的,但是@ooo 你愿意看一下VB.NET 代码吗?您在stackoverflow.com/questions/3903142/… 上说没问题,但从未接受那里的答案,所以我不确定在 VB.NET 中回答是否适合您。
  • @Otaku - 如果您有任何示例,我很乐意查看 VB.net 代码
  • 太棒了!请先重新访问另一个线程,至少让我知道代码是否适合您/不适合您,原因,需要更改的内容等。然后我很乐意为您提供帮助。
  • @Otaku - 另一个线程上的链接没有回答这个问题。它只是替换硬编码表中的现有文本。它不会操纵任何文本格式。

标签: c# powerpoint openxml


【解决方案1】:

您的 cmets 声明此格式适用于 PowerPoint 幻灯片中的表格。

假设
我假设您已经创建了表格、表格行、表格单元格和显示文本。
还假设您一切正常,现在您想添加格式。

如果您想格式化文本和单元格,您可以使用以下方法:

//Create the TableCell for the PowerPoint table you are building.
A.TableCell tableCell3 = new A.TableCell();
A.TextBody textBody5 = new A.TextBody();
A.BodyProperties bodyProperties5 = new A.BodyProperties();//Created but not modified.
A.ListStyle listStyle5 = new A.ListStyle();//Created but not modified.
A.Paragraph paragraph5 = new A.Paragraph();

//First Word: "Hello" with Font-Size 60x and Font-Color Green.
A.Run run1 = new A.Run();
A.RunProperties runProperties1 = new A.RunProperties() { Language = "en-US", FontSize = 6000, Dirty = false, SmartTagClean = false };//Set Font-Size to 60px.
A.SolidFill solidFill1 = new A.SolidFill();
A.RgbColorModelHex rgbColorModelHex1 = new A.RgbColorModelHex() { Val = "00B050" };//Set Font-Color to Green (Hex "00B050").
solidFill1.Append(rgbColorModelHex1);
runProperties1.Append(solidFill1);
A.Text text1 = new A.Text();
text1.Text = "Hello";
run1.Append(runProperties1);
run1.Append(text1);

//Second Word: "World" with Font-Size 60x and Font-Color Blue.
A.Run run2 = new A.Run();
A.RunProperties runProperties2 = new A.RunProperties() { Language = "en-US", FontSize = 6000, Dirty = false, SmartTagClean = false };//Set Font-Size to 60px.
A.SolidFill solidFill2 = new A.SolidFill();
A.RgbColorModelHex rgbColorModelHex2 = new A.RgbColorModelHex() { Val = "0070C0" };//Set Font-Color to Blue (Hex "0070C0").
solidFill2.Append(rgbColorModelHex2);
runProperties2.Append(solidFill2);
A.Text text2 = new A.Text();
text2.Text = " World";
run2.Append(runProperties2);
run2.Append(text2);

//This element specifies the text run properties that are to be used if another run is inserted after the last run specified.
//This effectively saves the run property state so that it can be applied when the user enters additional text.
//If this element is omitted, then the application can determine which default properties to apply.
//It is recommended that this element be specified at the end of the list of text runs within the paragraph so that an orderly list is maintained.
//  Source: http://msdn.microsoft.com/en-us/library/documentformat.openxml.drawing.endparagraphrunproperties.aspx
//Set the default formatting for words entered after "Hello World" with Font-Size 60x and Font-Color Blue.
A.EndParagraphRunProperties endParagraphRunProperties5 = new A.EndParagraphRunProperties() { Language = "en-US", FontSize = 6000, Dirty = false };//Set Font-Size to 60px.
A.SolidFill solidFill3 = new A.SolidFill();
A.RgbColorModelHex rgbColorModelHex3 = new A.RgbColorModelHex() { Val = "0070C0" };//Set Font-Color to Blue (Hex "0070C0").
solidFill3.Append(rgbColorModelHex3);
endParagraphRunProperties5.Append(solidFill3);

paragraph5.Append(run1);//Append Run: "Hello".
paragraph5.Append(run2);//Append Run: " World".
paragraph5.Append(endParagraphRunProperties5);//Append formmatting for any text the user may enter after the words "Hello World".
textBody5.Append(bodyProperties5);//Created but not modified.
textBody5.Append(listStyle5);//Created but not modified.
textBody5.Append(paragraph5);//Append Paragraph: "Hello World"

//TableCell Properties.  Set Background-Color to Red (Hex "FF0000").
A.TableCellProperties tableCellProperties3 = new A.TableCellProperties();
A.SolidFill solidFill4 = new A.SolidFill();
A.RgbColorModelHex rgbColorModelHex4 = new A.RgbColorModelHex() { Val = "FF0000" };//Red Background for Single TableCell.
solidFill4.Append(rgbColorModelHex4);
tableCellProperties3.Append(solidFill4);//Append Red Background.

tableCell3.Append(textBody5);
tableCell3.Append(tableCellProperties3);

我作弊并使用了“Open XML SDK 2.0 Productivity Tool for Microsoft Office”。
我只是创建了一个新的 PowerPoint 文件,添加了一个表格,然后编辑了第 3 个单元格。
然后我运行 SDK 工具并将代码反映在“[]/ppt/presentation.xml”上。
我在反射代码中添加了 cmets,以便您更好地理解它。

【讨论】:

    【解决方案2】:

    正如另一位用户指出的那样,这在 ML 中是可能的。这是我用来解决此问题的解决方案:

    // Assume we are adding a A.TableCell to A.TableRow...
    A.TableCell tc = new A.TableCell(
    new A.TextBody(
    new A.BodyProperties(),
    new A.Paragraph(new A.Run( 
    // -> Add the RunProperties as additional Element to A.Run constructor:
    new A.RunProperties() { FontSize = 600 }, new A.Text("some text") ) ) ),
    new A.TableCellProperties() );
    
    // Now add the cell to a A.TableRow instance...
    

    在创建 A.TableCell 单元格以附加到 A.Table 的行时,我向 A.Run 添加了一个 RunProperty 元素,该元素嵌套了单元格的 A.Text,并使用相应设置的 FontSize 属性对其进行了实例化:{ FontSize = 600 }.

    希望对某人有所帮助。

    【讨论】:

      【解决方案3】:

      一旦您有了想要操作的运行或段落的对象,您就可以将任何您想要的样式添加到运行或段落属性。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-01-23
        • 2014-07-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多