การทำงานกับMarkdownคุณสมบัติ

หัวข้อนี้อธิบายวิธีใช้คุณลักษณะMarkdownโดยใช้Aspose.Words Markdownเป็นวิธีง่ายๆในการจัดรูปแบบข้อความธรรมดาที่สามารถแปลงเป็นHTML Aspose.Wordsรองรับคุณลักษณะต่อไปนี้Markdown:

  • หัวข้อ
  • บล็อกคำพูด
  • กฎแนวนอน
  • เน้นตัวหนา
  • เน้นตัวเอียง

การใช้งานคุณลักษณะMarkdownส่วนใหญ่จะเป็นไปตามข้อกำหนดCommonMarkในAspose.WordsAPIและคุณลักษณะทั้งหมดจะแสดงเป็นรูปแบบที่สอดคล้องกันหรือการจัดรูปแบบโดยตรง ซึ่งหมายความว่า

  • ตัวหนาและตัวเอียงจะแสดงเป็นFont.BoldและFont.Italic.
  • ส่วนหัวเป็นย่อหน้าที่มีหัวข้อ1-หัวข้อ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-C
System::SharedPtr<Document> doc = System::MakeObject<Document>();
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);
builder->Writeln(u"Markdown treats asterisks (*) and underscores (_) as indicators of emphasis.");
builder->Write(u"You can write ");
builder->get_Font()->set_Bold(true);
builder->Write(u"bold");
builder->get_Font()->set_Bold(false);
builder->Write(u" or ");
builder->get_Font()->set_Italic(true);
builder->Write(u"italic");
builder->get_Font()->set_Italic(false);
builder->Writeln(u" text. ");
builder->Write(u"You can also write ");
builder->get_Font()->set_Bold(true);
builder->get_Font()->set_Italic(true);
builder->Write(u"BoldItalic");
builder->get_Font()->set_Bold(false);
builder->get_Font()->set_Italic(false);
builder->Write(u"text.");
System::String outputPath = outputDataDir + u"WorkingWithMarkdownFeatures.MarkdownDocumentWithEmphases.md";
builder->get_Document()->Save(outputPath);

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-C
System::SharedPtr<Document> doc = System::MakeObject<Document>();
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<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->get_Font()->set_Bold(false);
builder->get_Font()->set_Italic(false);
builder->Writeln(u"The following produces headings:");
builder->get_ParagraphFormat()->set_Style(doc->get_Styles()->idx_get(u"Heading 1"));
builder->Writeln(u"Heading1");
builder->get_ParagraphFormat()->set_Style(doc->get_Styles()->idx_get(u"Heading 2"));
builder->Writeln(u"Heading2");
builder->get_ParagraphFormat()->set_Style(doc->get_Styles()->idx_get(u"Heading 3"));
builder->Writeln(u"Heading3");
builder->get_ParagraphFormat()->set_Style(doc->get_Styles()->idx_get(u"Heading 4"));
builder->Writeln(u"Heading4");
builder->get_ParagraphFormat()->set_Style(doc->get_Styles()->idx_get(u"Heading 5"));
builder->Writeln(u"Heading5");
builder->get_ParagraphFormat()->set_Style(doc->get_Styles()->idx_get(u"Heading 6"));
builder->Writeln(u"Heading6");
// Note, emphases are also allowed inside Headings:
builder->get_Font()->set_Bold(true);
builder->get_ParagraphFormat()->set_Style(doc->get_Styles()->idx_get(u"Heading 1"));
builder->Writeln(u"Bold Heading1");
System::String outputPath = outputDataDir + u"WorkingWithMarkdownFeatures.MarkdownDocumentWithHeadings.md";
doc->Save(outputPath);

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-C
System::SharedPtr<Document> doc = System::MakeObject<Document>();
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);
builder->Writeln(u"We support blockquotes in Markdown:");
builder->get_ParagraphFormat()->set_Style(doc->get_Styles()->idx_get(u"Quote"));
builder->Writeln(u"Lorem");
builder->Writeln(u"ipsum");
builder->get_ParagraphFormat()->set_Style(doc->get_Styles()->idx_get(u"Normal"));
builder->Writeln(u"The quotes can be of any level and can be nested:");
System::SharedPtr<Style> quoteLevel3 = doc->get_Styles()->Add(Aspose::Words::StyleType::Paragraph, u"Quote2");
builder->get_ParagraphFormat()->set_Style(quoteLevel3);
builder->Writeln(u"Quote level 3");
System::SharedPtr<Style> quoteLevel4 = doc->get_Styles()->Add(Aspose::Words::StyleType::Paragraph, u"Quote3");
builder->get_ParagraphFormat()->set_Style(quoteLevel4);
builder->Writeln(u"Nested quote level 4");
builder->get_ParagraphFormat()->set_Style(doc->get_Styles()->idx_get(u"Quote"));
builder->Writeln();
builder->Writeln(u"Back to first level");
System::SharedPtr<Style> quoteLevel1WithHeading = doc->get_Styles()->Add(Aspose::Words::StyleType::Paragraph, u"Quote Heading 3");
builder->get_ParagraphFormat()->set_Style(quoteLevel1WithHeading);
builder->Write(u"Headings are allowed inside Quotes");
System::String outputPath = outputDataDir + u"WorkingWithMarkdownFeatures.MarkdownDocumentWithBlockQuotes.md";
doc->Save(outputPath);

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-C
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(System::MakeObject<Document>());
builder->Writeln(u"We support Horizontal rules (Thematic breaks) in Markdown:");
builder->InsertHorizontalRule();
System::String outputPath = outputDataDir + u"WorkingWithMarkdownFeatures.MarkdownDocumentWithHorizontalRule.md";
builder->get_Document()->Save(outputPath);

