使用Markdown功能

本主题讨论如何使用Aspose.Words实现Markdown功能。 Markdown是格式化纯文本的简单方法,可以轻松转换为HTML。 Aspose.Words支持以下Markdown功能:

  • 标题
  • 块引用
  • 横向规则
  • 大胆强调
  • 斜体强调

Markdown功能实现大多遵循Aspose.WordsAPI中的CommonMark规范,所有功能都表示为相应的样式或直接格式。 这意味着

  • 粗体和斜体表示为Font.BoldFont.Italic
  • 标题是具有Heading 1-Heading 6样式的段落。
  • 引号是样式名称中带有"Quote"的段落。
  • 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);