Markdown to Word Conversion

26 Apr 20246 minutes to read

Markdown is a lightweight markup language that adds formatting elements to plain text documents. The Java Word library supports the conversion of Markdown to Word document and vice versa, which mostly follows the CommonMark specification and GitHub-flavored syntax.

Convert Markdown to Word document

Convert an existing markdown file to a Word document (DOCX and RTF) using the Java Word library.

The following code example shows how to convert Markdown to Word document.

//Open an existing Markdown file.
WordDocument document = new WordDocument("Input.md", FormatType.Markdown);
//Save as a Word document.
document.save("MarkdownToWord.docx", FormatType.Docx);
//Close the document.
document.close();

TIPS

You can also save the markdown file as HTML.

Customize image data

The Java Word library provides a ImageNodeVisited event, which customizes image data while importing a Markdown file. Implement the logic to customize the image data by using this ImageNodeVisited event.

The following code example shows how to load image data based on the image source path when importing the Markdown files.

//Create a Word document instance.
WordDocument document = new WordDocument();
//Customize the image while importing Markdown using event.
document.getMdImportSettings().ImageNodeVisited.add("mdImportSettings_ImageNodeVisited", new MdImageNodeVisitedEventHandler() 
{ListSupport<MdImageNodeVisitedEventHandler> delegateList = new ListSupport<MdImageNodeVisitedEventHandler>(MdImageNodeVisitedEventHandler.class);
// Represents event handling for MdImageNodeVisitedEventHandlerCollection.
public void invoke(Object sender, MdImageNodeVisitedEventArgs args) throws Exception
{
    mdImportSettings_ImageNodeVisited(sender, args);
}
// Represents the method that handles ImageNodeVisited event.
public void dynamicInvoke(Object... args) throws Exception
{
    mdImportSettings_ImageNodeVisited((Object) args[0], (MdImageNodeVisitedEventArgs) args[1]);
}
// Represents the method that handles ImageNodeVisited event to add collection item.
public void add(MdImageNodeVisitedEventHandler delegate) throws Exception
{
    if (delegate != null)
        delegateList.add(delegate);
}
// Represents the method that handles ImageNodeVisited event to remove collection item.
public void remove(MdImageNodeVisitedEventHandler delegate) throws Exception
{
    if (delegate != null)
        delegateList.remove(delegate);
}
});
//Open the Markdown file.
document.Open("Input.md");
//Save as a Word document.
document.Save("Sample.docx");

The following code examples show the event handler to customize the image based on the source path.

private static void mdImportSettings_ImageNodeVisited(Object sender,MdImageNodeVisitedEventArgs args)throws Exception
{
    //Set the image stream based on the image name from the input Markdown.
    if(args.getUri().equals("Image_1.png"))
        args.setImageStream(new FileStreamSupport("Image_1.png",FileMode.Open));
    else
        if(args.getUri().equals("Image_2.png"))
            args.setImageStream(new FileStreamSupport("Image_2.png",FileMode.Open));
}

NOTE

Hook the event handler before opening a Word document as per the above code example.

Supported Markdown Syntax

Element

Syntax

Description

Bold

Sample content for **bold text**.

For bold, add ** to front and back of the text.

Italic

Sample content for *Italic text*.

For Italic, add * to front and back of the text.

Bold and Italics

Sample content for ***bold and Italic text***.

For bold and Italics, add *** to the front and back of the text.

Strikethrough

Sample content for ~~strike through text~~.

For strike through, add ~~ to front and back of the text.

Subscript

<sub>Subscript text</sub>

For subscript, add <sub> to the front and </sub> to the back of the text.

Superscript

<sup>Superscript text</sup>

For superscript, add <sup> to the front and </sup> to the back of the text.

Heading 1

#Heading 1 content

For heading 1, add # to start of the line.

Heading 2

##Heading 2 content

For heading 2, add ## to start of the line.

Heading 3

###Heading 3 content

For heading 3, add ### to start of the line.

Heading 4

####Heading 4 content

For heading 4, add #### to start of the line.

Heading 5

#####Heading 5 content

For heading 5, add ##### to start of the line.

Heading 6

######Heading 6 content

For heading 6, add ###### to start of the line.

Block quotes

>Block quotes text

For block quotes, add>to start of the line.

Code span

`Code span text`

For code span, add ` to front and back of the text.

Indented code block

4 spaces

For indented code block, add 4 spaces at the beginning of line.

Fenced code block

```
Multi line code text
Multi line code text
```

For fenced code block, add ``` in the new line before and after the content.

Ordered List

1. First
2. Second

For ordered list, preceding the text with 1. (number with dot and one space)

Unordered List

- First
- second

For unordered list, preceding the text with – (hyphen and space).

Links

Link text without title text :
[Link text](URL)
Link text with title text :
[Link text](URL , “title text”)

For hyperlink, enclose the link text within the brackets [ ], and then enclose the URL as first parameter and title as second parameter within the parentheses().
Note:The title text is optional.

Table

Table Syntax in Markdown

Create a table using the pipes and underscores as given in the syntax to create 2 x 2 table.

You can also set column alignments using the syntax below, default it is left aligned.

Right alignment:
Right aligned table Syntax in Markdown

Center alignment:
Center aligned table Syntax in Markdown

Horizontal Line

--- (three hyphen characters)

For horizontal line, add --- (three hyphens) in a new line.

Image

![Alternate text](URL path)

For image, enclose an alternative text within the brackets [], and then link of the image source within parentheses ().

If URL path is base64string, then it will be preserved properly in Word document. Otherwise, you can also

set image from stream while opening Markdown file.

Escape Character

\(any syntax)

Escape any markdown syntax by adding \ as prefix to the syntax.
Example:
\**non-bold text**