Mafesoft.PlainTextReport 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Mafesoft.PlainTextReport --version 1.0.0
                    
NuGet\Install-Package Mafesoft.PlainTextReport -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Mafesoft.PlainTextReport" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Mafesoft.PlainTextReport" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Mafesoft.PlainTextReport" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Mafesoft.PlainTextReport --version 1.0.0
                    
#r "nuget: Mafesoft.PlainTextReport, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Mafesoft.PlainTextReport@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Mafesoft.PlainTextReport&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Mafesoft.PlainTextReport&version=1.0.0
                    
Install as a Cake Tool

PlainTextReport

PlainTextReport is a .NET library to generate a plain text report from a collection of objects.

Status: release candidate (1.0.0).

To use it in your project, add the Mafesoft.PlainTextReport NuGet package to your project.

Nuget Nuget

Supported target frameworks: .NET 5, 6, 7, 8 and .NET Framework 4.5.2, 4.6.2, 4.7.2, 4.8.

Define the model

Define the model that you want to print, like this:

public class Model
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
    public DateTime CreatedOn { get; set; }
    public bool Enabled { get; set; }
}

Define the report

Create a PlainTextReport<T> and configure its options and columns:

using Mafesoft.PlainTextReport;

var report = new PlainTextReport<Model>(options =>
{
    // Options
    options.ShowTitle = true;
    options.Title = "My first plain text report";
    options.ShowHeader = true;
    options.ShowHeaderSeparator = true;
    options.ShowColumnSeparator = true;
    options.Width = 80;

    // Columns
    options.Columns.AddColumn(m => m.Id, style => { });
    options.Columns.AddColumn(m => m.Name, style => style.Width = 20);
    options.Columns.AddColumn(m => m.Price, style => { }, columnFormat: "N2");
    options.Columns.AddColumn(m => m.CreatedOn, style => { }, columnFormat: "dd/MM/yyyy");
    options.Columns.AddColumn(m => m.Enabled, style => style.Alignment = PlainTextReportAlignment.Center, new CustomBooleanConverter());
});

The result looks like this:

               My first plain text report

Id | Name                 | Price | CreatedOn  | Enabled
---+----------------------+-------+------------+--------
 1 | Product 1            |  1.40 | 06/11/2026 |   No
 2 | Product 2            |  2.80 | 05/11/2026 |   No
 3 | Product 3            |  4.20 | 04/11/2026 |   Yes

Options

Option Default Description
ShowTitle false Prints the title at the top of the report, centered, followed by an empty line.
Title The report title.
MultiLineTitle A title on more lines; when set (and not empty) it is used instead of Title.
ShowHeader false Prints the header row with the column names.
ShowHeaderSeparator false Prints a separator line below the header.
ShowColumnSeparator false Prints a separator between the columns; otherwise the columns are separated by a space.
ColumnSeparator '\|' The character used to separate the columns.
HeaderSeparator '-' The character used to draw the line below the header.
Width 0 The maximum width of the report, in characters. 0 means no limit.
MultiLineRows false Wraps the values longer than their column on more lines, instead of truncating them. See Multi-line rows.
Culture null The culture of the numbers, the dates, the column formats and the {date} placeholder. null means the invariant culture. See Culture.

Columns

A column is defined by a property or a field of the model, also nested: m => m.Customer.Address.City.

When an intermediate value is null (an order without a customer) the cell is empty. The default name of a nested column is its path, Customer.Address.City; give it a columnName to print something else.

Only members can be selected: m => m.Price * 2 or m => m.Name.ToUpper() throw an ArgumentException, use an IValueConverter instead.

AddColumn has several overloads, so a column can also specify:

  • a name (columnName) or a position (columnIndex); the columns are printed in columnIndex order;
  • a format string (columnFormat), for example "dd/MM/yyyy" for dates or "N2" for numbers;
  • an IFormatProvider;
  • a custom IValueConverter;
  • a style (PlainTextReportColumnStyle).

Culture

Values are formatted with the invariant culture, unless options.Culture is set or the column has its own IFormatProvider:

options.Culture = new CultureInfo("it-IT");
options.Columns.AddColumn(m => m.Price, style => { }, columnFormat: "N2");                // 1.234,50
options.Columns.AddColumn(m => m.CreatedOn, style => { }, columnFormat: "dd MMMM yyyy");  // 02 gennaio 2024
options.Columns.AddColumn(m => m.UpdatedOn, new CultureInfo("en-US"));                    // the column's provider wins

The culture is also used by the {date} placeholder of header and footer, and it is passed to the custom converters as provider.

Column style

Property Default Description
Caption null The text printed in the header. When null it is the column name (columnName, or the name of the property).
Width null Width of the column, in characters (20) or as percentage of the free space ("50%"). When null the width fits the header and the longest value.
Alignment null Left, Right or Center. When null numbers are right aligned and everything else is left aligned.
HeaderAlignment null Alignment of the header. When null it is the same as Alignment.
MultiLine null true wraps the values of the column on more lines, false keeps them on one line. When null it is the same as options.MultiLineRows.

