Com formatar text a .NET
Aspose.Slides FOSS for .NET provides fine-grained text formatting through the PortionFormat classe. A Portion és la unitat independent més petita de text; es correspon a una única execució de format dins d’un paràgraf. Aquesta guia mostra com aplicar format en negreta, cursiva, mida de lletra i color al text d’una presentació.
Guia pas a pas
Pas 1: Instal·la el paquet
dotnet add package Aspose.Slides.FossPas 2: Afegeix una forma amb un marc de text
Abans de formatar el text, una forma ha de contenir una TextFrame. Utilitzeu shape.AddTextFrame() per crear-ne una.
using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Export;
using var prs = new Presentation();
var slide = prs.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 500, 150);
var tf = shape.AddTextFrame("Default text: will be formatted");
prs.Save("output.pptx", SaveFormat.Pptx);Pas 3: Accedeix al TextFrame
shape.AddTextFrame() retorna el TextFrame objecte. També podeu recuperar-lo més tard a través de shape.TextFrame.
var tf = shape.TextFrame; // if the frame already exists
var tf = shape.AddTextFrame(""); // creates a new frameUna TextFrame conté una llista de Paragraph objectes (tf.Paragraphs). Cada Paragraph conté Portion objectes (paragraph.Portions).
Pas 4: Aplica format de negreta i cursiva
Utilitza PortionFormat.FontBold i PortionFormat.FontItalic. Aquestes propietats accepten NullableBool.True, NullableBool.False, o NullableBool.NotDefined (hereta del mestre).
using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Export;
using var prs = new Presentation();
var slide = prs.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 500, 150);
var tf = shape.AddTextFrame("Bold and italic text");
var fmt = tf.Paragraphs[0].Portions[0].PortionFormat;
fmt.FontBold = NullableBool.True;
fmt.FontItalic = NullableBool.True;
prs.Save("bold-italic.pptx", SaveFormat.Pptx);Pas 5: Estableix la mida de la lletra i el color
Estableix PortionFormat.FontHeight per a la mida (en punts) i utilitza FillFormat per al color.
using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Drawing;
using Aspose.Slides.Foss.Export;
using var prs = new Presentation();
var slide = prs.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 500, 150);
var tf = shape.AddTextFrame("Large corporate-blue heading");
var fmt = tf.Paragraphs[0].Portions[0].PortionFormat;
fmt.FontHeight = 32; // 32pt font
fmt.FontBold = NullableBool.True;
fmt.FillFormat.FillType = FillType.Solid;
fmt.FillFormat.SolidFillColor.Color = Color.FromArgb(255, 0, 70, 127);
prs.Save("colored-text.pptx", SaveFormat.Pptx);Color.FromArgb(alpha, red, green, blue) accepta valors 0-255 per a cada canal.
Pas 6: Múltiples porcions en un mateix paràgraf
Un sol paràgraf pot contenir diverses porcions amb formatatge diferent. Afegeix un nou Portion a la col·lecció d’un paràgraf Portions col·lecció:
using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Drawing;
using Aspose.Slides.Foss.Export;
using var prs = new Presentation();
var slide = prs.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 600, 100);
var tf = shape.AddTextFrame(""); // start with empty frame
var paragraph = tf.Paragraphs[0];
// First portion: normal text
var portion1 = paragraph.Portions[0];
portion1.Text = "Normal text followed by ";
portion1.PortionFormat.FontHeight = 20;
// Second portion: bold red text
var portion2 = new Portion();
portion2.Text = "bold red text";
portion2.PortionFormat.FontHeight = 20;
portion2.PortionFormat.FontBold = NullableBool.True;
portion2.PortionFormat.FillFormat.FillType = FillType.Solid;
portion2.PortionFormat.FillFormat.SolidFillColor.Color = Color.FromArgb(255, 200, 0, 0);
paragraph.Portions.Add(portion2);
prs.Save("mixed-format.pptx", SaveFormat.Pptx);Problemes comuns i solucions
El text apareix negre fins i tot després d’establir el color
Assegura’t que FillFormat.FillType = FillType.Solid està establert abans d’assignar el color. Sense establir el tipus de farciment, el canvi de color pot no tenir cap efecte.
NullableBool.True vs true
PortionFormat.FontBold espera NullableBool.True, no el C# true. Assignar C# true no es compilarà perquè els tipus són incompatibles.
La font no apareix al fitxer desat
El LatinFont la propietat estableix la família de fonts llatines. Si no s’estableix, s’utilitza la font del tema de la presentació. Les fonts personalitzades han d’estar incrustades o disponibles a la màquina de visualització.
Preguntes freqüents
Com canvio la família de fonts?
Estableix PortionFormat.LatinFont:
fmt.LatinFont = new FontData("Arial");FontData accepta el nom de la família de fonts com a cadena.
Com estableixo l’alineació del paràgraf?
Utilitza ParagraphFormat.Alignment:
tf.Paragraphs[0].ParagraphFormat.Alignment = TextAlignment.Center;Valors admesos: Left, Center, Right, Justify.
Com estableixo l’interlineat?
Utilitza ParagraphFormat.SpaceBefore (punts abans del paràgraf) o ParagraphFormat.SpaceAfter (punts després del paràgraf):
tf.Paragraphs[0].ParagraphFormat.SpaceBefore = 12; // 12pt before
tf.Paragraphs[0].ParagraphFormat.SpaceAfter = 6; // 6pt after