Class TxtSaveOptionsBase

Class TxtSaveOptionsBase

Namespace: Aspose.Words.Saving
Assembly: Aspose.Words.dll (26.4.0)

The base class for specifying additional options when saving a document into a text based formats.

To learn more, visit the Specify Save Options documentation article.

public abstract class TxtSaveOptionsBase : SaveOptions

Inheritance

object SaveOptions TxtSaveOptionsBase

Derived

MarkdownSaveOptions , TxtSaveOptions

Inherited Members

SaveOptions.CreateSaveOptions(SaveFormat) , SaveOptions.CreateSaveOptions(string) , SaveOptions.SaveFormat , SaveOptions.ExportGeneratorName , SaveOptions.TempFolder , SaveOptions.UpdateOleControlImages , SaveOptions.PrettyFormat , SaveOptions.UseAntiAliasing , SaveOptions.UseHighQualityRendering , SaveOptions.DmlRenderingMode , SaveOptions.DmlEffectsRenderingMode , SaveOptions.ImlRenderingMode , SaveOptions.DefaultTemplate , SaveOptions.UpdateFields , SaveOptions.UpdateLastSavedTimeProperty , SaveOptions.UpdateLastPrintedProperty , SaveOptions.UpdateCreatedTimeProperty , SaveOptions.MemoryOptimization , SaveOptions.UpdateAmbiguousTextFont , SaveOptions.Dml3DEffectsRenderingMode , SaveOptions.ProgressCallback , SaveOptions.AllowEmbeddingPostScriptFonts , SaveOptions.CustomTimeZoneInfo , object.GetType() , object.MemberwiseClone() , object.ToString() , object.Equals(object?) , object.Equals(object?, object?) , object.ReferenceEquals(object?, object?) , object.GetHashCode()

Examples

Shows how to save a .txt document with a custom paragraph break.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.Writeln("Paragraph 1.");
builder.Writeln("Paragraph 2.");
builder.Write("Paragraph 3.");

// Create a "TxtSaveOptions" object, which we can pass to the document's "Save" method
// to modify how we save the document to plaintext.
TxtSaveOptions txtSaveOptions = new TxtSaveOptions();

Assert.That(txtSaveOptions.SaveFormat, Is.EqualTo(SaveFormat.Text));

// Set the "ParagraphBreak" to a custom value that we wish to put at the end of every paragraph.
txtSaveOptions.ParagraphBreak = " End of paragraph.\n\n\t";

doc.Save(ArtifactsDir + "TxtSaveOptions.ParagraphBreak.txt", txtSaveOptions);

string docText = File.ReadAllText(ArtifactsDir + "TxtSaveOptions.ParagraphBreak.txt");

Assert.That(docText, Is.EqualTo("Paragraph 1. End of paragraph.\n\n\t" +
                "Paragraph 2. End of paragraph.\n\n\t" +
                "Paragraph 3. End of paragraph.\n\n\t"));

Constructors

TxtSaveOptionsBase()

protected TxtSaveOptionsBase()

Properties

Encoding

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

public Encoding Encoding { get; set; }

Property Value

Encoding

Examples

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

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

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

// Create a "TxtSaveOptions" object, which we can pass to the document's "Save" method
// to modify how we save the document to plaintext.
TxtSaveOptions txtSaveOptions = new TxtSaveOptions();

// Verify that the "Encoding" property contains the appropriate encoding for our document's contents.
Assert.That(txtSaveOptions.Encoding, Is.EqualTo(System.Text.Encoding.UTF8));

doc.Save(ArtifactsDir + "TxtSaveOptions.Encoding.UTF8.txt", txtSaveOptions);

string docText = System.Text.Encoding.UTF8.GetString(File.ReadAllBytes(ArtifactsDir + "TxtSaveOptions.Encoding.UTF8.txt"));

Assert.That(docText, Is.EqualTo("\uFEFFÀ È Ì Ò Ù.\r\n"));

// Using an unsuitable encoding may result in a loss of document contents.
txtSaveOptions.Encoding = System.Text.Encoding.ASCII;
doc.Save(ArtifactsDir + "TxtSaveOptions.Encoding.ASCII.txt", txtSaveOptions);
docText = System.Text.Encoding.ASCII.GetString(File.ReadAllBytes(ArtifactsDir + "TxtSaveOptions.Encoding.ASCII.txt"));

Assert.That(docText, Is.EqualTo("? ? ? ? ?.\r\n"));

ExportHeadersFootersMode

Specifies the way headers and footers are exported to the text formats. Default value is Aspose.Words.Saving.TxtExportHeadersFootersMode.PrimaryOnly.

public TxtExportHeadersFootersMode ExportHeadersFootersMode { get; set; }

Property Value

TxtExportHeadersFootersMode

Examples

Shows how to specify how to export headers and footers to plain text format.

Document doc = new Document();

// Insert even and primary headers/footers into the document.
// The primary header/footers will override the even headers/footers.
doc.FirstSection.HeadersFooters.Add(new HeaderFooter(doc, HeaderFooterType.HeaderEven));
doc.FirstSection.HeadersFooters[HeaderFooterType.HeaderEven].AppendParagraph("Even header");
doc.FirstSection.HeadersFooters.Add(new HeaderFooter(doc, HeaderFooterType.FooterEven));
doc.FirstSection.HeadersFooters[HeaderFooterType.FooterEven].AppendParagraph("Even footer");
doc.FirstSection.HeadersFooters.Add(new HeaderFooter(doc, HeaderFooterType.HeaderPrimary));
doc.FirstSection.HeadersFooters[HeaderFooterType.HeaderPrimary].AppendParagraph("Primary header");
doc.FirstSection.HeadersFooters.Add(new HeaderFooter(doc, HeaderFooterType.FooterPrimary));
doc.FirstSection.HeadersFooters[HeaderFooterType.FooterPrimary].AppendParagraph("Primary footer");

