Wie man Text in .NET formatiert

Wie man Text in .NET formatiert

Aspose.Slides FOSS for .NET provides fine-grained text formatting through the PortionFormat Klasse. A Portion ist die kleinste unabhängige Texteinheit; sie entspricht einem einzelnen Formatierungslauf innerhalb eines Absatzes. Dieser Leitfaden zeigt, wie man Fett, Kursiv, Schriftgröße und Farbformatierung auf Text in einer Präsentation anwendet.

Schritt-für-Schritt-Anleitung

Schritt 1: Paket installieren

dotnet add package Aspose.Slides.Foss

Schritt 2: Ein Shape mit einem Text Frame hinzufügen

Bevor Text formatiert wird, muss eine Form ein TextFrame. Verwenden Sie shape.AddTextFrame() um eines 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: Auf das TextFrame zugreifen

shape.AddTextFrame() gibt das TextFrame Objekt zurück. Sie können es später auch über shape.TextFrame.

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

Ein 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 PortionFormat.FontBold und PortionFormat.FontItalic. Diese Eigenschaften akzeptieren NullableBool.True, NullableBool.False, oder NullableBool.NotDefined (von Master erben).

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 PortionFormat.FontHeight für Größe (in Punkten) und verwenden FillFormat für 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 Portionen in einem Paragraph

Ein einzelner Absatz kann mehrere Abschnitte mit unterschiedlicher Formatierung enthalten. Fügen Sie ein neues Portion zu einem Absatz Portions Sammlung:

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

Der Text erscheint schwarz, selbst nach dem Festlegen der Farbe

Stellen Sie sicher FillFormat.FillType = FillType.Solid ist festgelegt, bevor die Farbe zugewiesen wird. 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. Zuweisen von C# true wird nicht kompiliert, weil die Typen inkompatibel sind.

Schriftart erscheint nicht in der gespeicherten Datei

Der LatinFont Die Eigenschaft legt die lateinische Schriftfamilie fest. Wenn sie nicht festgelegt ist, wird die Schriftart des Präsentationsthemas verwendet. Benutzerdefinierte Schriftarten müssen eingebettet sein oder auf dem Anzeigegerät verfügbar sein.


Häufig gestellte Fragen

Wie ändere ich die Schriftfamilie?

Setzen PortionFormat.LatinFont:

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

FontData akzeptiert den Namen der Schriftfamilie als Zeichenkette.

Wie stelle ich die Absatzausrichtung ein?

Verwenden ParagraphFormat.Alignment:

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

Unterstützte Werte: Left, Center, Right, Justify.

Wie stelle ich den Zeilenabstand ein?

Verwenden ParagraphFormat.SpaceBefore (Punkte vor Absatz) oder ParagraphFormat.SpaceAfter (Punkte nach Absatz):

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

Siehe auch

 Deutsch