Recursive AI

Scale PDF layout content to fit a page in C#

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

ScaleToFit() shrinks oversized blocks on a new PDF page. Blocks that fit stay at full size.

Terminal
dotnet add package RecursiveAI.OnePdf.Layout

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using OnePdf.Layout;
 
LayoutDocument.Compose(doc => doc.Page(p =>
{
    p.Size(300, 200);
    p.Margin(20);
    p.Content().ScaleToFit()
        .Width(320).Height(200)
        .Border(1).Padding(12)
        .Text("Warehouse inventory")
        .FontSize(20);
})).Save("fitted.pdf");
Console.WriteLine("Wrote fitted.pdf");

Unzip the example, then dotnet run.

Text
Wrote fitted.pdf

Open fitted.pdf to see the bordered inventory card on one page. The page is 300 × 200 points; with 20-point margins on each side, the card has 260 × 160 points to fit into. Its natural size is 320 × 200 points, so height determines how much it shrinks. If a block still won’t fit at the minimum scale on an empty page, layout throws PdfLayoutException.

Variations

Keep the card at full size when it fits. Change the card dimensions and leave the wrapper in place:

C#
.Width(240).Height(120)

Fit a smaller area than the page. Replace the line starting with p.Content() with:

C#
p.Content().Width(220).Height(130)
    .ScaleToFit()

The first width and height set the available box; the inner 320 × 200 dimensions still set the card’s natural size.

The short answer

Wrap the element in ScaleToFit() when composing a PDF with OnePDF Layout. Put the available dimensions outside the wrapper and the block’s own dimensions inside it. The wrapper scales the block uniformly, so a border, drawing, and type all shrink together. For the 320 × 200 point card in a 260 × 160 point area, the ratios are 260 ÷ 320 and 160 ÷ 200. The smaller ratio, 80%, is the one that fits both dimensions. An already fitting card stays at 100%.

How can I resize PDF content to fit the page?

While generating a PDF, set the paper size and margins first. Use the page’s Content() area, or give the wrapper an explicit smaller box if the block shares the page with other elements. The placement of Width and Height matters: setting those before ScaleToFit() sets the target box, while setting them after it gives the child its fixed dimensions. The wrapper transforms PDF drawing commands rather than turning them into an image, so text remains selectable and searchable. For content without a fixed width, OnePDF can lay the child out again as it tests smaller scales, so text may wrap differently.

This does not resize an existing PDF file. To change the paper size of an existing PDF, see Editing existing pages. Editing the page boxes changes the page boundaries without fitting the content to them. The page dimensions stay as specified in Size; only the block being composed changes. Likewise, changing a print dialog’s “Fit” setting affects printing, not the layout written by this program.

How do I fit an oversized block on one PDF page?

A fitted block has to be laid out without an internal page break. In flowing content, when it is too tall for the remaining space, OnePDF first tries it on the next page rather than squeezing it into the bottom of the current one. If the block needs scaling even on an empty page, the default lower bound is 5% of the original size. That is a fitting bound, not a legibility guarantee. For a long inventory table or a report people have to read on paper, allow the content to span pages instead of shrinking the entire report into a single block.

All onepdf recipes