Aspose.Words Text File Processor for .NET

The Aspose.Words Text File Processor for .NET is a lightweight API focused on plain‑text scenarios. Create new text files, load existing .txt, perform programmatic edits (insert, delete, replace), and save back to TXT with precise control over encoding, BOM, and line endings—ideal for services, ETL jobs, and CI/CD pipelines.

No Microsoft Office required. Works on Windows, Linux, and macOS with .NET Framework, .NET Core/5/6+, or Mono.


Installation and Setup

  1. Install Aspose.Words from NuGet.
  2. Apply your license at startup (see Metered Licensing ).
  3. Review system requirements in the Installation Guide .

Supported scenarios: file paths and streams (recommended for web/cloud).


Quick Start

Create a TXT file (UTF‑8, CRLF)

using Aspose.Words;
using Aspose.Words.Saving;
using System.Text;

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

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

var txt = new TxtSaveOptions
{
    Encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: true), // with BOM
    ParagraphBreak = "\r\n"                                          // Windows EOL
};

doc.Save("Output.txt", txt);

Load a TXT with explicit encoding, append text, save as LF

using Aspose.Words;
using Aspose.Words.Loading;
using Aspose.Words.Saving;
using System.Text;

var load = new TxtLoadOptions { Encoding = Encoding.UTF8 };
var doc = new Document("Input.txt", load);

var builder = new DocumentBuilder(doc);
builder.MoveToDocumentEnd();
builder.Writeln("");
builder.Writeln("Appended by Aspose.Words.");

var txt = new TxtSaveOptions
{
    Encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false), // UTF‑8 no BOM
    ParagraphBreak = "\n"                                              // Unix EOL
};

doc.Save("Updated.txt", txt);

Features and Functionality

Document Creation

  • Start from a blank text document in memory.
  • Set default encoding and EOL strategy when saving.
  • Optionally write header/footer comments before body text.

Loading Existing TXT

  • Load from path, stream, or byte[] with encoding auto‑detect or a forced encoding.
  • Preserve or normalize line breaks (CRLF / LF / CR) on load and/or save.
  • Stream very large files to keep memory stable.

Text Manipulation

  • Traverse the content via the familiar Document → Section → Paragraph → Run model.
  • Insert, append, or delete text at precise locations—no manual offset math.
  • Perform bulk find/replace across the whole document or scoped ranges.
  • Split/merge paragraphs by custom delimiters.

Saving to TXT

  • Save to file or stream with chosen encoding (UTF‑8/UTF‑16/ASCII/etc.).
  • Control BOM emission and ParagraphBreak (EOL) explicitly.
  • Overwrite or write to a target stream you manage.

Encoding & i18n

  • Full Unicode support.
  • Transparent fallbacks for legacy code pages when reading.
  • Detect and preserve BOM on load unless overridden.

Stream‑First APIs

  • Use StreamStream for web services and cloud functions.
  • Integrate easily with S3/Blob SDKs and in‑memory pipelines.

Performance

  • Lean object model for plain text.
  • Lazy operations minimize allocations.
  • Thread‑safe reading; synchronize writes when sharing resources.

Common Recipes

1) Regex Find/Replace (collapse multiple spaces)

using System.Text.RegularExpressions;
using Aspose.Words;
using Aspose.Words.Replacing;

var doc = new Document("input.txt");
var opts = new FindReplaceOptions { MatchCase = false };
doc.Range.Replace(new Regex(@"\s{2,}"), " ", opts);

doc.Save("cleaned.txt");

2) Stream→Stream (Windows‑1252 to UTF‑8 LF)

using (var input = File.OpenRead("legacy.txt"))
using (var output = File.Create("normalized.txt"))
{
    var load = new Aspose.Words.Loading.TxtLoadOptions
    {
        Encoding = Encoding.GetEncoding(1252)
    };
    var doc = new Document(input, load);

    var save = new Aspose.Words.Saving.TxtSaveOptions
    {
        Encoding = new UTF8Encoding(false), // no BOM
        ParagraphBreak = "\n"
    };
    doc.Save(output, save);
}

3) Normalize Line Endings (CRLF → LF)

var d = new Document("crlf.txt");
var save = new Aspose.Words.Saving.TxtSaveOptions { ParagraphBreak = "\n" };
d.Save("lf.txt", save);

Tips & Best Practices

  • Prefer stream‑based APIs for large files and web apps.
  • Always set encoding on load & save to avoid mojibake.
  • Batch edits in memory; save once to reduce I/O.
  • Normalize EOL on load, convert to target EOL on save.
  • Use regex replace for complex cleanups in one pass.
  • In long‑running services, monitor metered usage & handle quota gracefully.

FAQ

Does TXT preserve tables, images, or styles? No. TXT is plain text; advanced formatting is flattened.

Can I append to an existing file without overwriting? Open a FileStream in append mode and write the saved output to it. Ensure you add a preceding newline if needed.

How do I choose the line ending? Set TxtSaveOptions.ParagraphBreak to "\r\n" (Windows) or "\n" (Unix/macOS).

How do I disable the UTF‑8 BOM? Use new UTF8Encoding(false) in TxtSaveOptions.Encoding.

Is Office or Notepad++ required? No. Aspose.Words is standalone.