Wie man Text in .NET formatiert
Aspose.Slides FOSS for .NET bietet eine feinkörnige Textformatierung über die PortionFormat-Klasse. Ein Portion ist die kleinste unabhängige Texteinheit; er entspricht einem einzelnen Formatierungslauf innerhalb eines Absatzes. Dieser Leitfaden zeigt, wie man Fett‑, Kursiv‑, Schriftgrößen‑ und Farbformatierungen auf Text in einer Präsentation anwendet.
Schritt-für-Schritt-Anleitung
Schritt 1: Paket installieren
dotnet add package Aspose.Slides.FossSchritt 2: Eine Form mit einem Textfeld hinzufügen
Bevor Text formatiert wird, muss eine Form ein TextFrame enthalten. Verwenden Sie shape.AddTextFrame(), um eine zu erstellen.
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);Schritt 3: Zugriff auf das TextFrame
shape.AddTextFrame() gibt das TextFrame‑Objekt zurück. Sie können es später auch über shape.TextFrame abrufen.
var tf = shape.TextFrame; // if the frame already exists
var tf = shape.AddTextFrame(""); // creates a new frameEin TextFrame enthält eine Liste von Paragraph‑Objekten (tf.Paragraphs). Jeder Paragraph enthält Portion‑Objekte (paragraph.Portions).
Schritt 4: Fett‑ und Kursivformatierung anwenden
Verwenden Sie PortionFormat.FontBold und PortionFormat.FontItalic. Diese Eigenschaften akzeptieren NullableBool.True, NullableBool.False oder NullableBool.NotDefined (erben vom 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);Schritt 5: Schriftgröße und Farbe festlegen
Setzen Sie PortionFormat.FontHeight für die Größe (in Punkten) und verwenden Sie FillFormat für die Farbe.
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) akzeptiert Werte von 0‑255 für jeden Kanal.
Schritt 6: Mehrere Abschnitte in einem Absatz
Ein einzelner Absatz kann mehrere Abschnitte mit unterschiedlicher Formatierung enthalten. Fügen Sie ein neues Portion zu einer Portions‑Sammlung eines Absatzes hinzu:
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);Häufige Probleme und Lösungen
Text erscheint schwarz, obwohl die Farbe eingestellt wurde
Stellen Sie sicher, dass FillFormat.FillType = FillType.Solid gesetzt ist, bevor Sie die Farbe zuweisen. Ohne das Festlegen des Fülltyps kann die Farbänderung keine Wirkung haben.
NullableBool.True vs true
PortionFormat.FontBold erwartet NullableBool.True, nicht das C# true. Das Zuweisen von C# true wird nicht kompilieren, weil die Typen inkompatibel sind.
Schriftart erscheint nicht in der gespeicherten Datei
Die LatinFont‑Eigenschaft legt die lateinische Schriftfamilie fest. Wenn sie nicht festgelegt ist, wird die Schriftart des Präsentationsthemas verwendet. Benutzerdefinierte Schriftarten müssen eingebettet oder auf dem Anzeigegerät verfügbar sein.
Häufig gestellte Fragen
Wie ändere ich die Schriftfamilie?
Menge PortionFormat.LatinFont:
fmt.LatinFont = new FontData("Arial");FontData akzeptiert den Schriftfamiliennamen als Zeichenkette.
Wie stelle ich die Absatzausrichtung ein?
Verwenden Sie ParagraphFormat.Alignment:
tf.Paragraphs[0].ParagraphFormat.Alignment = TextAlignment.Center;Unterstützte Werte: Left, Center, Right, Justify.
Wie stelle ich den Zeilenabstand ein?
Verwenden Sie ParagraphFormat.SpaceBefore (Punkte vor dem Absatz) oder ParagraphFormat.SpaceAfter (Punkte nach dem Absatz):
tf.Paragraphs[0].ParagraphFormat.SpaceBefore = 12; // 12pt before
tf.Paragraphs[0].ParagraphFormat.SpaceAfter = 6; // 6pt after