การอ่านเอกสารMarkdown

ข้อมูลโค้ดต่อไปนี้จะแสดงวิธีการอ่านเอกสารmarkdown.

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// This is Markdown document that was produced in example of UC3.
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"QuotesExample.md");
// Let's remove Heading formatting from a Quote in the very last paragraph.
System::SharedPtr<Paragraph> paragraph = doc->get_FirstSection()->get_Body()->get_LastParagraph();
paragraph->get_ParagraphFormat()->set_Style(doc->get_Styles()->idx_get(u"Quote"));
System::String outputPath = outputDataDir + u"WorkingWithMarkdownFeatures.ReadMarkdownDocument.md";
doc->Save(outputPath);

ระบุตัวเลือกMarkdownบันทึก

Aspose.WordsAPIให้MarkdownSaveOptionsคลาสที่จะระบุตัวเลือกเพิ่มเติมในขณะที่บันทึกเอกสารลงในรูปแบบMarkdown.

ตัวอย่างรหัสต่อไปนี้แสดงให้เห็นถึงวิธีการระบุตัวเลือกการบันทึกต่างๆMarkdown.

auto builder = System::MakeObject<DocumentBuilder>();
builder->Writeln(u"Some text!");
// specify MarkDownSaveOptions
auto saveOptions = SaveOptions::CreateSaveOptions(SaveFormat::Markdown);
builder->get_Document()->Save(outputDataDir + u"TestDocument.md", saveOptions);

วิธีการจัดเนื้อหาภายในตารางขณะส่งออกเป็นMarkdown

Aspose.WordsAPIให้TableContentAlignmentการแจงนับซึ่งกำหนดทิศทางการจัดตำแหน่งเพื่อจัดตำแหน่งเนื้อหาในตารางในขณะที่ส่งออกไปยังเอกสารMarkdown ตัวอย่างรหัสต่อไปนี้แสดงให้เห็นถึงวิธีการจัดตำแหน่งเนื้อหาภายในตาราง.

auto builder = System::MakeObject<DocumentBuilder>();
// Create a new table with two cells.
builder->InsertCell();
builder->get_ParagraphFormat()->set_Alignment(ParagraphAlignment::Right);
builder->Write(u"Cell1");
builder->InsertCell();
builder->get_ParagraphFormat()->set_Alignment(ParagraphAlignment::Center);
builder->Write(u"Cell2");
auto saveOptions = System::MakeObject<MarkdownSaveOptions>();
// Makes all paragraphs inside table to be aligned to Left.
saveOptions->set_TableContentAlignment(TableContentAlignment::Left);
builder->get_Document()->Save(outputDataDir + u"left.md", saveOptions);
// Makes all paragraphs inside table to be aligned to Right.
saveOptions->set_TableContentAlignment(TableContentAlignment::Right);
builder->get_Document()->Save(outputDataDir + u"right.md", saveOptions);
// Makes all paragraphs inside table to be aligned to Center.
saveOptions->set_TableContentAlignment(TableContentAlignment::Center);
builder->get_Document()->Save(outputDataDir + u"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->set_TableContentAlignment(TableContentAlignment::Auto);
builder->get_Document()->Save(outputDataDir + u"auto.md", saveOptions);