The caption can be set together with the rest of the style:

options.Columns.AddColumn(m => m.Price, style =>
{
    style.Caption = "Prezzo";
    style.Width = 10;
    style.Alignment = PlainTextReportAlignment.Right;
}, columnFormat: "N2");

Column width in characters or percentage

Width is a ColumnWidth: it can be set with an int (characters) or with a string ("20" characters, "50%" percentage). ColumnWidth.Characters(20) and ColumnWidth.Percent(50) do the same.

options.Columns.AddColumn(m => m.Id, style => style.Width = 6);          // 6 characters
options.Columns.AddColumn(m => m.Name, style => style.Width = "100%");   // all the free space
options.Columns.AddColumn(m => m.Notes, style => style.Width = "50%");   // half of the free space

A percentage is a share of the free space: the report width (options.Width, or the printable width of the page) minus the column separators and the columns with a fixed or automatic width.

  • 100% takes all the free space, so the column grows until the table fills the report width.
  • When the percentages add up to more than 100% they are scaled down proportionally: two columns at 100% get 50% each, 100% and 50% get two thirds and one third.
  • Below 100% the rest of the free space stays empty: one column at 50% takes half of it.
  • Without a report width (no options.Width and no page settings) there is no free space to share, so a percentage column is sized like an automatic column.

Width and truncation

When Width is set and the columns don't fit, the report shrinks the automatic text columns first, then the automatic numeric columns, and the fixed-width columns last. A value that doesn't fit its column ends with …, so a truncated value is always visible.

Line breaks and tabs in the values are replaced by spaces, and trailing spaces are removed from every line.

Multi-line rows

With options.MultiLineRows = true a value longer than its column is not truncated: it wraps on more lines, breaking between the words (a word longer than the column is split). The line breaks of a value start a new line, and the row is as tall as its tallest cell. A single column can turn it on or off with style.MultiLine.

options.MultiLineRows = true;
options.Columns.AddColumn(m => m.Id, style => { });
options.Columns.AddColumn(m => m.Name, style => style.Width = 16);
options.Columns.AddColumn(m => m.Price, style => { }, columnFormat: "N2");
Id | Name             | Price
---+------------------+------
 1 | Red apples from  |  2.50
   | the Italian Alps |
 2 | Bananas          |  1.20
   | imported         |

With pages, the lines of a row are kept on the same page; only a row taller than a whole page continues on the next one.

Pages

Set options.Page to split the report in pages ready to print, with a header and a footer on every page:

var report = new PlainTextReport<Model>(options =>
{
    options.Title = "Products";
    options.ShowHeader = true;
    options.ShowHeaderSeparator = true;
    options.ShowColumnSeparator = true;

    options.Page = new PlainTextReportPageSettings(PaperSize.A4, PageOrientation.Portrait)
    {
        MarginLeft = 4,
        MarginRight = 4,
        MarginTop = 2,
        MarginBottom = 2,
    };
    options.Page.Header.Left = "{title}";
    options.Page.Header.Right = "{date:dd/MM/yyyy}";
    options.Page.Header.ShowSeparator = true;
    options.Page.Footer.Center = "Page {page} of {pages}";
    options.Page.Footer.ShowSeparator = true;

    options.Columns.AddColumn(m => m.Id, style => { });
    options.Columns.AddColumn(m => m.Name, style => style.Width = "100%");
});

Every page has exactly the height of the paper, so the footer is always at the bottom:

    Products                                                        07/11/2026
    --------------------------------------------------------------------------
     Id | Name
    ----+---------------------------------------------------------------------
      1 | Product 1
      2 | Product 2
    ...
    --------------------------------------------------------------------------
                                   Page 1 of 3

Paper size and orientation

A plain text page is measured in characters and lines, so the paper size is converted with the printer pitch: CharactersPerInch (default 10) and LinesPerInch (default 6).

The whole ISO 216 A series is available: PaperSize.A0 ... PaperSize.A10, or PaperSize.A(n). PaperSize.Custom(name, width, height) creates any other size in millimeters. Each paper can be used in Portrait or Landscape.

Paper Size (mm) Portrait (chars x lines) Landscape (chars x lines)
A3 297 x 420 116 x 99 165 x 70
A4 210 x 297 82 x 70 116 x 49
A5 148 x 210 58 x 49 82 x 34

Values at 10 characters and 6 lines per inch, margins included. Width, Height, PrintableWidth and PrintableHeight of the page settings return the actual values.

Page settings

Property Default Description
PaperSize A4 The paper size.
Orientation Portrait Portrait or Landscape.
CharactersPerInch 10 Pitch: 10 is pica, 12 is elite, 17 is condensed.
LinesPerInch 6 Line spacing: 6 is the common value, 8 is compressed.
MarginLeft, MarginRight 0 Margins, in characters.
MarginTop, MarginBottom 0 Margins, in lines.
Header, Footer empty The bands printed on every page.
RepeatColumnHeader true Prints the column header on every page, not only on the first one.
PageBreak "\f" Text put at the beginning of every page after the first one by GetTextReport (form feed, so a printer starts a new sheet). Set "" to have no break character.
Date now Date used by the {date} placeholder.

