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

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

最佳答案

您的注释指出此格式适用于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文件,添加了一个表格,然后编辑了第三个单元格。
然后,我运行SDK工具,并将代码反射(reflect)在“[]/ppt/presentation.xml”上。
我在反射的代码中添加了注释,以便您可以更好地理解它。

关于c# - 使用C#和Powerpoint OpenXML,可以更改文本的字体大小和颜色,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5426378/

10-12 22:25