Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
To programmatically read, manipulate, and modify the content and formatting of a document, you need to translate it to the Aspose.Words Document Object Model (DOM).
In contrast to Word documents, Markdown does not conform to the DOM described in the Aspose.Words Document Object Model (DOM) article. However, Aspose.Words provides its own mechanism for translating Markdown documents to DOM and back, so that we can successfully work with their elements such as text formatting, tables, headers, and others.
This article explains how the various markdown features can be translated into Aspose.Words DOM and back to Markdown format.
The main difficulty of this mechanism is not only to translate Markdown to DOM, but also to do the reverse transformation – to save the document back to Markdown format with minimal loss. There are elements, such as multilevel quotes, for which the reverse transformation is not trivial.
Our translation engine allows users not only to work with complex elements in an existing Markdown document, but also to create their own document in Markdown format with the original structure from scratch. To create various elements, you need to use styles with specific names according to certain rules described later in this article. Such styles can be created programmatically.
We use Font formatting for inline blocks. When there is no direct correspondence for a Markdown feature in Aspose.Words DOM, we use a character style with a name that starts from some special words.
For container blocks, we use style inheritance to denote nested Markdown features. In this case, even when there are no nested features, we also use paragraph styles with a name that starts from some special words.
Bulleted and ordered lists are container blocks in Markdown as well. Their nesting is represented in DOM the same way as for all other container blocks using style inheritance. However, additionally, lists in DOM have corresponded number formatting in either list style or paragraph formatting.
We use Font formatting when translating Bold, Italic or Strikethrough inline markdown features.
| Markdown feature | Aspose.Words |
|---|---|
Bold**bold text** |
Font.bold = True |
|
|
Italic*italic text* |
Font.italic = True |
|
|
Strikethrough~Strikethrough text~ |
Font.strike_through = True |
|
We use a character style with a name that starts from the word InlineCode, followed by an optional dot (.) and a number of backticks (`) for the InlineCode feature. If a number of backticks is missed, then one backtick will be used by default.
| Markdown feature | Aspose.Words |
|---|---|
InlineCode**inline code** |
Font.style_name = "InlineCode[.][N]" |
|
|
Autolink<scheme://domain.com><email@domain.com> |
The FieldHyperlink class. |
|
|
Link[link text](url)[link text](<url> "title")[link text](url 'title')[link text](url (title)) |
The FieldHyperlink |
|
|
Image) |
The Shape class. |
|
A document is a sequence of container blocks such as headings, paragraphs, lists, quotes, and others. Container blocks can be divided into 2 classes: Leaf blocks and Complex Containers. Leaf blocks can only contain inline content. Complex containers, in turn, can contain other container blocks, including Leaf blocks.
The table below shows examples of using Markdown Leaf blocks in Aspose.Words:
| Markdown feature | Aspose.Words |
|---|---|
HorizontalRule----- |
This is a simple paragraph with a corresponding HorizontalRule shape: DocumentBuilder.insert_horizontal_rule() |
ATX Heading# H1, ## H2, ### H3… |
ParagraphFormat.style_name = "Heading N", where (1<= N <= 9).This is translated into a built-in style and should be exactly of the specified pattern (no suffixes or prefixes are allowed). Otherwise, it will be just a regular paragraph with a corresponding style. |
Setext Heading=== (if Heading level 1),--- (if Heading level 2) |
ParagraphFormat.style_name = "SetextHeading[some suffix]", based on "Heading N" style.If (N >= 2), then "Heading 2" will be used, otherwise "Heading 1".Any suffix is allowed, but Aspose.Words importer uses numbers “1” and “2” respectively. |
|
|
| Indented Code | ParagraphFormat.style_name = "IndentedCode[some suffix]" |
|
|
Fenced Code |
ParagraphFormat.style_name = "FencedCode[.][info string]"The [.] and [info string] are optional. |
|
The table below shows examples of using Markdown Complex Containers in Aspose.Words:
| Markdown feature | Aspose.Words |
|---|---|
Quote> quote,>> nested quote |
ParagraphFormat.style_name = "Quote[some suffix]"The suffix in style name is optional, but Aspose.Words importer uses the ordered numbers 1, 2, 3, …. in case of nested quotes. The nesting is defined via the inherited styles. |
|
|
BulletedList- Item 1- Item 2 - Item 2a - Item 2b |
Bulleted lists are represented using paragraph numbering: ListFormat.apply_bullet_default() There can be 3 types of bulleted lists. They are only diff in a numbering format of the very first level. These are: '-', '+' or '*' respectively. |
|
|
OrderedList1. Item 12. Item 21) Item 2a2) Item 2b |
Ordered lists are represented using paragraph numbering: ListFormat.apply_number_default() There can be 2 number format markers: '.' and ')'. The default marker is '.'. |
|
Aspose.Words also allows to translate tables into DOM, as shown below:
| Markdown feature | Aspose.Words |
|---|---|
Table a\ | b-\ | -c\ | d |
Table, Row and Cell classes. |
|
Q: How should heading styles be named when translating Markdown headings to the Aspose.Words DOM?
A: Use built‑in heading styles with the exact pattern Heading N where N is the level (1‑9). For Setext headings the importer creates a style named SetextHeading that is based on the corresponding Heading N style. Do not add any extra prefixes or suffixes; otherwise the paragraph will be treated as a normal paragraph.
Q: How can I change the bullet or number format of a Markdown list after it has been imported?
A: After creating the list with builder.list_format.apply_bullet_default() or apply_number_default(), modify the first level’s number_format property. For example, builder.list_format.list.list_levels[0].number_format = "-" changes a bulleted list to use a hyphen, and builder.list_format.list.list_levels[0].number_format = "1)" changes an ordered list to use a closing parenthesis.
Q: What is the recommended way to represent nested blockquotes in the DOM?
A: Create a paragraph style named Quote for the first level. For each deeper level, add a new paragraph style (e.g., Quote1, Quote2) and set its base_style_name to the style of the previous level. The importer will then map nested > symbols to these inherited styles.
Q: How can I adjust the font size of Markdown content that has been imported into a document?
A: Use DocumentBuilder to set the font size before writing the text, e.g., builder.font.size = 14. If the content is already present, retrieve the relevant paragraphs and set paragraph.paragraph_format.font.size = 14 for each paragraph that needs resizing.
Q: How are indented and fenced code blocks represented, and can I customize their appearance?
A: Indented code blocks use a paragraph style IndentedCode[...]; fenced code blocks use FencedCode[.][info]. You can create or modify these styles via doc.styles.add(aw.StyleType.PARAGRAPH, "IndentedCode") or FencedCode.C#, then adjust properties such as paragraph_format.font.name, font.size, or paragraph_format.shading to change the visual appearance of the code blocks.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.