จัดรูปแบบข้อความการนำเสนอใน .NET
ภาพรวม
บทความนี้แสดงวิธีกำหนดรูปแบบข้อความในงานนำเสนอ PowerPoint และ OpenDocument ด้วย Aspose.Slides สำหรับ .NET ครอบคลุมสีพื้นหลัง, ความโปร่งแสง, ระยะห่างระหว่างอักขระ, คุณสมบัติตัวอักษร, การหมุน, ระยะห่างระหว่างย่อหน้า, พฤติกรรม Autofit, การยึดตำแหน่งข้อความ, จุดหยุดแท็บ, และการตั้งค่าภาษา.
ในตัวอย่างด้านล่าง เราจะใช้ไฟล์ชื่อ “sample.pptx” ซึ่งมีกล่องข้อความเดียวบนสไลด์แรกพร้อมข้อความต่อไปนี้:

เพื่อค้นหาและไฮไลต์ข้อความตามตัวหรือผลตรงกับ regular‑expression ดูที่ ค้นหาและแทนที่ข้อความ.
ตั้งค่าสีพื้นหลังของข้อความ
ใช้ IParagraphFormat.DefaultPortionFormat เพื่อตั้งค่าสีไฮไลต์เริ่มต้นสำหรับย่อหน้า หรือใช้ IBasePortionFormat.HighlightColor สำหรับส่วนข้อความแต่ละส่วน.
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีตั้งค่าสีพื้นหลังสำหรับ ย่อหน้าเต็ม:
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;
using (var presentation = new Presentation("sample.pptx"))
{
var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
var paragraph = autoShape.TextFrame.Paragraphs[0];
// ตั้งค่าสีไฮไลท์สำหรับย่อหน้าทั้งหมด.
paragraph.ParagraphFormat.DefaultPortionFormat.HighlightColor.Color = Color.LightGray;
presentation.Save("gray_paragraph.pptx", SaveFormat.Pptx);
}
ผลลัพธ์:

ตัวอย่างโค้ดด้านล่างแสดงวิธีตั้งค่าสีพื้นหลังสำหรับ ส่วนข้อความที่มีฟอนต์หนา:
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;
using (var presentation = new Presentation("sample.pptx"))
{
var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
var paragraph = autoShape.TextFrame.Paragraphs[0];
foreach (var portion in paragraph.Portions)
{
if (portion.PortionFormat.GetEffective().FontBold)
{
// ตั้งค่าสีไฮไลท์สำหรับส่วนข้อความ.
portion.PortionFormat.HighlightColor.Color = Color.LightGray;
}
}
presentation.Save("gray_text_portions.pptx", SaveFormat.Pptx);
}
ผลลัพธ์:

จัดย่อหน้าข้อความ
ใช้ IParagraphFormat.Alignment เพื่อตั้งค่าการจัดแนวย่อหน้าภายในกรอบข้อความ ค่าอาจเป็นศูนย์กลาง, จัดชิดซ้าย, จัดชิดขวา, เรียงแนวเส้น, เป็นต้น.
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีจัดแนวย่อหน้าให้อยู่ กึ่งกลาง:
using Aspose.Slides;
using Aspose.Slides.Export;
using (var presentation = new Presentation("sample.pptx"))
{
var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
var paragraph = autoShape.TextFrame.Paragraphs[0];
// ตั้งค่าการจัดแนวของย่อหน้าให้ศูนย์กลาง.
paragraph.ParagraphFormat.Alignment = TextAlignment.Center;
presentation.Save("aligned_paragraph.pptx", SaveFormat.Pptx);
}
ผลลัพธ์:

