Quick Start
Overview
This page creates a Word document in code, writes a heading and a paragraph into it, saves it as a DOCX file, and then converts an existing document to Markdown. It uses two classes: Document, which represents the whole document, and DocumentBuilder, which writes content into it.
Prerequisites
| Requirement | Detail |
|---|---|
| Java | 8 or later |
| Dependency | The package from Installation |
| Input file | A document named Report.docx, used by the last two lines of the example |
Step 1: Create the Document
Create an empty Document and wrap it in a DocumentBuilder. The builder keeps a cursor in the document, and writeln inserts text followed by a paragraph break at that cursor. All classes are in the com.aspose.words package.
Step 2: Write a Heading and a Paragraph
Set the paragraph style with StyleIdentifier, then write the text. The example below does all of it and saves the result with Document.save. Its last two statements open the existing document Report.docx and save it with a Markdown file name:
import com.aspose.words.*;
// Create a document from scratch.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_1);
builder.writeln("Hello from Aspose.Words FOSS!");
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.BODY_TEXT);
builder.writeln("This document was created entirely in code, no Word installed.");
doc.save("Hello.docx");
// Convert an existing document to Markdown.
Document report = new Document("Report.docx");
report.save("Report.md");This example compiled and ran without an error on Java 8, 17 and 21 against the published package. The Document constructors and Document.save declare throws Exception, so call them from a method that declares it or catches it.