การทำงานกับคุณสมบัติ Markdown
หัวข้อนี้อธิบายวิธีการใช้คุณลักษณะ Markdown โดยใช้ Aspose.Words Markdown เป็นวิธีง่ายๆ ในการจัดรูปแบบข้อความธรรมดาที่สามารถแปลงเป็น HTML ได้อย่างง่ายดาย Aspose.Words รองรับคุณสมบัติ Markdown ต่อไปนี้:
- หัวเรื่อง
- บล็อกโควต
- กฎแนวนอน
- เน้นตัวหนา
- เน้นตัวเอียง
การใช้งานฟีเจอร์ Markdown ส่วนใหญ่เป็นไปตามข้อกำหนด CommonMark
ใน Aspose.Words API และฟีเจอร์ทั้งหมดจะแสดงเป็นรูปแบบที่สอดคล้องกันหรือการจัดรูปแบบโดยตรง ซึ่งหมายความว่า
- ตัวหนาและตัวเอียงจะแสดงเป็น
Font.Bold
และFont.Italic
- หัวเรื่องเป็นย่อหน้าที่มีหัวเรื่อง 1 - หัวเรื่อง 6 รูปแบบ
- เครื่องหมายคำพูดคือย่อหน้าที่มี “เครื่องหมายคำพูด” อยู่ในชื่อรูปแบบ -HorizontalRule คือย่อหน้าที่มีรูปร่างของ HorizionalRule
เอกสาร Markdown พร้อมเน้น
ส่วนนี้จะแสดงวิธีสร้างเอกสาร markdown โดยเน้นตามที่ระบุด้านล่าง
Markdown treats asterisks (*) and underscores (_) as indicators of emphasis.
You can write **bold** or *italic* text.
You can also write ***BoldItalic*** text.
ข้อมูลโค้ดต่อไปนี้สามารถใช้เพื่อสร้างเอกสาร markdown ที่ระบุข้างต้น
// 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); | |
builder.Writeln("Markdown treats asterisks (*) and underscores (_) as indicators of emphasis."); | |
builder.Write("You can write "); | |
builder.Font.Bold = true; | |
builder.Write("bold"); | |
builder.Font.Bold = false; | |
builder.Write(" or "); | |
builder.Font.Italic = true; | |
builder.Write("italic"); | |
builder.Font.Italic = false; | |
builder.Writeln(" text. "); | |
builder.Write("You can also write "); | |
builder.Font.Bold = true; | |
builder.Font.Italic = true; | |
builder.Write("BoldItalic"); | |
builder.Font.Bold = false; | |
builder.Font.Italic = false; | |
builder.Write("text."); | |
builder.Document.Save("EmphasesExample.md"); |
เอกสาร Markdown พร้อมส่วนหัว
ส่วนนี้จะแสดงวิธีสร้างเอกสาร markdown โดยมีหัวข้อต่างๆ ดังต่อไปนี้:
The following produces headings:
# Heading1
## Heading2
### Heading3
#### Heading4
##### Heading5
###### Heading6
# **Bold Heading1**
ข้อมูลโค้ดต่อไปนี้สามารถใช้เพื่อสร้างเอกสาร markdown ที่ระบุข้างต้น
// 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); | |
// By default Heading styles in Word may have bold and italic formatting. | |
// If we do not want text to be emphasized, set these properties explicitly to false. | |
builder.Font.Bold = false; | |
builder.Font.Italic = false; | |
builder.Writeln("The following produces headings:"); | |
builder.ParagraphFormat.Style = doc.Styles["Heading 1"]; | |
builder.Writeln("Heading1"); | |
builder.ParagraphFormat.Style = doc.Styles["Heading 2"]; | |
builder.Writeln("Heading2"); | |
builder.ParagraphFormat.Style = doc.Styles["Heading 3"]; | |
builder.Writeln("Heading3"); | |
builder.ParagraphFormat.Style = doc.Styles["Heading 4"]; | |
builder.Writeln("Heading4"); | |
builder.ParagraphFormat.Style = doc.Styles["Heading 5"]; | |
builder.Writeln("Heading5"); | |
builder.ParagraphFormat.Style = doc.Styles["Heading 6"]; | |
builder.Writeln("Heading6"); | |
// Note, emphases are also allowed inside Headings: | |
builder.Font.Bold = true; | |
builder.ParagraphFormat.Style = doc.Styles["Heading 1"]; | |
builder.Writeln("Bold Heading1"); | |
doc.Save(dataDir + "HeadingsExample.md"); |
เอกสาร Markdown พร้อมเครื่องหมายคำพูดแบบบล็อก
ส่วนนี้จะแสดงวิธีสร้างเอกสาร markdown ด้วยเครื่องหมายคำพูดแบบบล็อกตามที่ระบุด้านล่าง:
We support blockquotes in Markdown:
>*Lorem*
>*ipsum*
>The quotes can be of any level and can be nested:
>>>Quote level 3
>>>
>>>>Nested quote level 4
>
>*Back to first level*
>### Headings are allowed inside Quotes
>
ข้อมูลโค้ดต่อไปนี้สามารถใช้เพื่อสร้างเอกสาร markdown ที่ระบุข้างต้น
// 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); | |
builder.Writeln("We support blockquotes in Markdown:"); | |
builder.ParagraphFormat.Style = doc.Styles["Quote"]; | |
builder.Writeln("Lorem"); | |
builder.Writeln("ipsum"); | |
builder.ParagraphFormat.Style = doc.Styles["Normal"]; | |
builder.Writeln("The quotes can be of any level and can be nested:"); | |
Style quoteLevel3 = doc.Styles.Add(StyleType.Paragraph, "Quote2"); | |
builder.ParagraphFormat.Style = quoteLevel3; | |
builder.Writeln("Quote level 3"); | |
Style quoteLevel4 = doc.Styles.Add(StyleType.Paragraph, "Quote3"); | |
builder.ParagraphFormat.Style = quoteLevel4; | |
builder.Writeln("Nested quote level 4"); | |
builder.ParagraphFormat.Style = doc.Styles["Quote"]; | |
builder.Writeln(); | |
builder.Writeln("Back to first level"); | |
Style quoteLevel1WithHeading = doc.Styles.Add(StyleType.Paragraph, "Quote Heading 3"); | |
builder.ParagraphFormat.Style = quoteLevel1WithHeading; | |
builder.Write("Headings are allowed inside Quotes"); | |
doc.Save(dataDir + "QuotesExample.md"); |
เอกสาร Markdown พร้อมกฎแนวนอน
ส่วนนี้จะแสดงวิธีสร้างเอกสาร markdown ด้วยกฎแนวนอนตามที่ระบุด้านล่าง:
We support Horizontal rules (Thematic breaks) in Markdown:
-----
ข้อมูลโค้ดต่อไปนี้สามารถใช้เพื่อสร้างเอกสาร markdown ที่ระบุข้างต้น
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// Initialize document. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.Writeln("Insert a horizontal rule shape into the document."); | |
builder.InsertHorizontalRule(); | |
dataDir = dataDir + "DocumentBuilder.InsertHorizontalRule_out.doc"; | |
doc.Save(dataDir); |
การอ่านเอกสาร Markdown
ข้อมูลโค้ดต่อไปนี้จะแสดงวิธีการอ่านเอกสาร markdown
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// This is Markdown document that was produced in example of UC3. | |
Document doc = new Document(dataDir + "QuotesExample.md"); | |
// Let's remove Heading formatting from a Quote in the very last paragraph. | |
Paragraph paragraph = doc.FirstSection.Body.LastParagraph; | |
paragraph.ParagraphFormat.Style = doc.Styles["Quote"]; | |
doc.Save(dataDir + "QuotesModifiedExample.md"); |
ระบุตัวเลือกการบันทึก Markdown
Aspose.Words API มีคลาส MarkdownSaveOptions เพื่อระบุตัวเลือกเพิ่มเติมขณะบันทึกเอกสารเป็นรูปแบบ Markdown
ตัวอย่างโค้ดต่อไปนี้สาธิตวิธีการระบุตัวเลือกการบันทึก Markdown ต่างๆ
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
DocumentBuilder builder = new DocumentBuilder(); | |
builder.Writeln("Some text!"); | |
// specify MarkDownSaveOptions | |
MarkdownSaveOptions saveOptions = (MarkdownSaveOptions)SaveOptions.CreateSaveOptions(SaveFormat.Markdown); | |
builder.Document.Save(dataDir + "TestDocument.md", saveOptions); |
วิธีจัดแนวเนื้อหาภายในตารางขณะส่งออกไปยัง Markdown
Aspose.Words API ให้การแจงนับ TableContentAlignment ซึ่งกำหนดทิศทางการจัดตำแหน่งเพื่อจัดแนวเนื้อหาในตารางขณะส่งออกไปยังเอกสาร Markdown ตัวอย่างโค้ดต่อไปนี้สาธิตวิธีการจัดแนวเนื้อหาภายในตาราง
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
DocumentBuilder builder = new DocumentBuilder(); | |
// Create a new table with two cells. | |
builder.InsertCell(); | |
builder.ParagraphFormat.Alignment = ParagraphAlignment.Right; | |
builder.Write("Cell1"); | |
builder.InsertCell(); | |
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center; | |
builder.Write("Cell2"); | |
MarkdownSaveOptions saveOptions = new MarkdownSaveOptions(); | |
// Makes all paragraphs inside table to be aligned to Left. | |
saveOptions.TableContentAlignment = TableContentAlignment.Left; | |
builder.Document.Save(dataDir + "left.md", saveOptions); | |
// Makes all paragraphs inside table to be aligned to Right. | |
saveOptions.TableContentAlignment = TableContentAlignment.Right; | |
builder.Document.Save(dataDir + "right.md", saveOptions); | |
// Makes all paragraphs inside table to be aligned to Center. | |
saveOptions.TableContentAlignment = TableContentAlignment.Center; | |
builder.Document.Save(dataDir + "center.md", saveOptions); | |
// Makes all paragraphs inside table to be aligned automatically. | |
// The alignment in this case will be taken from the first paragraph in corresponding table column. | |
saveOptions.TableContentAlignment = TableContentAlignment.Auto; | |
builder.Document.Save(dataDir + "auto.md", saveOptions); |