איך לעצב טקסט ב-.NET
Aspose.Slides FOSS for .NET provides fine-grained text formatting through the PortionFormat מחלקה. A Portion הוא היחידה העצמאית הקטנה ביותר של טקסט; הוא ממופה לרצף עיצוב יחיד בתוך פסקה. מדריך זה מראה כיצד להחיל עיצוב מודגש, נטוי, גודל גופן וצבע על טקסט במצגת.
מדריך שלב אחר שלב
שלב 1: התקנת החבילה
dotnet add package Aspose.Slides.Fossשלב 2: הוסף צורה עם מסגרת טקסט
לפני עיצוב טקסט, צורה חייבת להכיל TextFrame. השתמש shape.AddTextFrame() ליצור אחת.
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);שלב 3: גש ל-TextFrame
shape.AddTextFrame() מחזיר את TextFrame אובייקט. אתה גם יכול לאחזר אותו מאוחר יותר דרך shape.TextFrame.
var tf = shape.TextFrame; // if the frame already exists
var tf = shape.AddTextFrame(""); // creates a new frameA TextFrame מכיל רשימה של Paragraph אובייקטים (tf.Paragraphs). כל Paragraph מכיל Portion אובייקטים (paragraph.Portions).
שלב 4: החל עיצוב מודגש ונטוי
השתמש PortionFormat.FontBold ו PortionFormat.FontItalic. תכונות אלו מקבלות NullableBool.True, NullableBool.False, או NullableBool.NotDefined (ירש מהמאסטר).
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);שלב 5: הגדר גודל גופן וצבע
הגדר PortionFormat.FontHeight לגודל (בנקודות) ולשימוש FillFormat לצבע.
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) מקבל ערכים 0-255 לכל ערוץ.
שלב 6: ריבוי חלקים בפסקה אחת
פסקה בודדת יכולה להכיל מספר חלקים עם עיצוב שונה. הוסף חדש Portion לפסקה Portions אוסף:
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);בעיות נפוצות ותיקונים
הטקסט מופיע שחור גם אחרי שהוגדר הצבע
ודא FillFormat.FillType = FillType.Solid מוגדר לפני הקצאת הצבע. ללא הגדרת סוג המילוי, שינוי הצבע עשוי לא להיות בעל השפעה.
NullableBool.True לעומת true
PortionFormat.FontBold מצפה NullableBool.True, ולא את C# true. הקצאת C# true לא יקומפל מכיוון שהסוגים אינם תואמים.
הגופן אינו מופיע בקובץ השמור
ה LatinFont המאפיין מגדיר את משפחת הגופנים הלטינית. אם לא מוגדר, נעשה שימוש בגופן של ערכת הנושא של המצגת. גופנים מותאמים אישית חייבים להיות משובצים או זמינים במחשב הצופה.
שאלות נפוצות
איך אני משנה את משפחת הגופנים?
הגדר PortionFormat.LatinFont:
fmt.LatinFont = new FontData("Arial");FontData מקבל את שם משפחת הגופנים כמחרוזת.
איך אני מגדיר יישור פסקה?
השתמש ParagraphFormat.Alignment:
tf.Paragraphs[0].ParagraphFormat.Alignment = TextAlignment.Center;ערכים נתמכים: Left, Center, Right, Justify.
איך אני מגדיר ריווח שורות?
השתמש ParagraphFormat.SpaceBefore (נקודות לפני פסקה) או ParagraphFormat.SpaceAfter (נקודות אחרי פסקה):
tf.Paragraphs[0].ParagraphFormat.SpaceBefore = 12; // 12pt before
tf.Paragraphs[0].ParagraphFormat.SpaceAfter = 6; // 6pt after