使用 C# 和Powerpoint OpenXML,是否可以更改文本的字体大小和颜色

发布于 2024-10-26 19:58:11 字数 364 浏览 4 评论 0原文

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

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

I am using openXML and C# to generate a powerpoint slide but I can't seem to figure out how to change / set the text size and color. Is this possible and are there any example as I can't seem to find any with googling?

I am building out a table (similar to this: http://blogs.msdn.com/b/brian_jones/archive/2009/08/13/adding-repeating-data-to-powerpoint.aspx) and I want to change a number of things in each cell (fontsize, font color, backcolor of cell).

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

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

发布评论

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

评论(3

固执像三岁 2024-11-02 19:58:11

您的评论表明此格式适用于 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);

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

Your comments state this Formatting is for a Table inside a PowerPoint slide.

Assumptions
I am assuming you have already created the table, table rows, table cells, and display text.
Also assuming you have everything working and now you want to add formatting.

If you want to format your Text and Cells, you may do so using the following:

//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);

I cheated and used the "Open XML SDK 2.0 Productivity Tool for Microsoft Office".
I simply created a new PowerPoint file, added a table, and edited the 3rd Cell.
Then I ran the SDK tool and reflected the code on "[]/ppt/presentation.xml".
I added comments to the reflected code so that you may understand it better.

迷路的信 2024-11-02 19:58:11

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

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

希望对某人有帮助。

As another user pointed out this is possible in ML. Here is a solution I used to solve this problem:

// 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...

When creating an A.TableCell cell for appending to the row of an A.Table, I added a RunProperty Element to the A.Run nesting the A.Text for the cell, and I instantiated it with the FontSize attribute set accordingly: { FontSize = 600 }.

Hope that helps someone.

无远思近则忧 2024-11-02 19:58:11

一旦获得了要操作的连续或段落的对象,您就可以将所需的任何样式添加到连续或段落属性中。

Once you have the object for the run or paragraph you wish to manipulate, you can add whatever styling you want to the run or paragraph properties.

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