ตั้งค่าความโปร่งแสงสำหรับข้อความ
ความโปร่งแสงของข้อความควบคุมโดยส่วนประกอบอัลฟาของสีที่กำหนดให้กับ IBasePortionFormat.FillFormat. ในตัวอย่างด้านล่าง alpha = 50 เป็นค่าช่องอัลฟา ARGB บนสเกล 0–255 ไม่ใช่เปอร์เซ็นต์ความโปร่งแสง.
ตัวอย่างโค้ดด้านล่างแสดงวิธีใช้ความโปร่งแสงกับ ย่อหน้าเต็ม:
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;
int alpha = 50;
using (var presentation = new Presentation("sample.pptx"))
{
var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
var paragraph = autoShape.TextFrame.Paragraphs[0];
// ตั้งค่าสีเติมของข้อความให้เป็นสีโปร่งใส.
paragraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
paragraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.FromArgb(alpha, Color.Black);
presentation.Save("transparent_paragraph.pptx", SaveFormat.Pptx);
}
ผลลัพธ์:

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีใช้ความโปร่งแสงกับ ส่วนข้อความที่มีฟอนต์หนา:
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;
int alpha = 50;
using (var presentation = new Presentation("sample.pptx"))
{
var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
var paragraph = autoShape.TextFrame.Paragraphs[0];
foreach (var portion in paragraph.Portions)
{
if (portion.PortionFormat.GetEffective().FontBold)
{
// ตั้งค่าความโปร่งใสของส่วนข้อความ.
portion.PortionFormat.FillFormat.FillType = FillType.Solid;
portion.PortionFormat.FillFormat.SolidFillColor.Color = Color.FromArgb(alpha, Color.Black);
}
}
presentation.Save("transparent_text_portions.pptx", SaveFormat.Pptx);
}
ผลลัพธ์:

ตั้งค่าการเว้นระยะระหว่างอักขระของข้อความ
ใช้ IBasePortionFormat.Spacing เพื่อขยายหรือบีบอัดระยะห่างระหว่างอักขระในกล่องข้อความ.
โค้ด C# ต่อไปนี้แสดงวิธีขยายระยะห่างอักขระใน ย่อหน้าเต็ม:
using Aspose.Slides;
using Aspose.Slides.Export;
using (var presentation = new Presentation("sample.pptx"))
{
var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
var paragraph = autoShape.TextFrame.Paragraphs[0];
// หมายเหตุ: ใช้ค่าติดลบเพื่อบีบอัดระยะห่างระหว่างอักขระ.
paragraph.ParagraphFormat.DefaultPortionFormat.Spacing = 3; // ขยายระยะห่างอักขระ.
presentation.Save("character_spacing_in_paragraph.pptx", SaveFormat.Pptx);
}
ผลลัพธ์:

ตัวอย่างโค้ดด้านล่างแสดงวิธีขยายระยะห่างอักขระใน ส่วนข้อความที่มีฟอนต์หนา:
using Aspose.Slides;
using Aspose.Slides.Export;
using (var presentation = new Presentation("sample.pptx"))
{
var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
var paragraph = autoShape.TextFrame.Paragraphs[0];
foreach (var portion in paragraph.Portions)
{
if (portion.PortionFormat.GetEffective().FontBold)
{
// หมายเหตุ: ใช้ค่าติดลบเพื่อบีบอัดระยะห่างระหว่างอักขระ.
portion.PortionFormat.Spacing = 3; // ขยายระยะห่างอักขระ.
}
}
presentation.Save("character_spacing_in_text_portions.pptx", SaveFormat.Pptx);
}
ผลลัพธ์:

