Docs / Guides

Flowing documents

FlowDocument is SoftZenPdf's layout engine: you describe paragraphs, lists and tables, and it breaks them into lines and pages. The HTML converter is built on it.

Paragraphs, lists and tables

A document is a list of blocks. A Paragraph holds runs of text in TextStyles, images and fields; a Table holds rows of cells, each with its own blocks; a Container draws a border and background around other blocks. Headers and footers are blocks too:

Program.cs
using SoftZenPdf;
using SoftZenPdf.Layout;

var flow = new FlowDocument();
var heading = new TextStyle { FontSize = 18, Bold = true };
var small = new TextStyle { FontSize = 9 };

// A footer on every page: "Page 3 of 7".
var footer = new Paragraph { Style = new ParagraphStyle { Alignment = ParagraphAlignment.Center } };
footer.Add("Page ", small).Add(new Field(FieldKind.PageNumber, small)).Add(" of ", small).Add(new Field(FieldKind.PageCount, small));
flow.PageSetup.Footer.Add(footer);

flow.Add(new Paragraph("Project status", heading) { OutlineLevel = 1, Style = new ParagraphStyle { SpaceAfter = 6, KeepWithNext = true } });
flow.Add(new Paragraph("Text wraps to the width of the page and continues on as many pages as it needs. ")
    .Add("Runs can be bold, ", new TextStyle { Bold = true })
    .Add("coloured", new TextStyle { Color = PdfColor.Blue })
    .Add(" or ")
    .Add("links", new TextStyle { Link = "https://example.com/", Underline = true })
    .Add("."));

foreach (var (item, i) in new[] { "Design approved", "Build in progress", "Launch in May" }.Select((t, i) => (t, i)))
    flow.Add(new Paragraph(item) { Marker = $"{i + 1}.", Style = new ParagraphStyle { LeftIndent = 24, FirstLineIndent = -18 } });

var table = new Table { Width = ColumnWidth.Percent(100), CellBorders = Borders.All(BorderLine.Thin) };
table.AddRow("Task", "Owner", "Due").IsHeader = true;   // repeated on every page the table continues on
table.AddRow("Wireframes", "Ana", "3 March");
table.AddRow("API", "Ben", "17 March");
flow.Add(table);

flow.ToPdf().Save("flow.pdf");

Paragraph layout

  • Alignment: start, end, left, centre, right or justified; the start follows the paragraph's direction.
  • Indents and spacing: left, right and first-line indents (negative for hanging), space before and after, and line spacing as a multiple or in points.
  • Breaking: lines break at spaces, hyphens, soft hyphens and between CJK characters; words longer than a line are split. Keep-with-next, keep-together and widow and orphan control decide where pages break.
  • Lists: a Marker such as a bullet or 3. is drawn in the hanging indent.
  • Right-to-left: Hebrew, Arabic and mixed lines are ordered with the Unicode Bidirectional Algorithm, with runs in different styles.
  • Fields: page number and page count, filled in when each page is drawn.

Tables

  • Column widths are fixed, percentages, or follow the content, as in HTML's automatic table layout; narrow columns keep their natural width next to long text.
  • Cells can span columns and rows, and have their own padding, borders, background and vertical alignment.
  • Header rows repeat at the top of every page the table continues on; a row that does not fit is split between pages.

Fonts

A FontProvider finds fonts by family name. Register font files (.ttf, .otf with TrueType outlines, and .ttc collections), whole folders, or the fonts installed on the computer with AddSystemFonts. Family lists work as in CSS, and characters the chosen font lacks come from FallbackFamilies, then from any registered font:

Program.cs
using SoftZenPdf;
using SoftZenPdf.Layout;

var fonts = new FontProvider();
fonts.AddFile("NotoSans-Regular.ttf");          // a family by its own name: "Noto Sans"
fonts.AddFile("NotoSansHebrew-Regular.ttf");
fonts.FallbackFamilies.Add("Noto Sans Hebrew"); // for characters "Noto Sans" does not have
fonts.DefaultFamily = "Noto Sans";

// Family lists work as in CSS: the first available family wins.
var font = fonts.GetFont("Calibri, 'Noto Sans', sans-serif");
Console.WriteLine(((TrueTypeFont)font).FamilyName);

var flow = new FlowDocument { Fonts = fonts };
flow.Add(new Paragraph("Shalom שלום in one run: the Hebrew comes from the fallback font."));
flow.ToPdf().Save("fonts.pdf");
Output
Noto Sans

A family without a bold or italic face is drawn with its regular face, slightly heavier or slanted. A character that no font can show becomes a question mark and is reported in Warnings.

Pages, headers and footers

PageSetup sets the page size, margins, header and footer (with a different first page if you want). A SectionBreak starts a new page with another setup, a PageBreak just a new page. Blocks with an Id can be linked to with #id, and paragraphs with an OutlineLevel become bookmarks.

API: FlowDocument, Paragraph, Table, FontProvider, PageSetup.