จัดการย่อหน้าข้อความ PowerPoint ใน .NET
ภาพรวม
Aspose.Slides for .NET แสดงข้อความเป็นลำดับขั้นของ text frames, paragraphs, และ portions:
- ITextFrame แสดงคอนเทนเนอร์ข้อความในรูปร่างและให้การเข้าถึงคอลเลกชันของ paragraph
- IParagraph แสดง paragraph หนึ่งใน text frame และให้การเข้าถึง portions ของมันพร้อมการจัดรูปแบบระดับ paragraph
- IPortion แสดง text run ภายใน paragraph แต่ละ portion สามารถมีข้อความและการจัดรูปแบบระดับอักขระของตนเองได้
ดังนั้น paragraph จึงสามารถมีข้อความที่ใช้ฟอนต์ สี ขนาด และการจัดรูปแบบอื่น ๆ ที่แตกต่างกันได้โดยใช้หลาย portion
สร้างและจัดรูปแบบย่อหน้า
สร้างย่อหน้าใน text frame ของรูปร่างและใช้ portions เพื่อจัดรูปแบบ text run แยกแต่ละส่วน ส่วนนี้จะแสดงวิธีการรวมหลาย portion ภายในแต่ละ paragraph และใช้สี ขนาดฟอนต์ และสไตล์ที่ต่างกัน
สร้างย่อหน้าด้วยหลาย Portion
ตัวอย่าง C# ด้านล่างสร้างสาม paragraph โดยแต่ละ paragraph มีสาม text run อยู่ในคอลเลกชัน IParagraph.Portions ของมัน ตัวอย่างนี้ใช้ paragraph เริ่มต้นและ portion ที่ว่างเปล่าเป็นฐาน แล้วเพิ่ม paragraph และ portion ที่เหลือ การจัดรูปแบบทำผ่าน IPortion.PortionFormat โดย portion แรกของแต่ละ paragraph มีสีแดง ตัวหนา และขนาด 15 pt; portion ที่สองมีสีน้ำเงิน ตัวเอียง และขนาด 18 pt; ส่วนที่สามคงการจัดรูปแบบเริ่มต้นไว้:
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 150, 300, 150);
var textFrame = shape.TextFrame;
var firstParagraph = textFrame.Paragraphs[0];
firstParagraph.Portions.Add(new Portion());
firstParagraph.Portions.Add(new Portion());
var secondParagraph = new Paragraph();
secondParagraph.Portions.Add(new Portion());
secondParagraph.Portions.Add(new Portion());
secondParagraph.Portions.Add(new Portion());
textFrame.Paragraphs.Add(secondParagraph);
var thirdParagraph = new Paragraph();
thirdParagraph.Portions.Add(new Portion());
thirdParagraph.Portions.Add(new Portion());
thirdParagraph.Portions.Add(new Portion());
textFrame.Paragraphs.Add(thirdParagraph);
var paragraphCount = textFrame.Paragraphs.Count;
for (var paragraphIndex = 0; paragraphIndex < paragraphCount; paragraphIndex++)
{
var paragragaph = textFrame.Paragraphs[paragraphIndex];
var portionCount = paragragaph.Portions.Count;
for (var portionIndex = 0; portionIndex < portionCount; portionIndex++)
{
var portion = paragragaph.Portions[portionIndex];
portion.Text = $"Portion {paragraphIndex + 1}.{portionIndex + 1}";
if (portionIndex == 0)
{
portion.PortionFormat.FillFormat.FillType = FillType.Solid;
portion.PortionFormat.FillFormat.SolidFillColor.Color = Color.Red;
portion.PortionFormat.FontBold = NullableBool.True;
portion.PortionFormat.FontHeight = 15;
}
else if (portionIndex == 1)
{
portion.PortionFormat.FillFormat.FillType = FillType.Solid;
portion.PortionFormat.FillFormat.SolidFillColor.Color = Color.Blue;
portion.PortionFormat.FontItalic = NullableBool.True;
portion.PortionFormat.FontHeight = 18;
}
}
}
presentation.Save("paragraphs_with_portions.pptx", SaveFormat.Pptx);
สร้างรายการหัวข้อและรายการลำดับเลข
จัดข้อความในย่อหน้าให้เป็นรายการที่ใช้สัญลักษณ์ ตัวเลข หรือรูปภาพแบบกำหนดเอง ตัวอย่างในส่วนนี้ครอบคลุมการจัดรูปแบบ bullet รายการหลายระดับ และการกำหนดหมายเลขเริ่มต้นแบบกำหนดเอง
สร้างรายการหัวข้อหรือรายการลำดับเลข
Bullet และการนับเลขช่วยให้มองรายการที่เกี่ยวข้องได้ง่ายขึ้น ใน Aspose.Slides การตั้งค่ารายการถูกกำหนดผ่าน IBulletFormat
ตัวอย่าง C# นี้เพิ่ม bullet แบบสัญลักษณ์และรายการนับเลขลงใน text frame เดียวกัน IBulletFormat.Type ระบุประเภทของ bullet, IBulletFormat.Char ให้สัญลักษณ์, และ IBulletFormat.NumberedBulletStyle เลือกสไตล์การนับเลข ทั้งสองรายการใช้ bullet สีดำ สูง 100 % และมีการเยื้อง paragraph 25 pt:
using System;
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);
var textFrame = shape.TextFrame;
textFrame.Paragraphs.Clear();
var symbolParagraph = new Paragraph { Text = "Welcome to Aspose.Slides" };
symbolParagraph.ParagraphFormat.Bullet.Type = BulletType.Symbol;
symbolParagraph.ParagraphFormat.Bullet.Char = Convert.ToChar(0x2022);
symbolParagraph.ParagraphFormat.Indent = 25;
symbolParagraph.ParagraphFormat.Bullet.Color.ColorType = ColorType.RGB;
symbolParagraph.ParagraphFormat.Bullet.Color.Color = Color.Black;
symbolParagraph.ParagraphFormat.Bullet.IsBulletHardColor = NullableBool.True;
symbolParagraph.ParagraphFormat.Bullet.Height = 100;
textFrame.Paragraphs.Add(symbolParagraph);
var numberedParagraph = new Paragraph { Text = "This is a numbered item" };
numberedParagraph.ParagraphFormat.Bullet.Type = BulletType.Numbered;
numberedParagraph.ParagraphFormat.Bullet.NumberedBulletStyle = NumberedBulletStyle.BulletCircleNumWDBlackPlain;
numberedParagraph.ParagraphFormat.Indent = 25;
numberedParagraph.ParagraphFormat.Bullet.Color.ColorType = ColorType.RGB;
numberedParagraph.ParagraphFormat.Bullet.Color.Color = Color.Black;
numberedParagraph.ParagraphFormat.Bullet.IsBulletHardColor = NullableBool.True;
numberedParagraph.ParagraphFormat.Bullet.Height = 100;
textFrame.Paragraphs.Add(numberedParagraph);
presentation.Save("bulleted_and_numbered_list.pptx", SaveFormat.Pptx);
ใช้ Picture Bullets
Picture bullets ทำให้คุณใช้รูปภาพกำหนดเองแทนสัญลักษณ์หรือหมายเลข
ตัวอย่าง C# นี้เพิ่มรูปภาพลงในคอลเลกชันภาพของงานนำเสนอเป็น IPPImage แล้วใช้เป็น bullet ของ paragraph มันตั้งค่า IBulletFormat.Type ให้ใช้ picture bullet และกำหนดภาพผ่าน IBulletFormat.Picture IBulletFormat.Height ถูกตั้งเป็น 100 % เพื่อกำหนดขนาด bullet ให้สัมพันธ์กับข้อความของ paragraph:
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
using var bulletImage = Images.FromFile("bullets.png");
var presentationImage = presentation.Images.AddImage(bulletImage);
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);
var textFrame = shape.TextFrame;
textFrame.Paragraphs.Clear();
var paragraph = new Paragraph { Text = "Welcome to Aspose.Slides" };
paragraph.ParagraphFormat.Bullet.Type = BulletType.Picture;
paragraph.ParagraphFormat.Bullet.Picture.Image = presentationImage;
paragraph.ParagraphFormat.Bullet.Height = 100;
textFrame.Paragraphs.Add(paragraph);
presentation.Save("picture_bullet.pptx", SaveFormat.Pptx);
presentation.Save("picture_bullet.ppt", SaveFormat.Ppt);
สร้างรายการหลายระดับ
ตั้งค่า IParagraphFormat.Depth เพื่อวาง paragraph ไว้ในระดับต่าง ๆ ของรายการ ระดับบนสุดมีค่า depth เป็น 0
ตัวอย่าง C# นี้กำหนดค่า IParagraphFormat.Depth ตั้งแต่ 0 ถึง 3 ให้กับสี่ paragraph เพื่อสร้างลำดับชั้นภายใน text frame หนึ่ง paragraph ใช้ bullet แบบสัญลักษณ์โดย IBulletFormat.Char สลับระหว่าง bullet วงกลมและ hyphen เพื่อแยกระดับที่อยู่ติดกัน:
using System;
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);
var textFrame = shape.TextFrame;
textFrame.Paragraphs.Clear();
var firstParagraph = new Paragraph { Text = "Content" };
firstParagraph.ParagraphFormat.Bullet.Type = BulletType.Symbol;
firstParagraph.ParagraphFormat.Bullet.Char = Convert.ToChar(0x2022);
firstParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
firstParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
firstParagraph.ParagraphFormat.Depth = 0;
var secondParagraph = new Paragraph { Text = "Second level" };
secondParagraph.ParagraphFormat.Bullet.Type = BulletType.Symbol;
secondParagraph.ParagraphFormat.Bullet.Char = '-';
secondParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
secondParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
secondParagraph.ParagraphFormat.Depth = 1;
var thirdParagraph = new Paragraph { Text = "Third level" };
thirdParagraph.ParagraphFormat.Bullet.Type = BulletType.Symbol;
thirdParagraph.ParagraphFormat.Bullet.Char = Convert.ToChar(0x2022);
thirdParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
thirdParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
thirdParagraph.ParagraphFormat.Depth = 2;
var fourthParagraph = new Paragraph { Text = "Fourth level" };
fourthParagraph.ParagraphFormat.Bullet.Type = BulletType.Symbol;
fourthParagraph.ParagraphFormat.Bullet.Char = '-';
fourthParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
fourthParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
fourthParagraph.ParagraphFormat.Depth = 3;
textFrame.Paragraphs.Add(firstParagraph);
textFrame.Paragraphs.Add(secondParagraph);
textFrame.Paragraphs.Add(thirdParagraph);
textFrame.Paragraphs.Add(fourthParagraph);
presentation.Save("multilevel_list.pptx", SaveFormat.Pptx);
กำหนดค่าเริ่มต้นของรายการนับเลขแบบกำหนดเอง
ใช้ IBulletFormat.NumberedBulletStartWith เพื่อตั้งค่าตัวเลขเริ่มต้นที่แสดงสำหรับ paragraph ที่นับเลข
ตัวอย่าง C# นี้ตั้งค่า IBulletFormat.Type ให้ใช้การนับเลขสำหรับสาม paragraph และกำหนดค่า IBulletFormat.NumberedBulletStartWith เป็น 2, 3, และ 7 ค่าต่าง ๆ นี้กำหนดแยกกันสำหรับแต่ละ paragraph ทำให้ตัวเลขที่แสดงข้ามค่าได้:
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);
var textFrame = shape.TextFrame;
textFrame.Paragraphs.Clear();
var firstParagraph = new Paragraph { Text = "Start at 2" };
firstParagraph.ParagraphFormat.Bullet.Type = BulletType.Numbered;
firstParagraph.ParagraphFormat.Bullet.NumberedBulletStartWith = 2;
textFrame.Paragraphs.Add(firstParagraph);
var secondParagraph = new Paragraph { Text = "Start at 3" };
secondParagraph.ParagraphFormat.Bullet.Type = BulletType.Numbered;
secondParagraph.ParagraphFormat.Bullet.NumberedBulletStartWith = 3;
textFrame.Paragraphs.Add(secondParagraph);
var thirdParagraph = new Paragraph { Text = "Start at 7" };
thirdParagraph.ParagraphFormat.Bullet.Type = BulletType.Numbered;
thirdParagraph.ParagraphFormat.Bullet.NumberedBulletStartWith = 7;
textFrame.Paragraphs.Add(thirdParagraph);
presentation.Save("custom_numbered_list.pptx", SaveFormat.Pptx);
ควบคุมการจัดวางและคุณสมบัติสิ้นสุดของ Paragraph
ปรับระยะห่างและการเยื้องของ paragraph เพื่อควบคุมตำแหน่งของบรรทัดแรกและบรรทัดที่ต่อเนื่อง คุณยังสามารถจัดรูปแบบสัญลักษณ์สิ้นสุดของ paragraph แยกจากส่วนข้อความที่มองเห็นได้
ตั้งค่า First-Line Indent
ใช้คุณสมบัติ IParagraphFormat.Indent เพื่อควบคุมการเยื้องบรรทัดแรกของ paragraph คุณสมบัตินี้ย้ายเฉพาะบรรทัดแรกสัมพันธ์กับ margin ด้านซ้ายของ paragraph ค่าเป็นบวกจะเลื่อนบรรทัดแรกไปทางขวา ส่วนบรรทัดที่เหลือยังคงอยู่ในแนวกับเนื้อหา paragraph
ใช้ IParagraphFormat.MarginLeft เมื่อคุณต้องการย้ายทั้ง paragraph ใช้ IParagraphFormat.Indent เมื่อต้องการย้ายเฉพาะบรรทัดแรกเท่านั้น
ตัวอย่างต่อไปนี้ให้สาม paragraph มี IParagraphFormat.MarginLeft เท่ากับ 20 pt และค่า IParagraphFormat.Indent เป็น 0, 20, และ 40 pt บรรทัดแรกเริ่มต้นที่การเยื้องเพิ่มขึ้นทีละขั้น ในขณะที่บรรทัดที่ต่อเนื่องยังคง margin ด้านซ้ายเหมือนเดิม:
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 420, 220);
shape.FillFormat.FillType = FillType.NoFill;
shape.LineFormat.FillFormat.FillType = FillType.Solid;
shape.LineFormat.FillFormat.SolidFillColor.Color = Color.Gray;
var textFrame = shape.TextFrame;
textFrame.TextFrameFormat.AutofitType = TextAutofitType.Shape;
textFrame.Paragraphs.Clear();
var firstParagraph = new Paragraph { Text = "No first-line indent. Wrapped lines start at the same position as the first line." };
firstParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
firstParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
firstParagraph.ParagraphFormat.MarginLeft = 20;
firstParagraph.ParagraphFormat.Indent = 0;
var secondParagraph = new Paragraph { Text = "First-line indent of 20 points. The first line moves to the right, while wrapped lines remain aligned to the paragraph body." };
secondParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
secondParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
secondParagraph.ParagraphFormat.MarginLeft = 20;
secondParagraph.ParagraphFormat.Indent = 20;
var thirdParagraph = new Paragraph { Text = "First-line indent of 40 points. This paragraph shows a larger first-line offset to make the effect easier to see." };
thirdParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
thirdParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
thirdParagraph.ParagraphFormat.MarginLeft = 20;
thirdParagraph.ParagraphFormat.Indent = 40;
textFrame.Paragraphs.Add(firstParagraph);
textFrame.Paragraphs.Add(secondParagraph);
textFrame.Paragraphs.Add(thirdParagraph);
presentation.Save("paragraph_indent.pptx", SaveFormat.Pptx);
ผลลัพธ์:

ตั้งค่า Hanging Indent
Hanging indent คือการจัดวาง paragraph ที่บรรทัดแรกเริ่มอยู่ด้านซ้ายของบรรทัดที่เหลือ ใน Aspose.Slides คุณสร้างเอฟเฟกต์นี้ด้วยคุณสมบัติ IParagraphFormat.Indent ตั้งค่า Indent ให้เป็นค่าลบเพื่อย้ายบรรทัดแรกไปทางซ้ายสัมพันธ์กับเนื้อหา paragraph
โดยปฏิบัติ, IParagraphFormat.MarginLeft กำหนดตำแหน่งซ้ายของเนื้อหา paragraph, ส่วน IParagraphFormat.Indent กำหนดตำแหน่งของบรรทัดแรกสัมพันธ์กับ margin นั้น เพื่อสร้าง hanging indent ให้ตั้งค่า MarginLeft เป็นบวกและ Indent เป็นลบ
การจัดรูปแบบนี้มีประโยชน์สำหรับบรรณานุกรม, การอ้างอิง, รายการอภิธานศัพท์, และ paragraph อื่น ๆ ที่บรรทัดต่อเนื่องต้องเรียงกันภายใต้เนื้อหา paragraph แทนที่จะอยู่ใต้ตัวอักษรแรกของบรรทัดแรก
ตัวอย่าง C# ด้านล่างจับคู่ค่า IParagraphFormat.MarginLeft 40 และ 60 pt กับค่า IParagraphFormat.Indent –20 และ –30 pt ในแต่ละ paragraph บรรทัดแรกของแต่ละ paragraph เริ่มต้นซ้ายของบรรทัดต่อเนื่อง; paragraph ที่สองมี hanging indent ลึกกว่า:
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 420, 220);
shape.FillFormat.FillType = FillType.NoFill;
shape.LineFormat.FillFormat.FillType = FillType.Solid;
shape.LineFormat.FillFormat.SolidFillColor.Color = Color.Gray;
var textFrame = shape.TextFrame;
textFrame.TextFrameFormat.AutofitType = TextAutofitType.Shape;
textFrame.Paragraphs.Clear();
var firstParagraph = new Paragraph { Text = "A hanging indent is created by combining a positive left margin with a negative indent. The first line starts to the left, while wrapped lines align with the paragraph body." };
firstParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
firstParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
firstParagraph.ParagraphFormat.MarginLeft = 40;
firstParagraph.ParagraphFormat.Indent = -20;
var secondParagraph = new Paragraph { Text = "This second example uses a deeper hanging indent so the difference between the first line and the wrapped lines is easier to compare." };
secondParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
secondParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
secondParagraph.ParagraphFormat.MarginLeft = 60;
secondParagraph.ParagraphFormat.Indent = -30;
textFrame.Paragraphs.Add(firstParagraph);
textFrame.Paragraphs.Add(secondParagraph);
presentation.Save("hanging_indent.pptx", SaveFormat.Pptx);
ผลลัพธ์:

ตั้งค่าคุณสมบัติสิ้นสุดของ Paragraph Run
คุณสมบัติ IParagraph.EndParagraphPortionFormat ควบคุมการจัดรูปแบบของสัญลักษณ์สิ้นสุด paragraph ตัวอย่างต่อไปนี้สร้าง PortionFormat โดยกำหนด IBasePortionFormat.FontHeight เป็น 48 pt และ IBasePortionFormat.LatinFont เป็น Times New Roman จากนั้นนำรูปแบบนี้ไปใช้กับสัญลักษณ์สิ้นสุดของ paragraph ที่สองโดยไม่กระทบต่อ portion ที่มองเห็นได้:
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation("Test.pptx");
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 10, 10, 200, 250);
var textFrame = shape.TextFrame;
textFrame.Paragraphs.Clear();
var firstParagraph = new Paragraph();
firstParagraph.Portions.Add(new Portion("Sample text"));
var secondParagraph = new Paragraph();
secondParagraph.Portions.Add(new Portion("Sample text 2"));
var endParagraphFormat = new PortionFormat();
endParagraphFormat.FontHeight = 48;
endParagraphFormat.LatinFont = new FontData("Times New Roman");
secondParagraph.EndParagraphPortionFormat = endParagraphFormat;
textFrame.Paragraphs.Add(firstParagraph);
textFrame.Paragraphs.Add(secondParagraph);
presentation.Save("end_paragraph_format.pptx", SaveFormat.Pptx);
นับจำนวนบรรทัดที่แสดงผล
สำหรับกฎของ paragraph ที่ส่งผลต่อการห่ออัตโนมัติและเครื่องหมายวรรคตอนที่จบบรรทัด ดูที่ Control Line Breaking และ Control Hanging Punctuation
ใช้ IParagraph.GetLinesCount เพื่อวัดจำนวนบรรทัดที่ paragraph ใช้หลังจากการจัดวางข้อความ รวมถึงการห่ออัตโนมัติ ซึ่งเป็นประโยชน์เมื่อทำการตรวจสอบความยาวของข้อความและการจัดวางในเทมเพลตการนำเสนอ
paragraph เป็นรายการหนึ่งใน ITextFrame.Paragraphs และอาจใช้หลายบรรทัดที่แสดงผล การขึ้นบรรทัดใหม่โดยเจตนาใน paragraph จะบังคับให้เกิดบรรทัดใหม่โดยไม่สร้าง paragraph ใหม่ การห่ออัตโนมัติจะสร้างบรรทัดตามความกว้างที่มีอยู่โดยไม่แทรกตัวอักษรขึ้นบรรทัดใหม่ในข้อความ ดังนั้นการนับจำนวน paragraph หรืออักขระการขึ้นบรรทัดไม่ได้ให้จำนวนบรรทัดที่แสดงผล
ตัวอย่างต่อไปนี้สร้างรูปข้อความ, นับบรรทัด, ลดความกว้างของรูป, แล้วแทนที่ข้อความด้วยสตริงสั้นกว่า ตัวอย่างเปิดใช้งาน ITextFrameFormat.WrapText และปิด ITextFrameFormat.AutofitType เพื่อให้ความกว้างของรูปควบคุมการห่อโดยไม่ให้ข้อความหดหรือรูปเปลี่ยนขนาด การวัดแต่ละครั้งใช้ IParagraph.GetLinesCount. ขนาดของรูประบุเป็นจุด สุดท้ายตัวอย่างเพิ่ม paragraph อีกหนึ่งและรวมจำนวนบรรทัดทั้งหมดของ text frame:
using System;
using Aspose.Slides;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 400, 200);
var textFrame = shape.TextFrame;
textFrame.TextFrameFormat.WrapText = NullableBool.True;
textFrame.TextFrameFormat.AutofitType = TextAutofitType.None;
var paragraph = textFrame.Paragraphs[0];
paragraph.ParagraphFormat.DefaultPortionFormat.FontHeight = 20;
paragraph.Text = "This text demonstrates how automatic wrapping changes the number of rendered lines.";
Console.WriteLine($"Original width: {paragraph.GetLinesCount()}");
shape.Width = 150;
Console.WriteLine($"Narrower shape: {paragraph.GetLinesCount()}");
paragraph.Text = "Short text.";
Console.WriteLine($"Shorter text: {paragraph.GetLinesCount()}");
var secondParagraph = new Paragraph { Text = "Another paragraph." };
secondParagraph.ParagraphFormat.DefaultPortionFormat.FontHeight = 20;
textFrame.Paragraphs.Add(secondParagraph);
var totalLineCount = 0;
foreach (var currentParagraph in textFrame.Paragraphs)
{
totalLineCount += currentParagraph.GetLinesCount();
}
Console.WriteLine($"Total lines in the text frame: {totalLineCount}");
ด้วยข้อความและขนาดเหล่านี้ การทำให้รูปแคบลงจะเพิ่มจำนวนบรรทัด ส่วนการแทนที่ข้อความด้วยสตริงสั้นจะลดจำนวนบรรทัด จำนวนที่ได้อาจแตกต่างตามฟอนต์ที่มีและการแทนที่, ขนาดฟอนต์, margin, การเยื้อง, การห่อ, และการตั้งค่า autofit ใช้ฟอนต์และการตั้งค่าการจัดวางที่ตั้งใจให้สอดคล้องกับสภาพแวดล้อมเป้าหมายเมื่อทำการตรวจสอบเทมเพลต
จำนวนบรรทัดเพียงอย่างเดียวไม่บ่งบอกว่าข้อความล้นคอนเทนเนอร์หรือไม่ ความสูงที่มี, ความสูงของบรรทัด, ระยะห่างของ paragraph และบรรทัด, รวมถึงพฤติกรรม autofit ก็มีผลด้วย; แม้บรรทัดเดียวก็อาจเกินความกว้างที่มีเมื่อปิดการห่อ
นำเข้าและส่งออกเนื้อหา Paragraph
ใช้ HTML นำข้อความเข้าสู่การนำเสนอหรือส่งออก paragraph ที่มีอยู่เพื่อใช้นอกงานนำเสนอ ส่วนนี้ยังแสดงวิธีการเรนเดอร์ paragraph แยกเป็นภาพ รวมถึง paragraph ในเซลล์ตาราง
นำเข้า HTML Text ไปยัง Paragraphs
ใช้ ParagraphCollection.AddFromHtml เพื่อแปลง markup HTML ไปเป็น paragraph และ portion ใน text frame
ตัวอย่าง C# นี้อ่าน HTML จากไฟล์แล้วส่ง markup ให้กับ ParagraphCollection.AddFromHtml มันล้าง paragraph เริ่มต้นของ text frame เพื่อให้ฟิลด์มีเฉพาะเนื้อหาที่นำเข้า รูปสี่เหลี่ยมทิ้ง margin 10 pt ไว้ด้านทุกด้านของสไลด์:
using System.IO;
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shapeWidth = presentation.SlideSize.Size.Width - 20;
var shapeHeight = presentation.SlideSize.Size.Height - 20;
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 10, 10, shapeWidth, shapeHeight);
shape.FillFormat.FillType = FillType.NoFill;
shape.TextFrame.Paragraphs.Clear();
using var reader = new StreamReader("file.html");
var html = reader.ReadToEnd();
shape.TextFrame.Paragraphs.AddFromHtml(html);
presentation.Save("html_text.pptx", SaveFormat.Pptx);
ส่งออกข้อความ Paragraph เป็น HTML
ใช้ ParagraphCollection.ExportToHtml เพื่อส่งออกช่วง paragraph ที่เลือกเป็น HTML เมื่อต้องการใช้ข้อความจากงานนำเสนอบนเว็บไซต์อาจต้องการรักษา emphasis ที่กำหนดไว้ในระดับ local โดยไม่ต้องนำฟอนต์, ขนาด หรือสไตล์ของกล่องข้อความของงานนำเข้ามาด้วย ตั้งค่า TextToHtmlConversionOptions.TextInheritanceLimit เพื่อควบคุมความลึกของการสืบทอดรูปแบบของตัวส่งออก
ดาวน์โหลด ตัวอย่างงานนำเสนอ แล้ววางไว้ในไดเรกทอรีการทำงานของแอปพลิเคชัน สไลด์แรกของไฟล์นี้มีรูปข้อความหนึ่งที่มีข้อความ “Inherited text; explicit red 18pt”. อินพุตใช้หลายระดับการจัดรูปแบบอย่างเจตนา:
- สไตล์ข้อความเริ่มต้นของงานนำเสนอใช้ฟอนต์ Calibri ขนาด 28 pt
- สไตล์ข้อความระดับแรกของ text frame ใช้ข้อความสีน้ำเงินและตัวเอียง
- paragraph ตั้งค่าให้ข้อความเป็นตัวหนาอย่างชัดเจน
- portion ที่สอง “explicit red 18pt” ตั้งค่าสีแดงและขนาด 18 pt อย่างชัดเจน
ตัวอย่างเต็มต่อไปนี้ส่งออก paragraph เดียวกันสามครั้ง ส่งผ่าน TextToHtmlConversionOptions ไปยังเมธอดส่งออกและเขียนผลลัพธ์แต่ละไฟล์เป็นไฟล์ HTML UTF‑8 แยกกัน:
using System.IO;
using System.Text;
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation("text-inheritance.pptx");
var textShape = (IAutoShape)presentation.Slides[0].Shapes[0];
var paragraphs = textShape.TextFrame.Paragraphs;
var modes = new[] { TextInheritanceLimit.All, TextInheritanceLimit.TextBox, TextInheritanceLimit.ParagraphOnly };
foreach (var mode in modes)
{
var options = new TextToHtmlConversionOptions { TextInheritanceLimit = mode };
var html = paragraphs.ExportToHtml(0, paragraphs.Count, options);
File.WriteAllText($"{mode}.html", html, Encoding.UTF8);
}
HTML ที่สร้างขึ้นมี CSS ต่อไปนี้สำหรับ portion แรก “Inherited text; ” ตารางแสดงการประกาศที่เกี่ยวข้อง; ค่าที่สร้างอื่น ๆ ถูกละเว้นเพื่อความกระชับ
| Output | Relevant CSS for the first portion | Appearance |
|---|---|---|
All.html |
font-family:'Calibri'; font-size:28pt; color:#0000ff; font-style:italic; font-weight:bold |
ข้อความสีฟ้า ตัวหนา ตัวเอียง ขนาด 28 pt ในฟอนต์ Calibri หากฟอนต์นั้นมีในระบบ |
TextBox.html |
color:#0000ff; font-style:italic; font-weight:bold |
ข้อความสีฟ้า ตัวหนา ตัวเอียง; ฟอนต์และขนาดจะถูกกำหนดโดยหน้าเว็บหรือเบราว์เซอร์ |
ParagraphOnly.html |
font-weight:bold |
ข้อความตัวหนา; ฟอนต์, ขนาด, สี, และสไตล์อักษรเอียงจะต้องมาจากหน้าเว็บหรือเบราว์เซอร์ |
portion ที่สองยังคงมี color:#ff0000, font-size:18pt, และ font-weight:bold ในทุก ๆ ผลลัพธ์ และยังคงเป็นตัวเอียงใน All และ TextBox แต่ไม่ใช่ใน ParagraphOnly การจำกัดการสืบทอดจึงไม่ลบการจัดรูปแบบที่กำหนดโดยตรงให้กับ paragraph หรือ portion
เปิดไฟล์ในเบราว์เซอร์เดียวกันเพื่อเปรียบเทียบ ในสองโหมดที่จำกัด CSS ที่ถูกละเว้นจะถูกแทนที่ด้วยสไตล์ของหน้าเป้าหมายหรือค่าเริ่มต้นของเบราว์เซอร์ ดังนั้นลักษณะที่ปรากฏจะขึ้นกับที่ใช้ HTML
บันทึก Paragraph ที่ส่งออกเป็น HTML UTF‑8
ITextToHtmlConversionOptions.EncodingName กำหนด charset ที่ประกาศในสตริง HTML ที่คืนค่าจาก ParagraphCollection.ExportToHtml เมื่อคุณบันทึกสตริงนั้น การเข้ารหัสที่ส่งให้ File.WriteAllText จะกำหนดไบต์จริง ใช้การเข้ารหัสเดียวกันสำหรับทั้งสองขั้นตอนเพื่อให้เบราว์เซอร์สามารถถอดรหัสข้อความหลายภาษาได้อย่างถูกต้อง
using System.IO;
using System.Text;
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 0, 0, 400, 100);
shape.TextFrame.Text = "Café | Ωω | 日本語 | 😀";
var options = new TextToHtmlConversionOptions
{
EncodingName = "utf-8",
AddClipboardFragmentHeader = false
};
var paragraphs = shape.TextFrame.Paragraphs;
var html = paragraphs.ExportToHtml(0, paragraphs.Count, options);
File.WriteAllText("paragraphs.html", html, new UTF8Encoding(false));
ไฟล์ที่บันทึกประกาศ charset=utf-8 และมีไบต์ UTF‑8 เปิด paragraphs.html ในเบราว์เซอร์จะแสดงข้อความ “Café | Ωω | 日本語 | 😀”
สร้าง HTML Clipboard Fragment
ตั้งค่า AddClipboardFragmentHeader เป็น false เพื่อสร้าง HTML ธรรมดา หรือ true เพื่อเพิ่มส่วนหัวของคลิปบอร์ด ทั้งสองการเรียกคืนสตริง ส่วนหัวให้ค่าออฟเซ็ตไบต์ UTF‑8 ของ HTML รอบ ๆ (StartHTML และ EndHTML) และส่วนเนื้อหาที่จะวาง (StartFragment และ EndFragment)
ตัวอย่างนี้ส่งออกสอง paragraph ที่จัดรูปแบบเป็น HTML ธรรมดา (html) และเป็น HTML Clipboard Fragment (clipboardHtml) การเขียน fragment ไปยังคลิปบอร์ดของระบบเป็นขั้นตอนแอปพลิเคชันแยกต่างหาก API ของคลิปบอร์ดที่รับไบต์ HTML แบบดิบต้องเป็น UTF‑8 โดยไม่มี BOM
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 0, 0, 400, 100);
var paragraphs = shape.TextFrame.Paragraphs;
paragraphs.Clear();
paragraphs.AddFromHtml("<p>Status: <b>Ready</b></p><p><i>Next steps</i></p>");
var options = new TextToHtmlConversionOptions { AddClipboardFragmentHeader = false };
var html = paragraphs.ExportToHtml(0, paragraphs.Count, options);
options.AddClipboardFragmentHeader = true;
var clipboardHtml = paragraphs.ExportToHtml(0, paragraphs.Count, options);
เรนเดอร์ Paragraph เป็นภาพ
IParagraph.GetImage เรนเดอร์ paragraph เดี่ยวโดยตรงและคืนค่าเป็น IImage บันทึกผลลัพธ์ไปยังไฟล์หรือสตรีมด้วย IImage.Save ไม่จำเป็นต้องเรนเดอร์รูปร่างที่บรรจุหรือครอบครองบิตแมปด้วยตนเอง
IParagraph.GetImage อาจคืนค่า null หากไม่พบ paragraph ในคอลเลกชันแม่, ไม่มีขอบเขตการเรนเดอร์ที่ถูกต้อง, หรือไม่สามารถเรนเดอร์ได้ ตรวจสอบผลลัพธ์ก่อนบันทึกและทำลายอ็อบเจกต์ภาพที่คืนค่าเมื่อใช้เสร็จ
เรนเดอร์ Paragraph ที่สเกลเริ่มต้น
ตัวอย่างใช้งานนำเสนอที่มีสไลด์หนึ่งซึ่งรูปแรกเป็น text box ที่มีสาม paragraph