ปิดการใช้งาน Kerning สำหรับแบบอักษรเฉพาะ
ในบางกรณี ข้อความที่เรนเดอร์โดย Aspose.Slides อาจดูแน่นกว่าข้อความเดียวกันที่แสดงใน PowerPoint สิ่งนี้อาจเกิดจาก PowerPoint เพิกเฉยต่อข้อมูล kerning ของแบบอักษรบางตัว แม้ว่าแบบอักษรจะมีข้อมูล kerning ที่ถูกต้องและ kerning ถูกเปิดใช้งานในการตั้งค่า PowerPoint ก็ตาม.
เพื่อให้ผลลัพธ์ที่เรนเดอร์ใกล้เคียงกับ PowerPoint ในกรณีเหล่านี้ คุณสามารถปิดการใช้งาน kerning สำหรับส่วนข้อความที่ใช้แบบอักษรที่ได้รับผลกระทบ ตั้งค่า IBasePortionFormat.KerningMinimalSize ให้เป็นค่าที่ใหญ่กว่าขนาดฟอนต์จริงอย่างมีนัยสำคัญ:
using Aspose.Slides;
using Aspose.Slides.Export;
using (var presentation = new Presentation("presentation.pptx"))
{
var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
var targetFont = "Roboto";
foreach (var paragraph in autoShape.TextFrame.Paragraphs)
{
foreach (var portion in paragraph.Portions)
{
if ((portion.PortionFormat.LatinFont != null &&
portion.PortionFormat.LatinFont.FontName == targetFont) ||
(portion.PortionFormat.EastAsianFont != null &&
portion.PortionFormat.EastAsianFont.FontName == targetFont) ||
(portion.PortionFormat.ComplexScriptFont != null &&
portion.PortionFormat.ComplexScriptFont.FontName == targetFont))
{
portion.PortionFormat.KerningMinimalSize = 100;
}
}
}
presentation.Save("output.pptx", SaveFormat.Pptx);
}
การตั้งค่านี้ป้องกันไม่ให้ kerning ถูกนำไปใช้กับส่วนข้อความที่ตรงกันและช่วยให้การเรนเดอร์ของ Aspose.Slides สอดคล้องกับผลลัพธ์ภาพของ PowerPoint สำหรับแบบอักษรที่ได้รับผลกระทบจากพฤติกรรมเฉพาะของ PowerPoint นี้.
จัดการคุณสมบัติฟอนต์ของข้อความ
คุณสมบัติดิจิตอลของฟอนต์สามารถตั้งค่าที่ระดับย่อหน้าผ่าน IParagraphFormat.DefaultPortionFormat หรือที่ส่วนแต่ละส่วนผ่าน IPortionFormat.
โค้ดต่อไปนี้ตั้งค่าฟอนต์และสไตล์ข้อความสำหรับย่อหน้าเต็ม: จะใช้ขนาดฟอนต์, ตัวหนา, ตัวเอียง, ขีดเส้นใต้เป็นจุด, และฟอนต์ Times New Roman กับทุกส่วนในย่อหน้า.
using Aspose.Slides;
using Aspose.Slides.Export;
using (var presentation = new Presentation("sample.pptx"))
{
var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
var paragraph = autoShape.TextFrame.Paragraphs[0];
// ตั้งค่าคุณสมบัติดิจิตอลของฟอนต์สำหรับย่อหน้า.
paragraph.ParagraphFormat.DefaultPortionFormat.FontHeight = 12;
paragraph.ParagraphFormat.DefaultPortionFormat.FontBold = NullableBool.True;
paragraph.ParagraphFormat.DefaultPortionFormat.FontItalic = NullableBool.True;
paragraph.ParagraphFormat.DefaultPortionFormat.FontUnderline = TextUnderlineType.Dotted;
paragraph.ParagraphFormat.DefaultPortionFormat.LatinFont = new FontData("Times New Roman");
presentation.Save("font_properties_for_paragraph.pptx", SaveFormat.Pptx);
}
ผลลัพธ์:

ตัวอย่างโค้ดด้านล่างใช้คุณสมบัติเช่นเดียวกันกับ ส่วนข้อความที่มีฟอนต์หนา:
using Aspose.Slides;
using Aspose.Slides.Export;
using (var presentation = new Presentation("sample.pptx"))
{
var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
var paragraph = autoShape.TextFrame.Paragraphs[0];
foreach (var portion in paragraph.Portions)
{
if (portion.PortionFormat.GetEffective().FontBold)
{
// ตั้งค่าคุณสมบัติดิจิตอลของฟอนต์สำหรับส่วนข้อความ.
portion.PortionFormat.FontHeight = 13;
portion.PortionFormat.FontItalic = NullableBool.True;
portion.PortionFormat.FontUnderline = TextUnderlineType.Dotted;
portion.PortionFormat.LatinFont = new FontData("Times New Roman");
}
}
presentation.Save("font_properties_for_text_portions.pptx", SaveFormat.Pptx);
}
ผลลัพธ์:

ตั้งค่าการหมุนข้อความ
ใช้ ITextFrameFormat.TextVerticalType เพื่อตั้งค่าการจัดแนวข้อความที่กำหนดไว้ล่วงหน้าในรูปทรง.
ตัวอย่างโค้ดต่อไปนี้ตั้งการจัดแนวข้อความในรูปทรงเป็น Vertical270 ซึ่งจะหมุนข้อความ 90 องศาทวนเข็มนาฬิกา:
using Aspose.Slides;
using Aspose.Slides.Export;
using (var presentation = new Presentation("sample.pptx"))
{
var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
autoShape.TextFrame.TextFrameFormat.TextVerticalType = TextVerticalType.Vertical270;
presentation.Save("text_rotation.pptx", SaveFormat.Pptx);
}
ผลลัพธ์:

ตั้งค่าการหมุนแบบกำหนดเองสำหรับกรอบข้อความ
ใช้ ITextFrameFormat.RotationAngle เพื่อตั้งค่ามุมการหมุนแบบกำหนดเองสำหรับ ITextFrame.
ตัวอย่างโค้ดด้านล่างหมุนกรอบข้อความโดย 3 องศาตามเข็มนาฬิกาในรูปทรง:
using Aspose.Slides;
using Aspose.Slides.Export;
using (var presentation = new Presentation("sample.pptx"))
{
var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
autoShape.TextFrame.TextFrameFormat.RotationAngle = 3;
presentation.Save("custom_text_rotation.pptx", SaveFormat.Pptx);
}
ผลลัพธ์:

