כיצד לעצב טקסט ב‑.NET
Aspose.Slides FOSS for .NET מספק עיצוב טקסט מדויק דרך המחלקה PortionFormat. 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 frameTextFrame מכיל רשימה של אובייקטים 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, ולא ל-true של C#. הקצאת 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