ตัวอย่างต่อไปนี้เลือก paragraph ที่สอง (ดัชนี 1) แล้วเรียก IParagraph.GetImage โดยไม่ระบุตัวแปรสเกล ตรวจสอบว่ารูปร่างมี paragraph ที่ต้องการและการเรนเดอร์ได้ภาพก่อนเรียก IImage.Save เพื่อบันทึกเป็น PNG การประกาศ using จะทำลายภาพหลังการใช้
using System;
using Aspose.Slides;
using var presentation = new Presentation("sample.pptx");
var shape = presentation.Slides[0].Shapes[0];
if (shape is IAutoShape textShape &&
textShape.TextFrame != null &&
textShape.TextFrame.Paragraphs.Count > 1)
{
var paragraph = textShape.TextFrame.Paragraphs[1];
using var paragraphImage = paragraph.GetImage();
if (paragraphImage != null)
{
paragraphImage.Save("paragraph.png", ImageFormat.Png);
}
else
{
Console.WriteLine("The paragraph could not be rendered.");
}
}
else
{
Console.WriteLine("The expected text shape or paragraph was not found.");
}
ผลลัพธ์:

เรนเดอร์ Paragraph ในเซลล์ตารางพร้อมสเกล
ใช้ overload ของ IParagraph.GetImage ที่รับพารามิเตอร์ float scaleX และ float scaleY เพื่อตั้งค่าตัวคูณสเกลแนวนอนและแนวตั้ง ตัวอย่างต่อไปนี้สร้างตาราง, เรนเดอร์ paragraph ในเซลล์แรกโดยขยายกว้างและสูงเป็นสองเท่าจากขนาดเริ่มต้น, แล้วบันทึกผลเป็นภาพ PNG
using System;
using Aspose.Slides;
var scaleX = 2f;
var scaleY = 2f;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var table = slide.Shapes.AddTable(50, 50, new[] { 300d }, new[] { 80d });
var paragraph = table[0, 0].TextFrame.Paragraphs[0];
paragraph.Text = "Text in a table cell";
using var paragraphImage = paragraph.GetImage(scaleX, scaleY);
if (paragraphImage != null)
{
paragraphImage.Save("table_paragraph.png", ImageFormat.Png);
}
else
{
Console.WriteLine("The paragraph could not be rendered.");
}
ตัวคูณสเกล 1 รักษาขนาดพิกเซลเริ่มต้นของแกนนั้น ตัวอย่างเช่น 2 สำหรับทั้งสองแกนจะให้ภาพที่กว้างและสูงประมาณสองเท่าของขนาดเริ่มต้น ทำให้จำนวนพิกเซลเพิ่มเป็นสี่เท่า ค่าใหญ่กว่าจะให้ข้อความคมชัดขึ้นสำหรับการซูมหรือเอาต์พุตความละเอียดสูง แต่ก็เพิ่มการใช้หน่วยความจำและขนาดไฟล์ ค่าใต 1 จะให้ภาพเล็กลงและรายละเอียดน้อยลง ใช้ค่าเท่ากันเพื่รักษาอัตราส่วนของ paragraph; ค่าที่แตกต่างกันระหว่างแกนแนวนอนและแนวตั้งจะทำให้ภาพยืดตามแกนนั้น
การเรนเดอร์รูปร่างทั้งหมดด้วย IShape.GetImage ยังมีประโยชน์เมื่อผลลัพธ์ต้องรวมการเติมสี, เส้นขอบ, หรือบริบทภาพอื่นของรูปร่าง สำหรับภาพที่มีเพียง paragraph ให้ใช้ IParagraph.GetImage
คำถามที่พบบ่อย
ฉันสามารถปิดการห่อบรรทัดใน text frame ได้ทั้งหมดหรือไม่?
ได้. ตั้งค่า ITextFrameFormat.WrapText เพื่อปิดการห่อบรรทัด ทำให้บรรทัดไม่แตกที่ขอบของ text frame
ทำอย่างไรจึงจะได้ขอบเขตบนสไลด์ที่แม่นยำของ paragraph ใด ๆ?
ใช้ IParagraph.GetRect เพื่อรับสี่เหลี่ยมขอบของ paragraph ส่วน IPortion.GetRect ให้ขอบของ portion แยกแต่ละส่วน
การจัดแนวของ paragraph (ซ้าย, ขวา, กลาง หรือจัดเต็ม) ถูกควบคุมที่ไหน?
IParagraphFormat.Alignment เป็นการตั้งค่าระดับ paragraph และใช้กับทั้ง paragraph ไม่ว่า portion แต่ละส่วนจะมีการจัดรูปแบบอย่างไร
เพื่อจัดแนวส่วนของฟอนต์ที่มีขนาดต่างกันในแต่ละบรรทัด ดูที่ Align Fonts Within a Line
ฉันสามารถตั้งค่าภาษา proofing ให้กับส่วนของ paragraph ได้หรือไม่?
ได้. ตั้งค่า IBasePortionFormat.LanguageId สำหรับ portion แยกแต่ละส่วน เพื่อให้ paragraph หนึ่งสามารถมีข้อความหลายภาษาได้