java.lang.Object
com.aspose.words.PlainTextDocument
public class PlainTextDocument
To learn more, visit the Working with Text Document documentation article. Example:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Hello world!");
doc.save(getArtifactsDir() + "PlainTextDocument.Load.docx");
PlainTextDocument plaintext = new PlainTextDocument(getArtifactsDir() + "PlainTextDocument.Load.docx");
Assert.assertEquals("Hello world!", plaintext.getText().trim());
| Constructor Summary |
|---|
PlainTextDocument(java.lang.String fileName)
Creates a plain text document from a file. Automatically detects the file format. |
PlainTextDocument(java.lang.String fileName, LoadOptions loadOptions)
Creates a plain text document from a file. Allows to specify additional options such as an encryption password. |
PlainTextDocument(java.io.InputStream stream)
Creates a plain text document from a stream. Automatically detects the file format. |
PlainTextDocument(java.io.InputStream stream, LoadOptions loadOptions)
Creates a plain text document from a stream. Allows to specify additional options such as an encryption password. |
| Property Getters/Setters Summary | ||
|---|---|---|
BuiltInDocumentProperties | getBuiltInDocumentProperties() | |
|
Gets |
||
CustomDocumentProperties | getCustomDocumentProperties() | |
|
Gets |
||
java.lang.String | getText() | |
| Gets textual content of the document concatenated as a string. | ||
| Constructor Detail |
|---|
public PlainTextDocument(java.lang.String fileName)
throws java.lang.Exception
fileName - Name of the file to extract the text from.Example:
Shows how to load the contents of a Microsoft Word document in plaintext.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Hello world!");
doc.save(getArtifactsDir() + "PlainTextDocument.Load.docx");
PlainTextDocument plaintext = new PlainTextDocument(getArtifactsDir() + "PlainTextDocument.Load.docx");
Assert.assertEquals("Hello world!", plaintext.getText().trim());
public PlainTextDocument(java.lang.String fileName, LoadOptions loadOptions)
throws java.lang.Exception
fileName - Name of the file to extract the text from.loadOptions - Additional options to use when loading a document. Can be null.Example:
Shows how to load the contents of an encrypted Microsoft Word document in plaintext.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Hello world!");
OoxmlSaveOptions saveOptions = new OoxmlSaveOptions();
saveOptions.setPassword("MyPassword");
doc.save(getArtifactsDir() + "PlainTextDocument.LoadEncrypted.docx", saveOptions);
LoadOptions loadOptions = new LoadOptions();
loadOptions.setPassword("MyPassword");
PlainTextDocument plaintext = new PlainTextDocument(getArtifactsDir() + "PlainTextDocument.LoadEncrypted.docx", loadOptions);
Assert.assertEquals("Hello world!", plaintext.getText().trim());
public PlainTextDocument(java.io.InputStream stream)
throws java.lang.Exception
The document must be stored at the beginning of the stream.
stream - The stream where to extract the text from.Example:
Shows how to load the contents of a Microsoft Word document in plaintext using stream.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Hello world!");
doc.save(getArtifactsDir() + "PlainTextDocument.LoadFromStream.docx");
try (FileInputStream stream = new FileInputStream(getArtifactsDir() + "PlainTextDocument.LoadFromStream.docx")) {
PlainTextDocument plaintext = new PlainTextDocument(stream);
Assert.assertEquals("Hello world!", plaintext.getText().trim());
}
public PlainTextDocument(java.io.InputStream stream, LoadOptions loadOptions)
throws java.lang.Exception
The document must be stored at the beginning of the stream.
stream - The stream where to extract the text from.loadOptions - Additional options to use when loading a document. Can be null.Example:
Shows how to load the contents of an encrypted Microsoft Word document in plaintext using stream.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Hello world!");
OoxmlSaveOptions saveOptions = new OoxmlSaveOptions();
saveOptions.setPassword("MyPassword");
doc.save(getArtifactsDir() + "PlainTextDocument.LoadFromStreamWithOptions.docx", saveOptions);
LoadOptions loadOptions = new LoadOptions();
loadOptions.setPassword("MyPassword");
try (FileInputStream stream = new FileInputStream(getArtifactsDir() + "PlainTextDocument.LoadFromStreamWithOptions.docx")) {
PlainTextDocument plaintext = new PlainTextDocument(stream, loadOptions);
Assert.assertEquals("Hello world!", plaintext.getText().trim());
}| Property Getters/Setters Detail |
|---|
getBuiltInDocumentProperties | |
public BuiltInDocumentProperties getBuiltInDocumentProperties() | |
Example:
Shows how to load the contents of a Microsoft Word document in plaintext and then access the original document's built-in properties.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Hello world!");
doc.getBuiltInDocumentProperties().setAuthor("John Doe");
doc.save(getArtifactsDir() + "PlainTextDocument.BuiltInProperties.docx");
PlainTextDocument plaintext = new PlainTextDocument(getArtifactsDir() + "PlainTextDocument.BuiltInProperties.docx");
Assert.assertEquals("Hello world!", plaintext.getText().trim());
Assert.assertEquals("John Doe", plaintext.getBuiltInDocumentProperties().getAuthor());getCustomDocumentProperties | |
public CustomDocumentProperties getCustomDocumentProperties() | |
Example:
Shows how to load the contents of a Microsoft Word document in plaintext and then access the original document's custom properties.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Hello world!");
doc.getCustomDocumentProperties().add("Location of writing", "123 Main St, London, UK");
doc.save(getArtifactsDir() + "PlainTextDocument.CustomDocumentProperties.docx");
PlainTextDocument plaintext = new PlainTextDocument(getArtifactsDir() + "PlainTextDocument.CustomDocumentProperties.docx");
Assert.assertEquals("Hello world!", plaintext.getText().trim());
Assert.assertEquals("123 Main St, London, UK", plaintext.getCustomDocumentProperties().get("Location of writing").getValue());getText | |
public java.lang.String getText() | |
Example:
Shows how to load the contents of a Microsoft Word document in plaintext.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Hello world!");
doc.save(getArtifactsDir() + "PlainTextDocument.Load.docx");
PlainTextDocument plaintext = new PlainTextDocument(getArtifactsDir() + "PlainTextDocument.Load.docx");
Assert.assertEquals("Hello world!", plaintext.getText().trim());