การทำงานกับMarkdownคุณสมบัติ
หัวข้อนี้อธิบายวิธีใช้คุณลักษณะMarkdownโดยใช้Aspose.Words Markdownเป็นวิธีง่ายๆในการจัดรูปแบบข้อความธรรมดาที่สามารถแปลงเป็นHTML Aspose.Wordsรองรับคุณลักษณะต่อไปนี้Markdown:
- หัวข้อ
- บล็อกคำพูด
- กฎแนวนอน
- เน้นตัวหนา
- เน้นตัวเอียง
การใช้งานคุณลักษณะMarkdownส่วนใหญ่จะเป็นไปตามข้อกำหนดCommonMark
ในAspose.WordsAPIและคุณลักษณะทั้งหมดจะแสดงเป็นรูปแบบที่สอดคล้องกันหรือการจัดรูปแบบโดยตรง ซึ่งหมายความว่า
- ตัวหนาและตัวเอียงจะแสดงเป็น
Font.Bold
และFont.Italic
- ส่วนหัวเป็นย่อหน้าที่มีลักษณะHeading 1-Heading 6
- คำพูดเป็นย่อหน้าที่มี"อ้าง"ในชื่อสไตล์
- HorizontalRuleเป็นย่อหน้าที่มีรูปร่าง
HorizontalRule
.
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-Java | |
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.getFont().setBold(true); | |
builder.write("bold"); | |
builder.getFont().setBold(false); | |
builder.write(" or "); | |
builder.getFont().setItalic(true); | |
builder.write("italic"); | |
builder.getFont().setItalic(false); | |
builder.writeln(" text. "); | |
builder.write("You can also write "); | |
builder.getFont().setBold(true); | |
builder.getFont().setItalic(true); | |
builder.write("BoldItalic"); | |
builder.getFont().setBold(false); | |
builder.getFont().setItalic(false); | |
builder.write("text."); | |
builder.getDocument().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-Java | |
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.getFont().setBold(false); | |
builder.getFont().setItalic(false); | |
builder.writeln("The following produces headings:"); | |
builder.getParagraphFormat().setStyle(doc.getStyles().get("Heading 1")); | |
builder.writeln("Heading1"); | |
builder.getParagraphFormat().setStyle(doc.getStyles().get("Heading 2")); | |
builder.writeln("Heading2"); | |
builder.getParagraphFormat().setStyle(doc.getStyles().get("Heading 3")); | |
builder.writeln("Heading3"); | |
builder.getParagraphFormat().setStyle(doc.getStyles().get("Heading 4")); | |
builder.writeln("Heading4"); | |
builder.getParagraphFormat().setStyle(doc.getStyles().get("Heading 5")); | |
builder.writeln("Heading5"); | |
builder.getParagraphFormat().setStyle(doc.getStyles().get("Heading 6")); | |
builder.writeln("Heading6"); | |
// Note, emphases are also allowed inside Headings: | |
builder.getFont().setBold(true); | |
builder.getParagraphFormat().setStyle(doc.getStyles().get("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-Java | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.writeln("We support blockquotes in Markdown:"); | |
builder.getParagraphFormat().setStyle(doc.getStyles().get("Quote")); | |
builder.writeln("Lorem"); | |
builder.writeln("ipsum"); | |
builder.getParagraphFormat().setStyle(doc.getStyles().get("Normal")); | |
builder.writeln("The quotes can be of any level and can be nested:"); | |
Style quoteLevel3 = doc.getStyles().add(StyleType.PARAGRAPH, "Quote2"); | |
builder.getParagraphFormat().setStyle(quoteLevel3); | |
builder.writeln("Quote level 3"); | |
Style quoteLevel4 = doc.getStyles().add(StyleType.PARAGRAPH, "Quote3"); | |
builder.getParagraphFormat().setStyle(quoteLevel4); | |
builder.writeln("Nested quote level 4"); | |
builder.getParagraphFormat().setStyle(doc.getStyles().get("Quote")); | |
builder.writeln(); | |
builder.writeln("Back to first level"); | |
Style quoteLevel1WithHeading = doc.getStyles().add(StyleType.PARAGRAPH, "Quote Heading 3"); | |
builder.getParagraphFormat().setStyle(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-Java | |
DocumentBuilder builder = new DocumentBuilder(new Document()); | |
builder.writeln("We support Horizontal rules (Thematic breaks) in Markdown:"); | |
builder.insertHorizontalRule(); | |
builder.getDocument().save(dataDir + "HorizontalRuleExample.md"); |
การอ่านเอกสารMarkdown
ข้อมูลโค้ดต่อไปนี้จะแสดงวิธีการอ่านเอกสารmarkdown.
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// This is Markdown document that was produced in example of | |
// MarkdownDocumentWithBlockQuotes. | |
Document doc = new Document(dataDir + "QuotesExample.md"); | |
// Let's remove Heading formatting from a Quote in the very last paragraph. | |
Paragraph paragraph = doc.getFirstSection().getBody().getLastParagraph(); | |
paragraph.getParagraphFormat().setStyle(doc.getStyles().get("Quote")); | |
doc.save(dataDir + "QuotesModifiedExample.md"); |
ระบุตัวเลือกMarkdownบันทึก
Aspose.WordsAPIให้MarkdownSaveOptionsคลาสที่จะระบุตัวเลือกเพิ่มเติมในขณะที่บันทึกเอกสารลงในรูปแบบMarkdown.
ตัวอย่างรหัสต่อไปนี้แสดงให้เห็นถึงวิธีการระบุตัวเลือกการบันทึกต่างๆMarkdown.
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
DocumentBuilder builder = new DocumentBuilder(); | |
builder.writeln("Some text!"); | |
// specify MarkDownSaveOptions | |
MarkdownSaveOptions saveOptions = (MarkdownSaveOptions) SaveOptions.createSaveOptions(SaveFormat.MARKDOWN); | |
builder.getDocument().save(dataDir + "TestDocument.md", saveOptions); |
วิธีการจัดเนื้อหาภายในตารางขณะส่งออกเป็นMarkdown
Aspose.WordsAPIให้TableContentAlignmentการแจงนับซึ่งกำหนดทิศทางการจัดตำแหน่งเพื่อจัดตำแหน่งเนื้อหาในตารางในขณะที่ส่งออกไปยังเอกสารMarkdown ตัวอย่างรหัสต่อไปนี้แสดงให้เห็นถึงวิธีการจัดตำแหน่งเนื้อหาภายในตาราง.
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
DocumentBuilder builder = new DocumentBuilder(); | |
// Create a new table with two cells. | |
builder.insertCell(); | |
builder.getParagraphFormat().setAlignment(ParagraphAlignment.RIGHT); | |
builder.write("Cell1"); | |
builder.insertCell(); | |
builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER); | |
builder.write("Cell2"); | |
MarkdownSaveOptions saveOptions = new MarkdownSaveOptions(); | |
// Makes all paragraphs inside table to be aligned to Left. | |
saveOptions.setTableContentAlignment(TableContentAlignment.LEFT); | |
builder.getDocument().save(dataDir + "left.md", saveOptions); | |
// Makes all paragraphs inside table to be aligned to Right. | |
saveOptions.setTableContentAlignment(TableContentAlignment.RIGHT); | |
builder.getDocument().save(dataDir + "right.md", saveOptions); | |
// Makes all paragraphs inside table to be aligned to Center. | |
saveOptions.setTableContentAlignment(TableContentAlignment.CENTER); | |
builder.getDocument().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.setTableContentAlignment(TableContentAlignment.AUTO); | |
builder.getDocument().save(dataDir + "auto.md", saveOptions); |