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 Einheit des Textes; es erfasst eine einzige Formatierung innerhalb eines Absatzes. Dieser Leitfaden zeigt, wie man Fett-, Kursiv- und Farbformatierungen auf den Text in einer Präsentation anwenden kann.
Schritt für Schritte
Schritt 1: Installieren des Pakets
aspose-slides-foss/slides is not yet published — build from source until it ships. See the project README for build instructions.
Schritt 2: Fügen Sie eine Form mit einem Textrahmen hinzu
Vor dem Formatieren von Text muss eine Form einen TextFrame.- Verwenden . shape.AddTextFrame() um einen zu schaffen.
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 den Textrahmen
shape.AddTextFrame() gibt den TextFrame Objekt. Sie können es auch später über shape.TextFrame.
var tf = shape.TextFrame; // if the frame already exists
var tf = shape.AddTextFrame(""); // creates a new frameA) Die TextFrame enthält eine Liste von Paragraph Objekte (tf.Paragraphs) Jeder einzelne. Paragraph enthält: Portion Objekte (paragraph.Portions).
Schritt 4: Fett- und Kursivformatierung anwenden
Verwendung PortionFormat.FontBold und . PortionFormat.FontItalic.Diese Eigenschaften akzeptieren die NullableBool.True, NullableBool.False,oder , NullableBool.NotDefined (Erbe vom Meister).
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: Fontskala und Farbe festlegen
Setz PortionFormat.FontHeight für Größe (in Punkten) und Verwendung 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 0-255 für jeden Kanal.
Schritt 6: Mehrfach in einem Absatz
Ein einzelner Absatz kann mehrere Teile mit unterschiedlicher Formatierung enthalten. Portion Die Kommission hat die 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
Text erscheint auch nach Einstellung der Farbe schwarz
Sicherstellen FillFormat.FillType = FillType.Solid ohne den Fülltyp zu setzen, hat die Farbänderung möglicherweise keine Wirkung.
NullableBool.True gegen true
PortionFormat.FontBold Erwartungen NullableBool.True, nicht das C# true.Zuteilung von C# true wird nicht kompiliert, da die Typen unvereinbar sind.
Schriftart nicht in der gespeicherten Datei angezeigt
Die LatinFont Die Eigenschaft setzt die lateinische Schriftfamilie ein. Wenn nicht festgelegt, wird die Präsentations-Themenschrift verwendet. Benutzerdefinierte Schriften müssen eingebettet oder auf der Betrachtungsmaschine verfügbar sein.
Häufig gestellte Fragen
Wie ändere ich die Schriftfamilie?
Setz PortionFormat.LatinFont:
fmt.LatinFont = new FontData("Arial");FontData Akzeptiert den Schriftfamiliennamen als Zeichenfolge.
Wie kann ich die Absätze ausrichten?
Verwendung ParagraphFormat.Alignment:
tf.Paragraphs[0].ParagraphFormat.Alignment = TextAlignment.Center;Unterstützte Werte: Left, Center, Right, Justify.
Wie kann ich den Linienabstand einstellen?
Verwendung 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