使用 VSTO 和 Aspose.Slides for Java 格式化文本
Contents
[
Hide
]
有时,您需要以编程方式格式化幻灯片上的文本。本文展示了如何使用 VSTO 和 Aspose.Slides for Java 读取带有一些文本的示例演示文稿的第一页。代码将幻灯片上第三个文本框中的文本格式化为与最后一个文本框中的文本相同的样式。
格式化文本
VSTO 和 Aspose.Slides 方法都执行以下步骤:
- 打开源演示文稿。
- 访问第一页幻灯片。
- 访问第三个文本框。
- 更改第三个文本框中文本的格式。
- 将演示文稿保存到磁盘。
以下截图展示了在执行 VSTO 和 Aspose.Slides for Java 代码之前和之后的示例幻灯片。
输入演示文稿
VSTO 代码示例
以下代码展示了如何使用 VSTO 重新格式化幻灯片上的文本。
使用 VSTO 重新格式化的文本
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Note: PowerPoint is a namespace which has been defined above like this | |
//using PowerPoint = Microsoft.Office.Interop.PowerPoint; | |
PowerPoint.Presentation pres = null; | |
//Open the presentation | |
pres = Globals.ThisAddIn.Application.Presentations.Open("c:\\source.ppt", | |
Microsoft.Office.Core.MsoTriState.msoFalse, | |
Microsoft.Office.Core.MsoTriState.msoFalse, | |
Microsoft.Office.Core.MsoTriState.msoTrue); | |
//Access the first slide | |
PowerPoint.Slide slide = pres.Slides[1]; | |
//Access the third shape | |
PowerPoint.Shape shp = slide.Shapes[3]; | |
//Change its text's font to Verdana and height to 32 | |
PowerPoint.TextRange txtRange = shp.TextFrame.TextRange; | |
txtRange.Font.Name = "Verdana"; | |
txtRange.Font.Size = 32; | |
//Bolden it | |
txtRange.Font.Bold = Microsoft.Office.Core.MsoTriState.msoCTrue; | |
//Italicize it | |
txtRange.Font.Italic = Microsoft.Office.Core.MsoTriState.msoCTrue; | |
//Change text color | |
txtRange.Font.Color.RGB = 0x00CC3333; | |
//Change shape background color | |
shp.Fill.ForeColor.RGB = 0x00FFCCCC; | |
//Reposition it horizontally | |
shp.Left -= 70; | |
//Write the output to disk | |
pres.SaveAs("c:\\outVSTO.ppt", | |
PowerPoint.PpSaveAsFileType.ppSaveAsPresentation, | |
Microsoft.Office.Core.MsoTriState.msoFalse); |
Aspose.Slides for Java 示例
要使用 Aspose.Slides 格式化文本,请在格式化文本之前添加字体。
使用 Aspose.Slides 创建的输出演示文稿
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Open the presentation | |
Presentation pres = new Presentation("c:\\source.ppt"); | |
//Access the first slide | |
ISlide slide = pres.getSlides().get_Item(0); | |
//Access the third shape | |
IShape shp = slide.getShapes().get_Item(2); | |
//Change its text's font to Verdana and height to 32 | |
ITextFrame tf = ((IAutoShape)shp).getTextFrame(); | |
IParagraph para = tf.getParagraphs().get_Item(0); | |
IPortion port = para.getPortions().get_Item(0); | |
port.getPortionFormat().setLatinFont(new FontData("Verdana")); | |
port.getPortionFormat().setFontHeight(32); | |
//Bolden it | |
port.getPortionFormat().setFontBold(NullableBool.True); | |
//Italicize it | |
port.getPortionFormat().setFontItalic(NullableBool.True); | |
//Change text color | |
//Set font color | |
port.getPortionFormat().getFillFormat().setFillType(FillType.Solid); | |
port.getPortionFormat().getFillFormat().getSolidFillColor().setColor(new java.awt.Color(0x33, 0x33, 0xCC)); | |
//Change shape background color | |
shp.getFillFormat().setFillType(FillType.Solid); | |
shp.getFillFormat().getSolidFillColor().setColor( new java.awt.Color(0xCC, 0xCC, 0xFF)); | |
//Write the output to disk | |
pres.save("c:\\data\\outAsposeSlides.pptx",SaveFormat.Pptx); |