PlainTextDocument
Inheritance: java.lang.Object
public class PlainTextDocument
Allows to extract plain-text representation of the document’s content.
To learn more, visit the Working with Text Document documentation article.
Examples:
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());
Constructors
Constructor | Description |
---|---|
PlainTextDocument(String fileName) | Creates a plain text document from a file. |
PlainTextDocument(String fileName, LoadOptions loadOptions) | Creates a plain text document from a file. |
PlainTextDocument(InputStream stream) | Initializes a new instance of this class. |
PlainTextDocument(InputStream stream, LoadOptions loadOptions) | Initializes a new instance of this class. |
Methods
Method | Description |
---|---|
getBuiltInDocumentProperties() | Gets getBuiltInDocumentProperties() of the document. |
getCustomDocumentProperties() | Gets getCustomDocumentProperties() of the document. |
getText() | Gets textual content of the document concatenated as a string. |
PlainTextDocument(String fileName)
public PlainTextDocument(String fileName)
Creates a plain text document from a file. Automatically detects the file format.
Parameters:
Parameter | Type | Description |
---|---|---|
fileName | java.lang.String | Name of the file to extract the text from. |
PlainTextDocument(String fileName, LoadOptions loadOptions)
public PlainTextDocument(String fileName, LoadOptions loadOptions)
Creates a plain text document from a file. Allows to specify additional options such as an encryption password.
Parameters:
Parameter | Type | Description |
---|---|---|
fileName | java.lang.String | Name of the file to extract the text from. |
loadOptions | LoadOptions | Additional options to use when loading a document. Can be null . |
PlainTextDocument(InputStream stream)
public PlainTextDocument(InputStream stream)
Initializes a new instance of this class.
Parameters:
Parameter | Type | Description |
---|---|---|
stream | java.io.InputStream |
PlainTextDocument(InputStream stream, LoadOptions loadOptions)
public PlainTextDocument(InputStream stream, LoadOptions loadOptions)
Initializes a new instance of this class.
Parameters:
Parameter | Type | Description |
---|---|---|
stream | java.io.InputStream | |
loadOptions | LoadOptions |
getBuiltInDocumentProperties()
public BuiltInDocumentProperties getBuiltInDocumentProperties()
Gets getBuiltInDocumentProperties() of the document.
Examples:
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());
Returns: BuiltInDocumentProperties - getBuiltInDocumentProperties() of the document.
getCustomDocumentProperties()
public CustomDocumentProperties getCustomDocumentProperties()
Gets getCustomDocumentProperties() of the document.
Examples:
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());
Returns: CustomDocumentProperties - getCustomDocumentProperties() of the document.
getText()
public String getText()
Gets textual content of the document concatenated as a string.
Examples:
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());
Returns: java.lang.String - Textual content of the document concatenated as a string.