Working with Text
Overview
DocumentBuilder provides methods to insert text, images and other content, and to specify font, paragraph and section formatting. It keeps a cursor in the document and writes at that position. This page uses it to write a heading and a paragraph, which is the starting point for most generated documents. The example then also opens an existing file, Report.docx, which must exist in the directory the program runs in (otherwise it stops with a FileNotFoundException after Hello.docx has been written), and saves it as Markdown.
Prerequisites
| Requirement | Detail |
|---|---|
| Java | 8 or later |
| Dependency | The package from Installation |
| Import | import com.aspose.words.*; |
Write a Heading and a Paragraph
write inserts a string at the cursor. writeln inserts a string and a paragraph break, so the next text starts a new paragraph. The paragraph style comes from getParagraphFormat().setStyleIdentifier(...), and the style is given as a StyleIdentifier value, which is a locale independent style identifier. The example sets HEADING_1 for the first line and BODY_TEXT for the second. Its last two statements open an 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");Tips and Best Practices
- In the example the style is set right before the line it applies to.
- Use
writeto build a paragraph from several pieces andwritelnwhen the paragraph is complete. - Save with a .docx file name to get a DOCX file. Saving Documents lists the other formats.
Common Issues
| Issue | Cause | Fix |
|---|---|---|
| The compiler reports cannot find symbol for StyleIdentifier. | The com.aspose.words package is not imported. | Add import com.aspose.words.*;. |
| The compiler reports an unreported exception. | save and other methods declare throws Exception. | Declare it on the calling method or catch it. |
FAQ
What is the difference between write and writeln?
write inserts a string into the document at the current insert position. writeln inserts a string and a paragraph break.
How do I make a line a heading?
Set StyleIdentifier.HEADING_1 on the paragraph format before writing the line.
Can I write text without a builder?
This guide covers only DocumentBuilder; no other way of writing text is shown here.
API Reference Summary
| Class / Method | Description |
|---|---|
DocumentBuilder | Provides methods to insert text, images and other content, specify font, paragraph and section formatting. |
DocumentBuilder.write(text) | Inserts a string into the document at the current insert position. |
DocumentBuilder.writeln(text) | Inserts a string and a paragraph break into the document. |
DocumentBuilder.getParagraphFormat() | Returns an object that represents current paragraph formatting properties. |
StyleIdentifier | Locale independent style identifier. |