PlainTextDocument

PlainTextDocument(string)

Creates a plain text document from a file. Automatically detects the file format.

public PlainTextDocument(string fileName)
ParameterTypeDescription
fileNameStringName of the file to extract the text from.

Exceptions

exceptioncondition
UnsupportedFileFormatExceptionThe document format is not recognized or not supported.
FileCorruptedExceptionThe document appears to be corrupted and cannot be loaded.
ExceptionThere is a problem with the document and it should be reported to Aspose.Words developers.
IOExceptionThere is an input/output exception.
IncorrectPasswordExceptionThe document is encrypted and requires a password to open, but you supplied an incorrect password.
ArgumentExceptionThe name of the file cannot be null or empty 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(ArtifactsDir + "PlainTextDocument.Load.docx");

PlainTextDocument plaintext = new PlainTextDocument(ArtifactsDir + "PlainTextDocument.Load.docx");

Assert.AreEqual("Hello world!", plaintext.Text.Trim());

See Also


PlainTextDocument(string, LoadOptions)

Creates a plain text document from a file. Allows to specify additional options such as an encryption password.

public PlainTextDocument(string fileName, LoadOptions loadOptions)
ParameterTypeDescription
fileNameStringName of the file to extract the text from.
loadOptionsLoadOptionsAdditional options to use when loading a document. Can be null.

Exceptions

exceptioncondition
UnsupportedFileFormatExceptionThe document format is not recognized or not supported.
FileCorruptedExceptionThe document appears to be corrupted and cannot be loaded.
ExceptionThere is a problem with the document and it should be reported to Aspose.Words developers.
IOExceptionThere is an input/output exception.
IncorrectPasswordExceptionThe document is encrypted and requires a password to open, but you supplied an incorrect password.
ArgumentExceptionThe name of the file cannot be null or empty string.

Examples

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.Password = "MyPassword";

doc.Save(ArtifactsDir + "PlainTextDocument.LoadEncrypted.docx", saveOptions);

LoadOptions loadOptions = new LoadOptions();
loadOptions.Password = "MyPassword";

PlainTextDocument plaintext = new PlainTextDocument(ArtifactsDir + "PlainTextDocument.LoadEncrypted.docx", loadOptions);

Assert.AreEqual("Hello world!", plaintext.Text.Trim());

See Also


PlainTextDocument(Stream)

Creates a plain text document from a stream. Automatically detects the file format.

public PlainTextDocument(Stream stream)
ParameterTypeDescription
streamStreamThe stream where to extract the text from.

Exceptions

exceptioncondition
UnsupportedFileFormatExceptionThe document format is not recognized or not supported.
FileCorruptedExceptionThe document appears to be corrupted and cannot be loaded.
ExceptionThere is a problem with the document and it should be reported to Aspose.Words developers.
IOExceptionThere is an input/output exception.
IncorrectPasswordExceptionThe document is encrypted and requires a password to open, but you supplied an incorrect password.
ArgumentNullExceptionThe stream cannot be null.
NotSupportedExceptionThe stream does not support reading or seeking.
ObjectDisposedExceptionThe stream is a disposed object.

Remarks

The document must be stored at the beginning of the stream. The stream must support random positioning.

Examples

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(ArtifactsDir + "PlainTextDocument.LoadFromStream.docx");

using (FileStream stream = new FileStream(ArtifactsDir + "PlainTextDocument.LoadFromStream.docx", FileMode.Open))
{
    PlainTextDocument plaintext = new PlainTextDocument(stream);

    Assert.AreEqual("Hello world!", plaintext.Text.Trim());
}

See Also


PlainTextDocument(Stream, LoadOptions)

Creates a plain text document from a stream. Allows to specify additional options such as an encryption password.

public PlainTextDocument(Stream stream, LoadOptions loadOptions)
ParameterTypeDescription
streamStreamThe stream where to extract the text from.
loadOptionsLoadOptionsAdditional options to use when loading a document. Can be null.

Exceptions

exceptioncondition
UnsupportedFileFormatExceptionThe document format is not recognized or not supported.
FileCorruptedExceptionThe document appears to be corrupted and cannot be loaded.
ExceptionThere is a problem with the document and it should be reported to Aspose.Words developers.
IOExceptionThere is an input/output exception.
IncorrectPasswordExceptionThe document is encrypted and requires a password to open, but you supplied an incorrect password.
ArgumentNullExceptionThe stream cannot be null.
NotSupportedExceptionThe stream does not support reading or seeking.
ObjectDisposedExceptionThe stream is a disposed object.

Remarks

The document must be stored at the beginning of the stream. The stream must support random positioning.

Examples

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.Password = "MyPassword";

doc.Save(ArtifactsDir + "PlainTextDocument.LoadFromStreamWithOptions.docx", saveOptions);

LoadOptions loadOptions = new LoadOptions();
loadOptions.Password = "MyPassword";

using (FileStream stream = new FileStream(ArtifactsDir + "PlainTextDocument.LoadFromStreamWithOptions.docx", FileMode.Open))
{
    PlainTextDocument plaintext = new PlainTextDocument(stream, loadOptions);

    Assert.AreEqual("Hello world!", plaintext.Text.Trim());
}

See Also