การทำงานกับย่อหน้า

ย่อหน้าคือชุดอักขระที่รวมกันเป็นบล็อกแบบลอจิคัลและลงท้ายด้วยอักขระพิเศษ - ตัวแบ่งย่อหน้า ใน Aspose.Words ย่อหน้าจะแสดงโดยคลาส Paragraph

แทรกย่อหน้า

หากต้องการแทรกย่อหน้าใหม่ลงในเอกสาร คุณจะต้องแทรกอักขระตัวแบ่งย่อหน้าลงไป DocumentBuilder.writeln จะแทรกสตริงข้อความลงในเอกสารด้วย แต่เพิ่มตัวแบ่งย่อหน้าด้วย

การจัดรูปแบบแบบอักษรปัจจุบันยังระบุโดยคุณสมบัติ font และการจัดรูปแบบย่อหน้าปัจจุบันจะถูกกำหนดโดยคุณสมบัติ paragraph_format

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการแทรกย่อหน้าลงในเอกสาร:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
font = builder.font
font.size = 16
font.bold = True
font.color = drawing.Color.blue
font.name = "Arial"
font.underline = aw.Underline.DASH
paragraphFormat = builder.paragraph_format
paragraphFormat.first_line_indent = 8
paragraphFormat.alignment = aw.ParagraphAlignment.JUSTIFY
paragraphFormat.keep_together = True
builder.writeln("A whole paragraph.")
doc.save(docs_base.artifacts_dir + "AddContentUsingDocumentBuilder.insert_paragraph.docx")

จัดรูปแบบย่อหน้า

การจัดรูปแบบย่อหน้าปัจจุบันแสดงโดยออบเจ็กต์ ParagraphFormat ที่ส่งคืนโดยคุณสมบัติ paragraph_format ออบเจ็กต์นี้สรุปคุณสมบัติการจัดรูปแบบย่อหน้าต่างๆ ที่มีอยู่ใน Microsoft Word คุณสามารถรีเซ็ตการจัดรูปแบบย่อหน้าให้เป็นค่าเริ่มต้นเป็นรูปแบบปกติ จัดชิดซ้าย ไม่มีการเยื้อง ไม่มีการเว้นวรรค ไม่มีเส้นขอบ และไม่มีแรเงา โดยการเรียก clear_formatting

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการตั้งค่าการจัดรูปแบบย่อหน้า:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
paragraphFormat = builder.paragraph_format
paragraphFormat.alignment = aw.ParagraphAlignment.CENTER
paragraphFormat.left_indent = 50
paragraphFormat.right_indent = 50
paragraphFormat.space_after = 25
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.")
doc.save(docs_base.artifacts_dir + "DocumentFormatting.paragraph_formatting.docx")

ใช้สไตล์ย่อหน้า

ออบเจ็กต์การจัดรูปแบบบางอย่าง เช่น Font หรือ ParagraphFormat รองรับสไตล์ สไตล์ในตัวหรือสไตล์ที่ผู้ใช้กำหนดจะแสดงด้วยออบเจ็กต์ Style ที่มีคุณสมบัติสไตล์ที่สอดคล้องกัน เช่น ชื่อ สไตล์พื้นฐาน แบบอักษรและการจัดรูปแบบย่อหน้าของสไตล์ และอื่นๆ

นอกจากนี้ ออบเจ็กต์ Style ยังจัดเตรียมคุณสมบัติ Style.style_identifier ที่ส่งคืนตัวระบุสไตล์ที่ไม่ขึ้นกับโลแคลซึ่งแสดงด้วยค่าการแจงนับ 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-Python-via-.NET
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
builder.paragraph_format.style_identifier = aw.StyleIdentifier.TITLE
builder.write("Hello")
doc.save(docs_base.artifacts_dir + "DocumentFormatting.apply_paragraph_style.docx")

แทรกตัวคั่นสไตล์เพื่อใส่สไตล์ย่อหน้าที่แตกต่างกัน

คุณสามารถเพิ่มตัวคั่นสไตล์ที่ส่วนท้ายของย่อหน้าได้โดยใช้ Ctrl + Alt + Enter แป้นพิมพ์ลัดใน MS Word คุณลักษณะนี้อนุญาตให้ใช้ลักษณะย่อหน้าที่แตกต่างกันสองแบบในย่อหน้าเดียวที่พิมพ์แบบลอจิคัล หากคุณต้องการให้ข้อความบางส่วนจากจุดเริ่มต้นของหัวข้อใดหัวข้อหนึ่งปรากฏในสารบัญ แต่ไม่ต้องการให้หัวข้อทั้งหมดในสารบัญ คุณสามารถใช้ฟีเจอร์นี้ได้

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการแทรกตัวคั่นสไตล์เพื่อรองรับสไตล์ย่อหน้าที่แตกต่างกัน:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
paraStyle = builder.document.styles.add(aw.StyleType.PARAGRAPH, "MyParaStyle")
paraStyle.font.bold = False
paraStyle.font.size = 8
paraStyle.font.name = "Arial"
# Append text with "Heading 1" style.
builder.paragraph_format.style_identifier = aw.StyleIdentifier.HEADING1
builder.write("Heading 1")
builder.insert_style_separator()
# Append text with another style.
builder.paragraph_format.style_name = paraStyle.name
builder.write("This is text with some other formatting ")
doc.save(docs_base.artifacts_dir + "WorkingWithStylesAndThemes.insert_style_separator.docx")