The table is fitted in the printable width (or in options.Width, if it is smaller). The title is printed only on the first page.

Header and Footer have three texts, Left, Center and Right, aligned on the printable width. A text can span more lines with \n. ShowSeparator draws a line (SeparatorChar, default -) between the band and the body.

The texts can contain these placeholders, with an optional format after ::

Placeholder Value Example
{page} Page number {page}, {page:000}
{pages} Number of pages Page {page} of {pages}
{title} Report title {title}
{date} Date of the page settings, yyyy-MM-dd by default {date:dd/MM/yyyy HH:mm}

Custom value converter

To control how a value is printed, implement IValueConverter:

using Mafesoft.PlainTextReport.Conversions;

public class CustomBooleanConverter : IValueConverter
{
    public string Convert(object value, object parameter, IFormatProvider provider)
        => value is bool b && b ? "Yes" : "No";
}

Without a custom converter a value is printed with the column's format and format provider; byte[] values are printed as base64.

Generate the report

Bind the collection and get the report as a single string, as an array of lines or as an array of pages:

// the whole report; with pages, every page after the first one starts with PageBreak
var text = report
    .Bind(models)
    .GetTextReport();

// the lines of all the pages
string[] lines = report
    .Bind(models)
    .GetTextLinesReport();

// the pages: Number, Count and Lines of each page
PlainTextReportPage[] pages = report
    .Bind(models)
    .GetPagesReport();

Without page settings GetPagesReport returns a single page with the whole report.

Save to file

Save the whole report in one file, or every page in its own file:

// one file; with pages, the pages are separated by PageBreak (form feed by default)
report.Bind(models).SaveToFile("report.txt");

// an absolute path: the directories are created when missing; the absolute path of the file is returned
string file = report.Bind(models).SaveToFile(@"C:\Reports\2026\report.txt");

// a file for every page: page-001.txt, page-002.txt, ...
string[] files = report.Bind(models).SavePagesToFiles("pages");
string[] absoluteFiles = report.Bind(models).SavePagesToFiles(@"C:\Reports\2026\pages");

// a custom file name, with the placeholders {page}, {pages}, {title} and {date}
report.Bind(models).SavePagesToFiles("pages", "{title}-{date:yyyyMMdd}-{page:000}.txt");
  • The files are written in UTF-8 without BOM, unless an Encoding is given (for example Encoding.GetEncoding(850) for an old printer; on .NET 5+ register CodePagesEncodingProvider.Instance first). With a code page that has no …, truncated values end with ?.
  • Paths can be absolute or relative to the current directory; both methods return absolute paths.
  • Existing files are overwritten, and missing directories are created.
  • The filename pattern must contain {page} when there is more than one page; characters that are not valid in a filename are replaced by _.
  • Without page settings SavePagesToFiles writes a single file.
  • SaveToFileAsync and SavePagesToFilesAsync do the same asynchronously, with an optional CancellationToken.

Write to a stream or a writer

The report can be written to any Stream or TextWriter, for example the body of an HTTP response, without a file:

// ASP.NET Core
app.MapGet("/report.txt", async (HttpResponse response) =>
{
    response.ContentType = "text/plain; charset=utf-8";
    await report.Bind(models).WriteToAsync(response.Body);
});

// a MemoryStream, with a different encoding
using var stream = new MemoryStream();
report.Bind(models).WriteTo(stream, Encoding.GetEncoding(850));

// a TextWriter
report.Bind(models).WriteTo(Console.Out);
  • The text is the same as GetTextReport(), followed by a line break, the same as SaveToFile.
  • The stream and the writer are flushed but not closed.
  • The BOM of the encoding, if any, is written only at the beginning of a seekable stream.

A complete example, with and without pages, is available in samples/PlainTextReportConsole.

Benchmark

The benchmark project uses BenchmarkDotNet to measure the report generation on 100 to 100,000 rows. Run it in Release mode:

License

MIT

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Framework net452 is compatible.  net46 was computed.  net461 was computed.  net462 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 is compatible.  net48 is compatible.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.5.2

    • No dependencies.
  • .NETFramework 4.6.2

    • No dependencies.
  • .NETFramework 4.7.2

    • No dependencies.
  • .NETFramework 4.8

    • No dependencies.
  • net10.0

    • No dependencies.
  • net5.0

    • No dependencies.
  • net6.0

    • No dependencies.
  • net7.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.4.0 93 11/14/2026
1.3.0 91 11/14/2026
1.2.1 91 11/14/2026
1.2.0 98 11/13/2026
1.1.0 90 11/12/2026
1.0.0.1 109 11/8/2026
1.0.0 100 11/8/2026