Come formattare il testo in .NET

Come formattare il testo in .NET

Aspose.Slides FOSS for .NET provides fine-grained text formatting through the PortionFormat classe. A Portion è l’unità indipendente più piccola di testo; corrisponde a un singolo intervallo di formattazione all’interno di un paragrafo. Questa guida mostra come applicare la formattazione grassetto, corsivo, dimensione del carattere e colore al testo in una presentazione.

Guida passo-passo

Passo 1: Installa il pacchetto

dotnet add package Aspose.Slides.Foss

Passo 2: Aggiungi una forma con un riquadro di testo

Prima di formattare il testo, una forma deve contenere un TextFrame. Usa shape.AddTextFrame() per crearne uno.

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

Passo 3: Accedi al TextFrame

shape.AddTextFrame() restituisce il TextFrame oggetto. Puoi anche recuperarlo più tardi tramite shape.TextFrame.

var tf = shape.TextFrame;          // if the frame already exists
var tf = shape.AddTextFrame("");   // creates a new frame

A TextFrame contiene un elenco di Paragraph oggetti (tf.Paragraphs). Ogni Paragraph contiene Portion oggetti (paragraph.Portions).


Passo 4: Applica la formattazione in grassetto e corsivo

Usa PortionFormat.FontBold e PortionFormat.FontItalic. Queste proprietà accettano NullableBool.True, NullableBool.False, o NullableBool.NotDefined (eredita da master).

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

Passo 5: Imposta la dimensione del carattere e il colore

Imposta PortionFormat.FontHeight per dimensione (in punti) e usa FillFormat per il colore.

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) accetta valori da 0 a 255 per ogni canale.


Passo 6: Più porzioni in un singolo paragrafo

Un singolo paragrafo può contenere più parti con formattazioni diverse. Aggiungi un nuovo Portion a un paragrafo Portions collezione:

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

Problemi comuni e soluzioni

Il testo appare nero anche dopo aver impostato il colore

Assicurati FillFormat.FillType = FillType.Solid sia impostato prima di assegnare il colore. Senza impostare il tipo di riempimento, la modifica del colore potrebbe non avere effetto.

NullableBool.True vs true

PortionFormat.FontBold si aspetta NullableBool.True, non il C# true. Assegnare C# true non compilerà perché i tipi sono incompatibili.

Il font non appare nel file salvato

Il LatinFont proprietà imposta la famiglia di caratteri Latin. Se non impostata, viene usato il carattere del tema della presentazione. I caratteri personalizzati devono essere incorporati o disponibili sulla macchina di visualizzazione.


Domande frequenti

Come faccio a cambiare la famiglia di font?

Imposta PortionFormat.LatinFont:

fmt.LatinFont = new FontData("Arial");

FontData accetta il nome della famiglia di caratteri come stringa.

Come impostare l’allineamento del paragrafo?

Usa ParagraphFormat.Alignment:

tf.Paragraphs[0].ParagraphFormat.Alignment = TextAlignment.Center;

Valori supportati: Left, Center, Right, Justify.

Come impostare l’interlinea?

Usa ParagraphFormat.SpaceBefore (punti prima del paragrafo) o ParagraphFormat.SpaceAfter (punti dopo il paragrafo):

tf.Paragraphs[0].ParagraphFormat.SpaceBefore = 12;   // 12pt before
tf.Paragraphs[0].ParagraphFormat.SpaceAfter = 6;     // 6pt after

Vedi anche

 Italiano