ตั้งค่าระยะห่างบรรทัดของย่อหน้า
Aspose.Slides มี IParagraphFormat.SpaceAfter, IParagraphFormat.SpaceBefore, และ IParagraphFormat.SpaceWithin เพื่อควบคุมระยะห่างของย่อหน้า คุณสมบัติเหล่านี้ใช้ดังนี้:
- ใช้ค่าบวกเพื่อระบุตัวหาระยะห่างบรรทัดเป็นเปอร์เซ็นต์ของความสูงบรรทัด
- ใช้ค่าลบเพื่อระบุตัวหาระยะห่างบรรทัดเป็นหน่วยจุด
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีระบุตัวหาระยะห่างบรรทัดภายในย่อหน้า:
using Aspose.Slides;
using Aspose.Slides.Export;
using (var presentation = new Presentation("sample.pptx"))
{
var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
var paragraph = autoShape.TextFrame.Paragraphs[0];
paragraph.ParagraphFormat.SpaceWithin = 200;
presentation.Save("line_spacing.pptx", SaveFormat.Pptx);
}
ผลลัพธ์:

ตั้งค่าชนิด Autofit สำหรับกรอบข้อความ
ITextFrameFormat.AutofitType กำหนดว่าข้อความจะทำงานอย่างไรเมื่อเกินขอบเขตของคอนเทนเนอร์ ใช้เพื่อควบคุมว่าข้อความจะหดลง, ไหลออกนอก, หรือปรับขนาดรูปทรงโดยอัตโนมัติ
using Aspose.Slides;
using Aspose.Slides.Export;
using (var presentation = new Presentation("sample.pptx"))
{
var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
autoShape.TextFrame.TextFrameFormat.AutofitType = TextAutofitType.Shape;
presentation.Save("autofit_type.pptx", SaveFormat.Pptx);
}
ตั้งค่าจุดยึดของกรอบข้อความ
ITextFrameFormat.AnchoringType กำหนดว่าข้อความอยู่ในตำแหน่งแนวตั้งภายในรูปทรงอย่างไร เช่น ด้านบน, กลาง, หรือด้านล่าง
using Aspose.Slides;
using Aspose.Slides.Export;
using (var presentation = new Presentation("sample.pptx"))
{
var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
autoShape.TextFrame.TextFrameFormat.AnchoringType = TextAnchorType.Bottom;
presentation.Save("text_anchor.pptx", SaveFormat.Pptx);
}
ตั้งค่าการแท็บข้อความ
ใช้ IParagraphFormat.DefaultTabSize และ IParagraphFormat.Tabs เพื่อกำหนดจุดหยุดแท็บในย่อหน้า
using Aspose.Slides;
using Aspose.Slides.Export;
using (var presentation = new Presentation("sample.pptx"))
{
var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
var paragraph = autoShape.TextFrame.Paragraphs[0];
paragraph.ParagraphFormat.DefaultTabSize = 100;
paragraph.ParagraphFormat.Tabs.Add(30, TabAlignment.Left);
presentation.Save("paragraph_tabs.pptx", SaveFormat.Pptx);
}
ผลลัพธ์:

