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

ConstructorDescription
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

MethodDescription
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:

ParameterTypeDescription
fileNamejava.lang.StringName 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:

ParameterTypeDescription
fileNamejava.lang.StringName of the file to extract the text from.
loadOptionsLoadOptionsAdditional 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:

ParameterTypeDescription
streamjava.io.InputStream

PlainTextDocument(InputStream stream, LoadOptions loadOptions)

public PlainTextDocument(InputStream stream, LoadOptions loadOptions)

Initializes a new instance of this class.

Parameters:

ParameterTypeDescription
streamjava.io.InputStream
loadOptionsLoadOptions

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.