Mafesoft.PlainTextReport
1.0.0.1
See the version list below for details.
dotnet add package Mafesoft.PlainTextReport --version 1.0.0.1
NuGet\Install-Package Mafesoft.PlainTextReport -Version 1.0.0.1
<PackageReference Include="Mafesoft.PlainTextReport" Version="1.0.0.1" />
<PackageVersion Include="Mafesoft.PlainTextReport" Version="1.0.0.1" />
<PackageReference Include="Mafesoft.PlainTextReport" />
paket add Mafesoft.PlainTextReport --version 1.0.0.1
#r "nuget: Mafesoft.PlainTextReport, 1.0.0.1"
#:package Mafesoft.PlainTextReport@1.0.0.1
#addin nuget:?package=Mafesoft.PlainTextReport&version=1.0.0.1
#tool nuget:?package=Mafesoft.PlainTextReport&version=1.0.0.1
PlainTextReport
PlainTextReport is a .NET library to generate a plain text report from a collection of objects. The columns are defined with lambda expressions, as in TinyCsv, and the report can be split in pages ready to print, with a header and a footer on every page.
Why PlainTextReport
| Feature | Description |
|---|---|
| Columns from lambda expressions | m => m.Price, also nested (m => m.Customer.Address.City) and null safe. See Columns |
| Column caption, width and alignment | The header text, a width in characters or as a percentage of the free space, left, right or center. See Column style |
| Formats and culture | Format strings ("N2", "dd/MM/yyyy"), IFormatProvider and the Culture option. See Culture |
| Custom converters | IValueConverter to print any value as you like. See Custom value converter |
| Long values | Truncated with … to fit the report width, or wrapped on more lines. See Width and truncation and Multi-line rows |
| Pages | ISO A paper sizes or custom sizes, portrait or landscape, margins, header and footer with placeholders. See Pages |
| Output | A string, the lines, the pages, a file, a file for every page, a Stream or a TextWriter, also asynchronously. See Generate the report |
Requirements
- .NET 5, 6, 7, 8 or 10
- .NET Framework 4.5.2, 4.6.2, 4.7.2 or 4.8
Installation
dotnet add package Mafesoft.PlainTextReport
Usage
Define the model that you want to print:
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; }
}
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());
});
string text = report.Bind(models).GetTextReport();
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 captions. |
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. |
Page |
null |
The page settings. null means a report without pages. See Pages. |
Columns
A column is defined by a property or a field of the model. AddColumn has several overloads, so a column can also specify:
- a name (
columnName) or a position (columnIndex); the columns are printed incolumnIndexorder; - a format string (
columnFormat), for example"dd/MM/yyyy"for dates or"N2"for numbers; - an
IFormatProvider; - a custom
IValueConverter; - a style (
PlainTextReportColumnStyle).
options.Columns.AddColumn(m => m.Id, style => { });
options.Columns.AddColumn("Product", m => m.Name);
options.Columns.AddColumn(0, "Code", m => m.Id, new CultureInfo("it-IT"));
options.Columns.AddColumn(m => m.Enabled, style => { }, new CustomBooleanConverter());
Only members can be selected: m => m.Price * 2 or m => m.Name.ToUpper() throw an ArgumentException, use an IValueConverter instead.
Nested properties
A column can read a property of a property, at any depth:
options.Columns.AddColumn(m => m.Customer.Address.City, style => { });
- When an intermediate value is
null(an order without a customer) the cell is empty, without aNullReferenceException. - The default name of a nested column is its path,
Customer.Address.City. Give it acolumnNameor a caption to print something else. Nullable<T>values work too:m => m.ShippedOn.Value.Yearis empty whenShippedOnisnull.
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. |
options.Columns.AddColumn(m => m.Price, style =>
{
style.Caption = "Prezzo";
style.Width = 10;
style.Alignment = PlainTextReportAlignment.Right;
}, columnFormat: "N2");
The caption changes only the header: the column name is still used to identify the column.
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%and50%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.Widthand 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.
Culture
Values are formatted with the invariant culture, so a report is the same on every machine, 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.
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.
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
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} |
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
string 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");
// asynchronously, with an optional CancellationToken
await report.Bind(models).SaveToFileAsync("report.txt", cancellationToken: token);
await report.Bind(models).SavePagesToFilesAsync("pages", cancellationToken: token);
- The files are written in UTF-8 without BOM, unless an
Encodingis given (for exampleEncoding.GetEncoding(850)for an old printer; on .NET 5+ registerCodePagesEncodingProvider.Instancefirst). 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
SavePagesToFileswrites a single file.
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 asSaveToFile. - 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.
WriteToAsynctakes an optionalCancellationToken.
Sample app
samples/PlainTextReportConsole prints a report without pages and a paged report, with header and footer, on the console:
dotnet run --project samples/PlainTextReportConsole
Benchmark
The benchmark project uses BenchmarkDotNet to measure the report generation on 100 to 100,000 rows. Run it in Release mode:
dotnet run -c Release --project benchmark/PlainTextReportBenchmark
Tests
The tests use xUnit and run on .NET 8 and .NET 10:
dotnet test src/PlainTextReport.sln
License
| Product | Versions 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. |
-
.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.