ระบุตัวคั่นลักษณะย่อหน้า

Aspose.Words จัดเตรียม break_is_style_separator ทรัพย์สินสาธารณะไว้ในคลาส Paragraph ช่วยให้สามารถระบุย่อหน้าตัวแยกสไตล์ดังที่แสดงในตัวอย่างด้านล่าง:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
doc = aw.Document(docs_base.my_dir + "Document.docx")
for paragraph in doc.get_child_nodes(aw.NodeType.PARAGRAPH, True) :
paragraph = paragraph.as_paragraph()
if (paragraph.break_is_style_separator) :
print("Separator Found!")

ใช้เส้นขอบและการแรเงากับย่อหน้า

เส้นขอบแสดงโดย BorderCollection นี่คือคอลเลกชันของออบเจ็กต์ Border ที่เข้าถึงได้โดยดัชนีหรือประเภทเส้นขอบ ประเภทเส้นขอบแสดงโดยการแจงนับ BorderType ค่าบางค่าของการแจงนับใช้ได้กับองค์ประกอบเอกสารหลายรายการหรือเพียงองค์ประกอบเดียวเท่านั้น ตัวอย่างเช่น BorderType.BOTTOM ใช้ได้กับย่อหน้าหรือเซลล์ตาราง ในขณะที่ BorderType.DIAGONALDOWN ระบุเส้นขอบแนวทแยงในเซลล์ตารางเท่านั้น

ทั้งคอลเลกชั่นเส้นขอบและเส้นขอบแต่ละอันแยกกันมีคุณสมบัติที่คล้ายกัน เช่น สี ลักษณะของเส้น ความกว้างของเส้น ระยะห่างจากข้อความ และเงาเพิ่มเติม แสดงด้วยคุณสมบัติที่มีชื่อเดียวกัน คุณสามารถบรรลุประเภทเส้นขอบที่แตกต่างกันได้โดยการรวมค่าคุณสมบัติ นอกจากนี้ ทั้งออบเจ็กต์ BorderCollection และ Border ยังช่วยให้คุณรีเซ็ตค่าเหล่านี้เป็นค่าเริ่มต้นได้โดยการเรียกเมธอด Border.clear_formatting

Aspose.Words ยังมีคลาส Shading ที่มีแอตทริบิวต์การแรเงาสำหรับองค์ประกอบเอกสาร คุณสามารถตั้งค่าพื้นผิวแรเงาที่ต้องการและสีที่ใช้กับพื้นหลังและพื้นหน้าขององค์ประกอบได้

พื้นผิวแรเงาถูกตั้งค่าด้วยค่าการแจงนับ TextureIndex ที่ช่วยให้สามารถนำรูปแบบต่างๆ ไปใช้กับออบเจ็กต์ Shading ได้ ตัวอย่างเช่น หากต้องการตั้งค่าสีพื้นหลังสำหรับองค์ประกอบเอกสาร ให้ใช้ค่า TextureIndex.TEXTURE_SOLID และตั้งค่าสีแรเงาพื้นหน้าตามความเหมาะสม ตัวอย่างด้านล่างแสดงวิธีการใช้เส้นขอบและการแรเงากับย่อหน้า

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการใช้เส้นขอบและการแรเงากับย่อหน้า:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
borders = builder.paragraph_format.borders
borders.distance_from_text = 20
borders.get_by_border_type(aw.BorderType.LEFT).line_style = aw.LineStyle.DOUBLE
borders.get_by_border_type(aw.BorderType.RIGHT).line_style = aw.LineStyle.DOUBLE
borders.get_by_border_type(aw.BorderType.TOP).line_style = aw.LineStyle.DOUBLE
borders.get_by_border_type(aw.BorderType.BOTTOM).line_style = aw.LineStyle.DOUBLE
shading = builder.paragraph_format.shading
shading.texture = aw.TextureIndex.TEXTURE_DIAGONAL_CROSS
shading.background_pattern_color = drawing.Color.light_coral
shading.foreground_pattern_color = drawing.Color.light_salmon
builder.write("I'm a formatted paragraph with double border and nice shading.")
doc.save(docs_base.artifacts_dir + "DocumentFormatting.apply_borders_and_shading_to_paragraph.doc")