// Insert pages to display these headers and footers.
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Writeln("Page 1");
builder.InsertBreak(BreakType.PageBreak);
builder.Writeln("Page 2");
builder.InsertBreak(BreakType.PageBreak); 
builder.Write("Page 3");

// Create a "TxtSaveOptions" object, which we can pass to the document's "Save" method
// to modify how we save the document to plaintext.
TxtSaveOptions saveOptions = new TxtSaveOptions();

// Set the "ExportHeadersFootersMode" property to "TxtExportHeadersFootersMode.None"
// to not export any headers/footers.
// Set the "ExportHeadersFootersMode" property to "TxtExportHeadersFootersMode.PrimaryOnly"
// to only export primary headers/footers.
// Set the "ExportHeadersFootersMode" property to "TxtExportHeadersFootersMode.AllAtEnd"
// to place all headers and footers for all section bodies at the end of the document.
saveOptions.ExportHeadersFootersMode = txtExportHeadersFootersMode;

doc.Save(ArtifactsDir + "TxtSaveOptions.ExportHeadersFooters.txt", saveOptions);

string docText = File.ReadAllText(ArtifactsDir + "TxtSaveOptions.ExportHeadersFooters.txt");

string newLine = Environment.NewLine;
switch (txtExportHeadersFootersMode)
{
    case TxtExportHeadersFootersMode.AllAtEnd:
        Assert.That(docText, Is.EqualTo($"Page 1{newLine}" +
                        $"Page 2{newLine}" +
                        $"Page 3{newLine}" +
                        $"Even header{newLine}{newLine}" +
                        $"Primary header{newLine}{newLine}" +
                        $"Even footer{newLine}{newLine}" +
                        $"Primary footer{newLine}{newLine}"));
        break;
    case TxtExportHeadersFootersMode.PrimaryOnly:
        Assert.That(docText, Is.EqualTo($"Primary header{newLine}" +
                        $"Page 1{newLine}" +
                        $"Page 2{newLine}" +
                        $"Page 3{newLine}" +
                        $"Primary footer{newLine}"));
        break;
    case TxtExportHeadersFootersMode.None:
        Assert.That(docText, Is.EqualTo($"Page 1{newLine}" +
                        $"Page 2{newLine}" +
                        $"Page 3{newLine}"));
        break;
}

ForcePageBreaks

Allows to specify whether the page breaks should be preserved during export.

The default value is false.

public bool ForcePageBreaks { get; set; }

Property Value

bool

Examples

Shows how to specify whether to preserve page breaks when exporting a document to plaintext.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.Writeln("Page 1");
builder.InsertBreak(BreakType.PageBreak);
builder.Writeln("Page 2");
builder.InsertBreak(BreakType.PageBreak);
builder.Writeln("Page 3");

// Create a "TxtSaveOptions" object, which we can pass to the document's "Save"
// method to modify how we save the document to plaintext.
TxtSaveOptions saveOptions = new TxtSaveOptions();

// The Aspose.Words "Document" objects have page breaks, just like Microsoft Word documents.
// Save formats such as ".txt" are one continuous body of text without page breaks.
// Set the "ForcePageBreaks" property to "true" to preserve all page breaks in the form of '\f' characters.
// Set the "ForcePageBreaks" property to "false" to discard all page breaks.
saveOptions.ForcePageBreaks = forcePageBreaks;

doc.Save(ArtifactsDir + "TxtSaveOptions.PageBreaks.txt", saveOptions);

// If we load a plaintext document with page breaks,
// the "Document" object will use them to split the body into pages.
doc = new Document(ArtifactsDir + "TxtSaveOptions.PageBreaks.txt");

Assert.That(doc.PageCount, Is.EqualTo(forcePageBreaks ? 3 : 1));

Remarks

The property affects only page breaks that are inserted explicitly into a document. It is not related to page breaks that MS Word automatically inserts at the end of each page.

ParagraphBreak

Specifies the string to use as a paragraph break when exporting in text formats.

public string ParagraphBreak { get; set; }

Property Value

string

Examples

Shows how to save a .txt document with a custom paragraph break.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.Writeln("Paragraph 1.");
builder.Writeln("Paragraph 2.");
builder.Write("Paragraph 3.");

// Create a "TxtSaveOptions" object, which we can pass to the document's "Save" method
// to modify how we save the document to plaintext.
TxtSaveOptions txtSaveOptions = new TxtSaveOptions();

Assert.That(txtSaveOptions.SaveFormat, Is.EqualTo(SaveFormat.Text));

// Set the "ParagraphBreak" to a custom value that we wish to put at the end of every paragraph.
txtSaveOptions.ParagraphBreak = " End of paragraph.\n\n\t";

doc.Save(ArtifactsDir + "TxtSaveOptions.ParagraphBreak.txt", txtSaveOptions);

string docText = File.ReadAllText(ArtifactsDir + "TxtSaveOptions.ParagraphBreak.txt");

Assert.That(docText, Is.EqualTo("Paragraph 1. End of paragraph.\n\n\t" +
                "Paragraph 2. End of paragraph.\n\n\t" +
                "Paragraph 3. End of paragraph.\n\n\t"));

Remarks

The default value is Aspose.Words.ControlChar.CrLf.

 English