Java: Convert Markdown to Word and PDF

Markdown is a popular format among writers and developers for its simplicity and readability, allowing content to be formatted using easy-to-write plain text syntax. However, converting Markdown files to universally accessible formats like Word documents and PDF files is essential for sharing documents with readers, enabling complex formatting, and ensuring capability and consistency across devices and platforms. This article demonstrates how to convert Markdown files to Word and PDF files with the powerful library Spire.Doc for Java, enhancing the versatility and distribution potential of your written content.

Install Spire.Doc for Java

First of all, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc</artifactId>
        <version>12.9.0</version>
    </dependency>
</dependencies>
    

Convert a Markdown File to a Word Document with Java

Spire.Doc for Java provides a simple way to convert Markdown format to Word and PDF document formats by using the Document.loadFromFile(String: fileName, FileFormat.Markdown) method to load the Markdown file and the Document.saveToFile(String: fileName, FileFormat: fileFormat) method to save the file as a Word or PDF document.

It should be noted that since images are stored as links in Markdown files, they need to be further processed after conversion if they are to be retained.

The detailed steps for converting a Markdown file to a Word document are as follows:

  • Create an instance of Document class.
  • Load a Markdown file using Document.loadFromFile(String: fileName, FileFormat.Markdown) method.
  • Save the Markdown file as Word document using Document.saveToFile(String: fileName, FileFormat.Docx) method.
  • Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;

public class MarkdownToWord {
    public static void main(String[] args) {
        // Create an instance of Document
        Document doc = new Document();

        // Load a Markdown file
        doc.loadFromFile("Sample.md", FileFormat.Markdown);

        // Save the Markdown file as Word document
        doc.saveToFile("output/MarkdownToWord.docx", FileFormat.Docx);
        doc.dispose();
    }
}

Java: Convert Markdown to Word and PDF

Convert a Markdown File to a PDF Document with Java

By using the FileFormat.PDF Enum as the format parameter of the Document.saveToFile() method, the Markdown file can be directly converted to a PDF document.

The detailed steps for converting a Markdown file to a PDF document are as follows:

  • Create an instance of Document class.
  • Load a Markdown file using Document.loadFromFile(String: fileName, FileFormat.Markdown) method.
  • Save the Markdown file as Word document using Document.saveToFile(String: fileName, FileFormat.PDF) method.
  • Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;

public class MarkdownToPDF {
    public static void main(String[] args) {
        // Create an instance of the Document class
        Document doc = new Document();

        // Load a Markdown file
        doc.loadFromFile("Sample.md");

        // Save the Markdown file as a PDF file
        doc.saveToFile("output/MarkdownToPDF.pdf", FileFormat.PDF);
        doc.dispose();
    }
}

Java: Convert Markdown to Word and PDF

Customizing Page Settings of the Result Document

Spire.Doc for Java also provides methods under PageSetup class to do page setup before the conversion, allowing control over page settings such as page margins and page size of the resulting document.

The following are the steps to customize the page settings of the resulting document:

  • Create an instance of Document class.
  • Load a Markdown file using Document.loadFromFile(String: fileName, FileFormat.Markdown) method.
  • Get the first section using Document.getSections().get() method.
  • Set the page size, page orientation, and page margins using methods under PageSetup class.
  • Save the Markdown file as Word document using Document.saveToFile(String: fileName, FileFormat.PDF) method.
  • Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.PageSetup;
import com.spire.doc.Section;
import com.spire.doc.documents.MarginsF;
import com.spire.doc.documents.PageOrientation;
import com.spire.doc.documents.PageSize;

public class PageSettingMarkdown {
    public static void main(String[] args) {
        // Create an instance of the Document class
        Document doc = new Document();

        // Load a Markdown file
        doc.loadFromFile("Sample.md");

        // Get the first section
        Section section = doc.getSections().get(0);

        // Set the page size, orientation, and margins
        PageSetup pageSetup = section.getPageSetup();
        pageSetup.setPageSize(PageSize.Letter);
        pageSetup.setOrientation(PageOrientation.Landscape);
        pageSetup.setMargins(new MarginsF(100, 100, 100, 100));

        // Save the Markdown file as a PDF file
        doc.saveToFile("output/MarkdownToPDF.pdf", FileFormat.PDF);
        doc.dispose();
    }
}

Java: Convert Markdown to Word and PDF

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.