Class LayoutDocument
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
documentPdfDocumentThe document to add pages to.
pageSizePdfRectangle?Size of new pages (default A4).
marginsThickness?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
ContentArea
public PdfRectangle ContentArea { get; }
Property Value
CurrentPage
The page currently being filled (null before the first element).
public PdfPage? CurrentPage { get; }
Property Value
CurrentY
Current vertical position (PDF y coordinate of the top of the free area).
public double CurrentY { get; }
Property Value
Document
The document the pages are added to.
public PdfDocument Document { get; }
Property Value
Footer
Called for every page when the layout is closed (draw footers/page numbers here).
public Action<PageContext>? Footer { get; set; }
Property Value
Header
Called for every page when the layout is closed (draw headers here).
public Action<PageContext>? Header { get; set; }
Property Value
Margins
Page margins in points.
public Thickness Margins { get; set; }
Property Value
PageSize
Size of pages created from now on.
public PdfRectangle PageSize { get; set; }
Property Value
PageStarted
Called when a new page is started (e.g. to draw a background or letterhead).
public Action<PageContext>? PageStarted { get; set; }
Property Value
Pages
Pages created by this layout document.
public IReadOnlyList<PdfPage> Pages { get; }
Property Value
RemainingHeight
Remaining height on the current page.
public double RemainingHeight { get; }
Property Value
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
Methods
Add(LayoutElement)
Adds an element at the current position, continuing on new pages as needed.
public LayoutDocument Add(LayoutElement element)
Parameters
elementLayoutElement
Returns
Add(string)
Adds text as a Paragraph with the default formatting.
public LayoutDocument Add(string text)
Parameters
textstring
Returns
AddSpace(double)
Moves the cursor down by height points.
public LayoutDocument AddSpace(double height)
Parameters
heightdouble
Returns
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()