การทำงานกับย่อหน้า
ย่อหน้าคือชุดอักขระที่รวมกันเป็นบล็อกแบบลอจิคัลและลงท้ายด้วยอักขระพิเศษ - ตัวแบ่งย่อหน้า ใน Aspose.Words ย่อหน้าจะแสดงโดยคลาส Paragraph
แทรกย่อหน้า
หากต้องการแทรกย่อหน้าใหม่ลงในเอกสาร คุณจะต้องแทรกอักขระตัวแบ่งย่อหน้าลงไป DocumentBuilder.Writeln ไม่เพียงแต่แทรกสตริงข้อความลงในเอกสาร แต่ยังเพิ่มตัวแบ่งย่อหน้าอีกด้วย
การจัดรูปแบบแบบอักษรปัจจุบันยังระบุโดยคุณสมบัติ Font และการจัดรูปแบบย่อหน้าปัจจุบันจะถูกกำหนดโดยคุณสมบัติ ParagraphFormat ในส่วนถัดไป เราจะอธิบายรายละเอียดเพิ่มเติมเกี่ยวกับการจัดรูปแบบย่อหน้า
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการแทรกย่อหน้าลงในเอกสาร:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_WorkingWithDocument(); | |
// Initialize document. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// Specify font formatting | |
Font font = builder.Font; | |
font.Size = 16; | |
font.Bold = true; | |
font.Color = System.Drawing.Color.Blue; | |
font.Name = "Arial"; | |
font.Underline = Underline.Dash; | |
// Specify paragraph formatting | |
ParagraphFormat paragraphFormat = builder.ParagraphFormat; | |
paragraphFormat.FirstLineIndent = 8; | |
paragraphFormat.Alignment = ParagraphAlignment.Justify; | |
paragraphFormat.KeepTogether = true; | |
builder.Writeln("A whole paragraph."); | |
dataDir = dataDir + "DocumentBuilderInsertParagraph_out.doc"; | |
doc.Save(dataDir); |
จัดรูปแบบย่อหน้า
การจัดรูปแบบย่อหน้าปัจจุบันแสดงโดยออบเจ็กต์ ParagraphFormat ที่ส่งคืนโดยคุณสมบัติ ParagraphFormat ออบเจ็กต์นี้สรุปคุณสมบัติการจัดรูปแบบย่อหน้าต่างๆ ที่มีอยู่ใน Microsoft Word คุณสามารถรีเซ็ตการจัดรูปแบบของย่อหน้าให้เป็นค่าเริ่มต้นได้อย่างง่ายดาย เช่น สไตล์ปกติ จัดชิดซ้าย ไม่มีการเยื้อง ไม่มีการเว้นวรรค ไม่มีเส้นขอบ ไม่มีการแรเงา ด้วยการเรียก ClearFormatting
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการตั้งค่าการจัดรูปแบบย่อหน้า:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// Set paragraph formatting properties | |
ParagraphFormat paragraphFormat = builder.ParagraphFormat; | |
paragraphFormat.Alignment = ParagraphAlignment.Center; | |
paragraphFormat.LeftIndent = 50; | |
paragraphFormat.RightIndent = 50; | |
paragraphFormat.SpaceAfter = 25; | |
// Output text | |
builder.Writeln("I'm a very nice formatted paragraph. I'm intended to demonstrate how the left and right indents affect word wrapping."); | |
builder.Writeln("I'm another nice formatted paragraph. I'm intended to demonstrate how the space after paragraph looks like."); | |
dataDir = dataDir + "DocumentBuilderSetParagraphFormatting_out.doc"; | |
doc.Save(dataDir); |
ใช้สไตล์ย่อหน้า
ออบเจ็กต์การจัดรูปแบบบางอย่าง เช่น Font หรือ ParagraphFormat รองรับสไตล์ สไตล์ในตัวหรือสไตล์ที่ผู้ใช้กำหนดจะแสดงด้วยออบเจ็กต์ Style ซึ่งมีคุณสมบัติสไตล์ที่เหมาะสม เช่น ชื่อ สไตล์พื้นฐาน แบบอักษร การจัดรูปแบบย่อหน้าสไตล์ และอื่นๆ
นอกจากนี้ ออบเจ็กต์ Style ยังเปิดเผยคุณสมบัติ StyleIdentifier ซึ่งส่งคืนตัวระบุสไตล์ที่ไม่ขึ้นกับโลแคลซึ่งแสดงโดยค่าการแจงนับ StyleIdentifier ความจริงก็คือชื่อของสไตล์ในตัวใน Microsoft Word ได้รับการแปลเป็นภาษาต่างๆ เมื่อใช้ตัวระบุสไตล์ คุณสามารถค้นหาสไตล์ที่ถูกต้องโดยไม่คำนึงถึงภาษาของเอกสาร ค่าการแจกแจงสอดคล้องกับสไตล์ Microsoft Word ในตัว เช่น Normal, Heading 1, Heading 2 และอื่นๆ สไตล์ที่ผู้ใช้กำหนดทั้งหมดถูกตั้งค่าเป็นค่าการแจงนับ StyleIdentifier.User
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการใช้สไตล์ย่อหน้า:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// Set paragraph style | |
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Title; | |
builder.Write("Hello"); | |
dataDir = dataDir + "DocumentBuilderApplyParagraphStyle_out.doc"; | |
doc.Save(dataDir); |
แทรกตัวคั่นสไตล์เพื่อใส่สไตล์ย่อหน้าที่แตกต่างกัน
คุณสามารถเพิ่มตัวคั่นรูปแบบที่ส่วนท้ายของย่อหน้าได้โดยใช้แป้นพิมพ์ลัด Ctrl+Alt+Enter ใน Microsoft Word คุณลักษณะนี้อนุญาตให้คุณใช้ลักษณะย่อหน้าสองแบบที่แตกต่างกันในย่อหน้าที่พิมพ์แบบลอจิคัลเดียวกัน หากคุณต้องการให้ข้อความบางส่วนจากจุดเริ่มต้นของหัวข้อใดหัวข้อหนึ่งปรากฏในสารบัญ แต่ไม่ต้องการให้หัวข้อทั้งหมดแสดงในสารบัญ คุณสามารถใช้ฟังก์ชันนี้ได้
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการแทรกตัวคั่นสไตล์เพื่อรองรับสไตล์ย่อหน้าที่แตกต่างกัน:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Style paraStyle = builder.Document.Styles.Add(StyleType.Paragraph, "MyParaStyle"); | |
paraStyle.Font.Bold = false; | |
paraStyle.Font.Size = 8; | |
paraStyle.Font.Name = "Arial"; | |
// Append text with "Heading 1" style. | |
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1; | |
builder.Write("Heading 1"); | |
builder.InsertStyleSeparator(); | |
// Append text with another style. | |
builder.ParagraphFormat.StyleName = paraStyle.Name; | |
builder.Write("This is text with some other formatting "); | |
dataDir = dataDir + "InsertStyleSeparator_out.doc"; | |
// Save the document to disk. | |
doc.Save(dataDir); |
ระบุตัวคั่นลักษณะย่อหน้า
Aspose.Words เปิดเผยทรัพย์สินสาธารณะของ BreakIsStyleSeparator ในคลาส Paragraph
เพื่อระบุย่อหน้าด้วยตัวคั่นสไตล์ ดังที่แสดงในตัวอย่างด้านล่าง:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_RenderingAndPrinting(); | |
// Initialize document. | |
string fileName = "TestFile.doc"; | |
Document doc = new Document(dataDir + fileName); | |
foreach (Paragraph paragraph in doc.GetChildNodes(NodeType.Paragraph, true)) | |
{ | |
if (paragraph.BreakIsStyleSeparator) | |
{ | |
Console.WriteLine("Separator Found!"); | |
} | |
} |
ใช้เส้นขอบและการแรเงากับย่อหน้า
เส้นขอบใน Aspose.Words แสดงโดยคลาส BorderCollection ซึ่งเป็นคอลเลกชันของออบเจ็กต์ Border ที่เข้าถึงได้โดยดัชนีหรือตามประเภทเส้นขอบ ประเภทของเส้นขอบจะแสดงโดยการแจงนับ BorderType ค่าการแจกแจงบางค่าใช้กับองค์ประกอบเอกสารหลายรายการหรือเพียงองค์ประกอบเดียวเท่านั้น ตัวอย่างเช่น BorderType.Bottom ใช้กับย่อหน้าหรือเซลล์ตาราง ในขณะที่ BorderType.DiagonalDown ระบุเส้นขอบแนวทแยงในเซลล์ตารางเท่านั้น
ทั้งคอลเลกชั่นเส้นขอบและเส้นขอบแต่ละอันแยกกันมีคุณลักษณะที่คล้ายกัน เช่น สี รูปแบบของเส้น ความกว้างของเส้น ระยะห่างจากข้อความ และเงาที่เป็นตัวเลือก แสดงด้วยคุณสมบัติที่มีชื่อเดียวกัน คุณสามารถรับประเภทเส้นขอบที่แตกต่างกันได้โดยการรวมค่าคุณสมบัติ นอกจากนี้ ออบเจ็กต์ BorderCollection และ Border ยังช่วยให้คุณรีเซ็ตค่าเหล่านี้เป็นค่าเริ่มต้นได้โดยการเรียกเมธอด ClearFormatting
Aspose.Words ยังมีคลาส Shading ที่มีแอตทริบิวต์การแรเงาสำหรับองค์ประกอบเอกสาร คุณสามารถตั้งค่าพื้นผิวแรเงาและสีที่ต้องการซึ่งนำไปใช้กับพื้นหลังและพื้นหน้าขององค์ประกอบได้โดยใช้ค่าการแจงนับ TextureIndex TextureIndex ยังช่วยให้คุณใช้รูปแบบต่างๆ กับออบเจ็กต์ Shading ได้ ตัวอย่างเช่น หากต้องการตั้งค่าสีพื้นหลังสำหรับองค์ประกอบเอกสาร ให้ใช้ค่า TextureIndex.TextureSolid และตั้งค่าสีแรเงาพื้นหน้าตามความเหมาะสม
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการใช้เส้นขอบและการแรเงากับย่อหน้า:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// Set paragraph borders | |
BorderCollection borders = builder.ParagraphFormat.Borders; | |
borders.DistanceFromText = 20; | |
borders[BorderType.Left].LineStyle = LineStyle.Double; | |
borders[BorderType.Right].LineStyle = LineStyle.Double; | |
borders[BorderType.Top].LineStyle = LineStyle.Double; | |
borders[BorderType.Bottom].LineStyle = LineStyle.Double; | |
// Set paragraph shading | |
Shading shading = builder.ParagraphFormat.Shading; | |
shading.Texture = TextureIndex.TextureDiagonalCross; | |
shading.BackgroundPatternColor = System.Drawing.Color.LightCoral; | |
shading.ForegroundPatternColor = System.Drawing.Color.LightSalmon; | |
builder.Write("I'm a formatted paragraph with double border and nice shading."); | |
dataDir = dataDir + "DocumentBuilderApplyBordersAndShadingToParagraph_out.doc"; | |
doc.Save(dataDir); |
นับบรรทัดย่อหน้า
หากคุณต้องการนับจำนวนบรรทัดในย่อหน้าสำหรับเอกสาร Word ใดๆ คุณสามารถใช้ตัวอย่างโค้ดต่อไปนี้:
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_WorkingWithDocument(); | |
string fileName = "Properties.doc"; | |
Document document = new Document(dataDir + fileName); | |
var collector = new LayoutCollector(document); | |
var it = new LayoutEnumerator(document); | |
foreach (Paragraph paragraph in document.GetChildNodes(NodeType.Paragraph, true)) | |
{ | |
var paraBreak = collector.GetEntity(paragraph); | |
object stop = null; | |
var prevItem = paragraph.PreviousSibling; | |
if (prevItem != null) | |
{ | |
var prevBreak = collector.GetEntity(prevItem); | |
if (prevItem is Paragraph) | |
{ | |
it.Current = collector.GetEntity(prevItem); // para break | |
it.MoveParent(); // last line | |
stop = it.Current; | |
} | |
else if (prevItem is Table) | |
{ | |
var table = (Table)prevItem; | |
it.Current = collector.GetEntity(table.LastRow.LastCell.LastParagraph); // cell break | |
it.MoveParent(); // cell | |
it.MoveParent(); // row | |
stop = it.Current; | |
} | |
else | |
{ | |
throw new Exception(); | |
} | |
} | |
it.Current = paraBreak; | |
it.MoveParent(); | |
// We move from line to line in a paragraph. | |
// When paragraph spans multiple pages the we will follow across them. | |
var count = 1; | |
while (it.Current != stop) | |
{ | |
if (!it.MovePreviousLogical()) | |
break; | |
count++; | |
} | |
const int MAX_CHARS = 16; | |
var paraText = paragraph.GetText(); | |
if (paraText.Length > MAX_CHARS) | |
paraText = $"{paraText.Substring(0, MAX_CHARS)}..."; | |
Console.WriteLine($"Paragraph '{paraText}' has {count} line(-s)."); | |
} |