Table of Contents

Class LayoutDocument

Namespace
MiPdf.Layout
Assembly
MiPdf.dll

Flows LayoutElements (paragraphs, tables, images) onto pages, creating new pages as needed. Headers and footers are drawn when the layout is closed, so they can show the total page count.

public sealed class LayoutDocument : IDisposable
Inheritance
LayoutDocument
Implements
Inherited Members

Examples

using var pdf = PdfDocument.Create();
using (var layout = new LayoutDocument(pdf, PageSize.A4, new Thickness(50)))
{
    layout.Footer = ctx => ctx.Canvas.DrawText($"Page {ctx.PageNumber} of {ctx.TotalPages}",
        PdfStandardFont.Helvetica, 8, ctx.ContentArea.Right, 30, alignment: TextAlignment.Right);
    layout.Add(new Paragraph("Hello world", PdfStandardFont.HelveticaBold, 18));
}
pdf.Save("out.pdf");

Constructors

LayoutDocument(PdfDocument, PdfRectangle?, Thickness?)

Creates a layout document that adds its pages to document.

public LayoutDocument(PdfDocument document, PdfRectangle? pageSize = null, Thickness? margins = null)

Parameters

document PdfDocument

The document to add pages to.

pageSize PdfRectangle?

Size of new pages (default A4).

margins Thickness?

Page margins in points (default 56.7 pt = 2 cm on every side).

Properties

Canvas

The canvas of the current page (for custom drawing at absolute positions).

public PdfCanvas Canvas { get; }

Property Value

PdfCanvas

ContentArea

The area inside the margins, computed from PageSize and Margins.

public PdfRectangle ContentArea { get; }

Property Value

PdfRectangle

CurrentPage

The page currently being filled (null before the first element).

public PdfPage? CurrentPage { get; }

Property Value

PdfPage

CurrentY

Current vertical position (PDF y coordinate of the top of the free area).

public double CurrentY { get; }

Property Value

double

Document

The document the pages are added to.

public PdfDocument Document { get; }

Property Value

PdfDocument

Called for every page when the layout is closed (draw footers/page numbers here).

public Action<PageContext>? Footer { get; set; }

Property Value

Action<PageContext>

Header

Called for every page when the layout is closed (draw headers here).

public Action<PageContext>? Header { get; set; }

Property Value

Action<PageContext>

Margins

Page margins in points.

public Thickness Margins { get; set; }

Property Value

Thickness

PageSize

Size of pages created from now on.

public PdfRectangle PageSize { get; set; }

Property Value

PdfRectangle

PageStarted

Called when a new page is started (e.g. to draw a background or letterhead).

public Action<PageContext>? PageStarted { get; set; }

Property Value

Action<PageContext>

Pages

Pages created by this layout document.

public IReadOnlyList<PdfPage> Pages { get; }

Property Value

IReadOnlyList<PdfPage>

RemainingHeight

Remaining height on the current page.

public double RemainingHeight { get; }

Property Value

double

StructureParent

Structure element that receives the tags of added elements when the document is tagged (default: the root "Document" element). Set it to a "Sect" or "Div" element to build a deeper structure.

public PdfStructureElement? StructureParent { get; set; }

Property Value

PdfStructureElement

Methods

Add(LayoutElement)

Adds an element at the current position, continuing on new pages as needed.

public LayoutDocument Add(LayoutElement element)

Parameters

element LayoutElement

Returns

LayoutDocument

Add(string)

Adds text as a Paragraph with the default formatting.

public LayoutDocument Add(string text)

Parameters

text string

Returns

LayoutDocument

AddSpace(double)

Moves the cursor down by height points.

public LayoutDocument AddSpace(double height)

Parameters

height double

Returns

LayoutDocument

Close()

Finishes the current page and draws headers and footers.

public void Close()

Dispose()

Calls Close().

public void Dispose()

NewPage()

Starts a new page.

public PdfPage NewPage()

Returns

PdfPage