Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Create Rich Word Document in Java
// Create a Document object
Document doc = new Document();
// Create DocumentBuiler
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert Image
builder.insertImage("aspose-words.png");
// Save the document
doc.save("Rich Word Document.docx");
// Create a Document object
Document doc = new Document();
doc.getLists().add(ListTemplate.BULLET_CIRCLE);
List list = doc.getLists().get(0);
// Set true to specify that the list has to be restarted at each section.
list.isRestartAtEachSection(true);
DocumentBuilder builder = new DocumentBuilder(doc);
builder.getListFormat().setList(list);
for (int i = 1; i < 45; i++) {
builder.writeln(String.format("List Item " + i));
// Insert section break.
if (i == 15)
builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
}
builder.getListFormat().removeNumbers();
// Save the document
doc.save("Rich Word Document.docx");
// Create a Document object
Document doc = new Document();
// Create a DocumentBuilder object
DocumentBuilder builder = new DocumentBuilder(doc);
// Create table
Table table = builder.startTable();
// Insert a cell
builder.insertCell();
table.autoFit(AutoFitBehavior.AUTO_FIT_TO_WINDOW);
builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
builder.write("This is Row 1 Cell 1");
builder.insertCell();
builder.write("This is Row 1 Cell 2");
// End row
builder.endRow();
// start a next row and set its properties
builder.getRowFormat().setHeight(100);
builder.getRowFormat().setHeightRule(HeightRule.EXACTLY);
builder.insertCell();
builder.write("This is Row 2 Cell 1");
builder.insertCell();
builder.write("This is Row 2 Cell 2");
builder.endRow();
// End table
builder.endTable();
// Save the document
doc.save("Rich Word Document.docx");
// Create a Document object
Document doc = new Document();
// Create a DocumentBuilder object
DocumentBuilder builder = new DocumentBuilder(doc);
// Specify font formatting
Font font = builder.getFont();
font.setSize(18);
font.setBold(true);
font.setColor(Color.BLACK);
font.setName("Arial");
builder.write("How to Create a Rich Word Document?");
builder.insertBreak(BreakType.LINE_BREAK);
// Start the paragraph
font.setSize(12);
font.setBold(false);
ParagraphFormat paragraphFormat = builder.getParagraphFormat();
paragraphFormat.setFirstLineIndent(12);
paragraphFormat.setKeepTogether(true);
builder.write("This article shows how to create a Word document containing text, images and lists.");
// Save the document
doc.save("Rich Word Document.docx");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.