Aspose::Words::Saving::TxtSaveOptionsBase::set_Encoding method

TxtSaveOptionsBase::set_Encoding method

Specifies the encoding to use when exporting in text formats. Default value is Encoding.UTF8.

void Aspose::Words::Saving::TxtSaveOptionsBase::set_Encoding(const System::SharedPtr<System::Text::Encoding> & value)

Examples

Shows how to set encoding for a .txt output document.

auto doc = System::MakeObject<Aspose::Words::Document>();
auto builder = System::MakeObject<Aspose::Words::DocumentBuilder>(doc);

// Add some text with characters from outside the ASCII character set.
builder->Write(u"À È Ì Ò Ù.");

// Create a "TxtSaveOptions" object, which we can pass to the document's "Save" method
// to modify how we save the document to plaintext.
auto txtSaveOptions = System::MakeObject<Aspose::Words::Saving::TxtSaveOptions>();

// Verify that the "Encoding" property contains the appropriate encoding for our document's contents.
ASPOSE_ASSERT_EQ(txtSaveOptions->get_Encoding(), System::Text::Encoding::get_UTF8());

doc->Save(get_ArtifactsDir() + u"TxtSaveOptions.Encoding.UTF8.txt", txtSaveOptions);

System::String docText = System::Text::Encoding::get_UTF8()->GetString(System::IO::File::ReadAllBytes(get_ArtifactsDir() + u"TxtSaveOptions.Encoding.UTF8.txt"));

ASSERT_EQ(docText, u"\ufeffÀ È Ì Ò Ù.\r\n");

// Using an unsuitable encoding may result in a loss of document contents.
txtSaveOptions->set_Encoding(System::Text::Encoding::get_ASCII());
doc->Save(get_ArtifactsDir() + u"TxtSaveOptions.Encoding.ASCII.txt", txtSaveOptions);
docText = System::Text::Encoding::get_ASCII()->GetString(System::IO::File::ReadAllBytes(get_ArtifactsDir() + u"TxtSaveOptions.Encoding.ASCII.txt"));

ASSERT_EQ(docText, u"? ? ? ? ?.\r\n");

See Also