Text formatieren mit VSTO und Aspose.Slides für Java
Text formatieren
Sowohl die VSTO- als auch die Aspose.Slides-Methoden führen die folgenden Schritte aus:
- Öffnen Sie die Quelldpräsentation.
- Greifen Sie auf die erste Folie zu.
- Greifen Sie auf das dritte Textfeld zu.
- Ändern Sie die Formatierung des Textes im dritten Textfeld.
- Speichern Sie die Präsentation auf der Festplatte.
Die untenstehenden Screenshots zeigen die Beispieldfolie vor und nach der Ausführung des VSTO- und Aspose.Slides für Java-Codes.
Die Eingabpräsentation
VSTO-Codebeispiel
Der folgende Code zeigt, wie Sie den Text auf einer Folie mit VSTO neu formatieren.
Der mit VSTO neu formatierte Text
//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 für Java-Beispiel
Um Text mit Aspose.Slides zu formatieren, fügen Sie die Schriftart hinzu, bevor Sie den Text formatieren.
Die Ausgabpräsentation, erstellt mit Aspose.Slides
//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); |