---
title: "Generate a PDF report with page totals in C#"
url: https://recursiveai.org/onepdf/cookbook/generate-pdf-report-with-page-totals/
description: "Generate a multipage PDF report in C# with OnePDF Layout. Let payments flow across pages and print a running total in each footer. Includes runnable code."
version: "1.25.1"
---

# Generate a PDF report with page totals in C#

**OnePDF** is a free, open-source PDF library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OnePdf.Layout) · [GitHub](https://github.com/recursive-ai-oss/onepdf-csharp) · [Docs](https://recursiveai.org/onepdf/docs/) · [Download example (.zip)](https://recursiveai.org/onepdf/cookbook/generate-pdf-report-with-page-totals/generate-pdf-report-with-page-totals.zip)

Show the total paid so far in each page footer.

```shell
dotnet add package RecursiveAI.OnePdf.Layout
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using OnePdf.Layout;

LayoutDocument.Compose(doc => doc.Page(p =>
{
    p.Size(240, 140);
    p.Footer().DynamicText(v =>
        $"Total paid: {v.Total("paid"):0.00}");
    decimal[] payments = [12.50m, 7.25m];
    p.Content().Column(col =>
    {
        foreach (var amount in payments)
            col.Item().Padding(16)
                .Accumulate("paid", amount)
                .Text($"Payment: {amount}");
    });
})).Save("payments.pdf");
Console.WriteLine("Wrote payments.pdf");
```

Unzip [the example](https://recursiveai.org/onepdf/cookbook/generate-pdf-report-with-page-totals/generate-pdf-report-with-page-totals.zip), then `dotnet run`.

Writes `payments.pdf` with one payment per page and running totals of 12.50 and 19.75. The full program below prints the totals from its PDF. No input file is needed.

```text
Wrote payments.pdf
```

## The full program

This version uses four sample payments, in USD, and makes a two-page statement. Each header shows the balance from the previous page, and each footer shows the updated balance. The program reopens the PDF and prints the text from both pages using the core `RecursiveAI.OnePdf` package, which Layout installs as a dependency.

```csharp
using System.Globalization;
using OnePdf;
using OnePdf.Layout;
using OnePdf.Text;

// Sample payments in report order, in USD.
decimal[] payments = [12.50m, 7.25m, 30m, 5.25m];
const string account = "paid";

LayoutDocument.Compose(doc => doc.Page(page =>
{
    page.Size(240, 125);
    page.Margin(20);
    page.Header().DynamicText(v =>
        $"Brought forward: {Usd(v.BroughtForward(account))}");
    page.Content().Column(col =>
    {
        col.Spacing(8);
        foreach (decimal amount in payments)
            col.Item().Padding(4)
                .Accumulate(account, amount).Text($"Payment: {Usd(amount)}");
    });
    page.Footer().DynamicText(v =>
        $"Carried forward: {Usd(v.Total(account))}").Bold();
})).Save("payments.pdf");

using var pdf = PdfDocument.Open("payments.pdf");
Console.WriteLine($"payments.pdf: {pdf.PageCount} pages");
foreach (var page in pdf.Pages)
{
    Console.WriteLine();
    string text = page.ExtractText().GetText(PdfTextMode.ReadingOrder);
    foreach (string line in text.Split('\n', StringSplitOptions.RemoveEmptyEntries))
        Console.WriteLine(line.Trim());
}

static string Usd(decimal amount) =>
    amount.ToString("C2", CultureInfo.GetCultureInfo("en-US"));
```

```text
payments.pdf: 2 pages

Brought forward: $0.00
Payment: $12.50
Payment: $7.25
Carried forward: $19.75

Brought forward: $19.75
Payment: $30.00
Payment: $5.25
Carried forward: $55.00
```

Use the same account name in `Accumulate` and `DynamicText`. If a sum exceeds the range of `decimal`, OnePDF throws `PdfLayoutException`.

## Variations

In the full program, replace the footer with a subtotal for just the payments on that page:

```csharp
page.Footer().DynamicText(v =>
    $"This page: {Usd(v.Sum(account))}");
```

Or show both figures on the same line:

```csharp
page.Footer().DynamicText(v =>
    $"Page: {Usd(v.Sum(account))} | To date: {Usd(v.Total(account))}");
```

## Related recipes

- [Generate a PDF table in C#](https://recursiveai.org/onepdf/cookbook/generate-tables/)
- [Add page numbers to a PDF in C#](https://recursiveai.org/onepdf/cookbook/add-page-n-of-m-numbers/)

## The short answer

To generate a PDF report in C#, pass your data to `LayoutDocument.Compose` and save the result as a PDF. For a running total, attach each amount to its printed entry with `Accumulate` and display `Total` in a `DynamicText` footer. OnePDF adds the amount on the page where its entry starts, so the total reflects what actually fit, not a guess at how many entries would fit.

## How do I generate a multipage PDF report in C#?

Write the entries in report order in `page.Content()`. The `Column` in this recipe flows onto another page when it runs out of room. The example uses short pages so four entries are enough to show the carryover. For a printed report, choose a paper size and set the margins before laying out the content. The number of entries per page can change when the page size, font size, or row height changes. The footer calculation doesn’t need to change with it.

Keep the amount next to the entry it describes in the layout. With a table, put `Accumulate` on the amount cell and keep the row together if a description can wrap across a page break. If a row moves to the next page, its amount moves with it. Accumulating on both the description and the amount would add the value twice.

## How do I show subtotals and running totals in a PDF report?

Use `Sum` for a page subtotal, `Total` for the cumulative amount through the current page, and `BroughtForward` for the amount through the previous page. On the second page in this example, those three figures are $35.25, $55.00, and $19.75. Showing both the brought-forward and carried-forward figures lets the reader reconcile the page with its neighbors.

Accounts are separate named sums, not printed labels. Use one name for each figure you want to track independently, such as net sales and tax. A total continues across sections if you keep the same account name. Start a new account when the next department or statement needs its own balance. For money, keep the input amounts as `decimal` and format them for the reader in the `DynamicText` callback. The example fixes the culture to `en-US` so the displayed amounts stay in US dollar format regardless of where the program runs. See [Values that depend on the page](https://recursiveai.org/onepdf/docs/layout/#values-that-depend-on-the-page).