ตั้งค่าภาษา Proofing
Aspose.Slides มี IBasePortionFormat.LanguageId ซึ่งให้คุณตั้งค่าภาษา proofing สำหรับส่วนข้อความ ภาษ proofing กำหนดภาษาที่ใช้ในการตรวจสอบการสะกดและไวยากรณ์ใน PowerPoint
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีตั้งค่าภาษา proofing สำหรับส่วนข้อความ:
using Aspose.Slides;
using Aspose.Slides.Export;
using (var presentation = new Presentation("presentation.pptx"))
{
var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
var paragraph = autoShape.TextFrame.Paragraphs[0];
paragraph.Portions.Clear();
var font = new FontData("SimSun");
var textPortion = new Portion();
textPortion.PortionFormat.ComplexScriptFont = font;
textPortion.PortionFormat.EastAsianFont = font;
textPortion.PortionFormat.LatinFont = font;
// ตั้งค่า Id ของภาษาการตรวจสอบ.
textPortion.PortionFormat.LanguageId = "zh-CN";
textPortion.Text = "1。";
paragraph.Portions.Add(textPortion);
presentation.Save("proofing_language.pptx", SaveFormat.Pptx);
}
ตั้งค่าภาษาเริ่มต้น
ใช้ LoadOptions.DefaultTextLanguage เพื่อกำหนดภาษาตั้งต้นสำหรับข้อความที่สร้างระหว่างการโหลดหรือสร้างการนำเสนอ
using Aspose.Slides;
var loadOptions = new LoadOptions();
loadOptions.DefaultTextLanguage = "en-US";
using (var presentation = new Presentation(loadOptions))
{
var slide = presentation.Slides[0];
// เพิ่มรูปร่างสี่เหลี่ยมผืนผ้าใหม่พร้อมข้อความ.
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 20, 20, 150, 50);
shape.TextFrame.Text = "Sample text";
// ตรวจสอบภาษาของส่วนข้อความแรก.
var portion = shape.TextFrame.Paragraphs[0].Portions[0];
Console.WriteLine(portion.PortionFormat.LanguageId);
}
ตั้งค่ารูปแบบข้อความเริ่มต้น
เพื่อใช้การจัดรูปแบบข้อความเริ่มต้นระดับการนำเสนอ ให้ใช้ IPresentation.DefaultTextStyle
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีตั้งค่าแบบอักษรหนาเริ่มต้นขนาด 14 pt สำหรับข้อความทั้งหมดทั่วสไลด์ในการนำเสนอใหม่
using Aspose.Slides;
using Aspose.Slides.Export;
using (var presentation = new Presentation())
{
// รับรูปแบบย่อหน้าระดับบน
var paragraphFormat = presentation.DefaultTextStyle.GetLevel(0);
if (paragraphFormat != null)
{
paragraphFormat.DefaultPortionFormat.FontHeight = 14;
paragraphFormat.DefaultPortionFormat.FontBold = NullableBool.True;
}
presentation.Save("default_text_style.pptx", SaveFormat.Pptx);
}
ดึงข้อความด้วยเอฟเฟกต์ All-Caps
ใน PowerPoint การใช้เอฟเฟกต์ฟอนต์ All Caps ทำให้ข้อความปรากฏเป็นตัวพิมพ์ใหญ่บนสไลด์แม้ว่าจะพิมพ์เป็นตัวพิมพ์เล็กเดิม เมื่อคุณดึงส่วนข้อความดังกล่าวด้วย Aspose.Slides ไลบรารีจะคืนค่าข้อความตามที่ป้อนไว้ เพื่อให้ตรงกับข้อความที่แสดง ตรวจสอบ TextCapType และแปลงสตริงที่คืนค่ามาเป็นตัวพิมพ์ใหญ่เมื่อค่าคือ All.
สมมติว่าเรามีกล่องข้อความต่อไปนี้บนสไลด์แรกของไฟล์ sample2.pptx

ตัวอย่างโค้ดด้านล่างแสดงวิธีดึงข้อความที่มีเอฟเฟกต์ All Caps ที่ใช้:
using Aspose.Slides;
using (var presentation = new Presentation("sample2.pptx"))
{
var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
var textPortion = autoShape.TextFrame.Paragraphs[0].Portions[0];
Console.WriteLine($"Original text: {textPortion.Text}");
var textFormat = textPortion.PortionFormat.GetEffective();
if (textFormat.TextCapType == TextCapType.All)
{
var text = textPortion.Text.ToUpper();
Console.WriteLine($"All-Caps effect: {text}");
}
}
ผลลัพธ์:
Original text: Hello, Aspose!
All-Caps effect: HELLO, ASPOSE!
คำถามที่พบบ่อย
วิธีแก้ไขข้อความในตารางบนสไลด์?
เพื่อแก้ไขข้อความในตารางบนสไลด์ ให้ใช้ ITable. วนลูปผ่านเซลล์และอัปเดตแต่ละเซลล์ผ่าน ICell.TextFrame และการจัดรูปแบบย่อหน้าผ่าน IParagraph.ParagraphFormat.
วิธีใช้สีไล่ระดับกับข้อความในสไลด์ PowerPoint?
เพื่อใช้สีไล่ระดับกับข้อความ ให้ใช้ IBasePortionFormat.FillFormat. ตั้งค่า IFillFormat.FillType เป็น FillType.Gradient และกำหนดจุดหยุดไล่ระดับ, ทิศทาง, และความโปร่งแสง.