---
title: "Stream large PDF creation in C#"
url: https://recursiveai.org/onepdf/cookbook/stream-large-pdf-creation/
description: "Stream large PDF creation in C#. Runnable .NET example with sample input, actual output and documented options."
version: "1.22.0"
---

# Stream large PDF creation 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) · [GitHub](https://github.com/recursive-ai-oss/onepdf-csharp) · [Docs](https://recursiveai.org/onepdf/docs/) · [Download example (.zip)](https://recursiveai.org/onepdf/cookbook/stream-large-pdf-creation/stream-large-pdf-creation.zip)

Write each PDF page to a stream as it is committed.

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

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

```csharp
using OnePdf;

using var file = File.Create("report.pdf");
using var writer =
    new PdfDocumentWriter(file);
var font =
    writer.AddStandardFont("Helvetica");
for (int i = 1; i <= 1000; i++)
{
    using var page =
        writer.BeginPage(612, 792);
    page.SetFont(font, 12);
    page.DrawText($"Page {i}", 50, 740);
    page.Commit();
}
writer.Finish();
```

Writes `report.pdf` with 1,000 numbered pages; prints nothing.

Unzip [the example](https://recursiveai.org/onepdf/cookbook/stream-large-pdf-creation/stream-large-pdf-creation.zip), then `dotnet run`.

## The full program

The full program closes the output before reopening it, then prints its page count and the text of its first and last pages. `Finish()` throws if a page, structure element or reserved form is still open; without it the output is incomplete.

```csharp
using OnePdf;

using (var file = File.Create("report.pdf"))
using (var writer = new PdfDocumentWriter(file))
{
    var font = writer.AddStandardFont("Helvetica");
    for (int i = 1; i <= 1000; i++)
    {
        using var page = writer.BeginPage(612, 792);
        page.SetFont(font, 12);
        page.DrawText($"Report page {i}", 50, 740);
        page.Commit();
    }
    writer.Finish();
}
using var doc = PdfDocument.Open("report.pdf");
Console.WriteLine($"Pages: {doc.Pages.Count}");
Console.WriteLine(doc.Pages[0].ExtractText().Text);
Console.WriteLine(doc.Pages[^1].ExtractText().Text);
```

```text
Pages: 1000
Report page 1
Report page 1000
```

## Variations

**Keep the destination stream open and fix the document ID.** Replace the writer declaration in the snippet at the top; it still writes `report.pdf` and prints nothing:

```csharp
// Replace the writer declaration in the snippet at the top.
using var writer = new PdfDocumentWriter(file,
    new PdfDocumentWriterOptions
    {
        LeaveOpen = true,
        DocumentId = new byte[16]
    });
```

## Related recipes

- [Create a PDF](https://recursiveai.org/onepdf/cookbook/create-pdf/)
- [Generate PDF tables](https://recursiveai.org/onepdf/cookbook/generate-tables/)

---

## The short answer

Use `PdfDocumentWriter` with a writable stream. Begin a page, draw its content, commit it, and then start the next page. Call `Finish()` after the last commit to write the document’s remaining resources and catalog. The full program creates 1,000 pages and reopens the finished file to read its page count and endpoint text.

This is page-at-a-time creation, not a way to edit an existing large PDF. You supply page dimensions, coordinates and content; the writer does not perform general document layout. Use the [content creation documentation](https://recursiveai.org/onepdf/docs/content-creation/) for the complete drawing and streaming API.

## How to generate a large PDF file?

Keep each page’s drawing inside the loop and commit it before beginning another. Only one page can be open at a time. A builder disposed without `Commit()` is dropped, except that a page with structure bound to it must be committed. This makes `using` useful for cleanup, but it is not a replacement for committing the page.

Create fonts through the writer, as the example does, rather than through an unrelated `PdfDocument`. Page content is written on commit. Fonts stay until `Finish()` so their subsets can include glyphs used by later pages. Page-tree entries, object offsets, bookmarks and named destinations also remain as bookkeeping. Streaming does not mean zero memory use or a fixed memory ceiling, and this example makes no memory-size claim.

Images, forms and ended structure elements are written when they become final. A form reserved for later content stays pending until it is filled. That supports details such as a total-page count that is unknown while earlier pages are being written, but every reserved form must be completed before finishing.

Stream lifetime is separate from page lifetime. By default, disposing the writer closes its output. Set `LeaveOpen = true` when the surrounding code owns the stream and needs to use it afterward. The variation also fixes the 16-byte document ID for reproducible output. The main program instead uses a file scope that ends before the file is reopened for reading, so no unfinished writer is left attached to it.

## How to reduce PDF file size in content stream?

The streaming writer’s `CompressStreams` option defaults to true. It compresses page content and other unfiltered streams as they are written. Keep that default unless you specifically need uncompressed content for inspection. Compression changes representation, not how much drawing your program requests; repeated or unnecessary content is still content that must be written.

The default cross-reference output is also compressed. A classic table is available through `XrefFormat = PdfXrefFormat.Table`, but object streams are not used on this path because objects are written as they become final. Do not confuse content-stream compression with buffering the entire PDF into a `MemoryStream`: the example writes directly to a file stream.

Encryption is not available through this writer. If the final PDF must be encrypted, the documented route is to save the result through `PdfDocument` afterward. That is a separate pass with different memory behavior, not an option that turns the page writer into an encrypted streaming output. Similarly, reopening the file in this sample is a readback step after creation, not part of its streaming loop.
