FlowDocuments

edit

Build a document from blocks

The content builder draws at positions you compute. PdfFlowDocument computes them: you add blocks in reading order, and Build breaks their text into lines and the blocks into pages:

var flow = new PdfFlowDocument(new PdfFlowOptions
{
    Title = "Quarterly report",
    Language = "en-US",
    Footer = (page, count) => $"Page {page} of {count}",
});
flow.TableOfContents()
    .Heading(1, "Summary")
    .Paragraph(text => text
        .Text("Sales grew by ")
        .Text("12%", new PdfTextStyle { Font = PdfFont.GetStandardFont(PdfStandardFont.HelveticaBold) })
        .Text(". See ")
        .Link("the website", new Uri("https://example.com")))
    .List(["North: +8%", "South: +15%"])
    .Heading(1, "Details")
    .Table(table => table
        .Columns(2, 1)
        .HeaderRow("Region", "Sales")
        .Row("North", "1,200")
        .Row("South", "1,500"));

var document = flow.Build();

Build returns a PdfDocument: change it like any other document, then save it.

Blocks

MethodBlock
HeadingA heading of level 1 to 6, in the heading font, kept with the block that follows it
ParagraphA paragraph of text, or of runs of text in several fonts, sizes and colors, with links (PdfInlineBuilder)
ListA bulleted or numbered list, whose items hold texts or blocks (nested lists)
ImageAn image with its alternate description, scaled down to fit the column and the page
TableA table: header rows repeated on each page, cells that span columns and rows and hold blocks
Rule, SpacerA horizontal line, an empty space
PageBreakThe next block starts a new page
ColumnsBlocks laid out in columns, which continue on the next pages
TableOfContentsThe headings of levels 1 to 3, with their page numbers and links to them
  • PdfParagraphStyle sets the alignment (left, center, right, justified), the spaces before and after, the indentation and the text style of a paragraph; PdfTextStyle the font, size and color of a run.
  • The text is broken at the break opportunities of the Unicode line breaking algorithm, and a word longer than the line between its characters. A line feed breaks the line.
  • A paragraph whose first strong character is right to left (Hebrew, Arabic) is aligned to the right, and an embedded font shapes its text.
  • A table row stays on one page, with the rows its cells span.
  • The table of contents needs the pages of the headings: Build lays the document out again until they are stable.

Pages and styles

PdfFlowOptions sets the size of the pages and their margins, the font, size, line height and color of the text, the heading font, and the space after the blocks. Header and Footer give the text of the header and of the footer of each page from its number and the number of pages; they are drawn centered in the margins.

The headings are added to the outline (the bookmarks) unless CreateOutline is false.

Accessible documents

The document is tagged unless Tagged is false: its structure tree has a Document element with the headings (H1 to H6), the paragraphs (P), the lists (L, LI, Lbl, LBody), the figures with their alternate descriptions, the tables (Table, THead, TBody, TR, TH with their scope, TD, with their spans), the links and the table of contents (TOC, TOCI). The borders, rules, headers and footers are artifacts.

For a PDF/UA document, embed the fonts (Font), set the Title and the Language, and set PdfUAConformance on the document Build returns: see Tagged PDF and PDF/UA. The attributes of the structure elements are written with SetAttribute, and read with GetAttribute.