Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Some links on this page are affiliate links: if you buy through them we may earn a commission, at no extra cost to you.

The right way to fit content in an iTextSharp PDF depends on what is overflowing. For a table, set its width before measuring its height; then either give it a page tall enough to preserve its size, scale it to a fixed page, or let it continue onto another page. For text in a fixed rectangle, use text-layout APIs such as ColumnText or FitText. Clipping is only appropriate when hiding some content is acceptable.

The examples below concern iText 5/iTextSharp. The official iText examples often show Java; the C# snippets here use corresponding iTextSharp-style APIs. Verify signatures against the version referenced by your project.

First identify what must fit

“Fit the PDF content on a page” can mean several different things, and they do not have the same solution. Decide which requirement is fixed before changing dimensions or font sizes:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • A table must fit on one page, but the page can change size: measure the table at its intended width and make a custom page tall enough.
  • The page must remain a standard size and the whole table must stay on one page: render the table to a template and scale that template down to the available rectangle.
  • A text block must fit a fixed box: fit or lay out the text in that rectangle, then choose what to do if it does not fit.
  • All content must be complete and readable: paginate rather than shrinking it without limit or clipping it.
  • A cell may show only a shortened value: use deliberate clipping or truncation and accept that some content will not appear.

These approaches solve different constraints. Scaling an object makes it smaller; changing the page makes the page larger; pagination preserves the content at its normal scale.

Measure a table correctly before choosing a page

A PdfPTable’s height depends on its width, because narrower columns wrap their contents onto more lines. In iTextSharp, set the table’s total width before reading TotalHeight. The table implementation returns zero for TotalHeight when no total width has been set. The iText 5 Knowledge Base demonstrates setting TotalWidth and LockedWidth before measuring: how to define page size based on content and the iTextSharp PdfPTable source.

var table = new PdfPTable(3);
// Add cells before measuring, so the table contains the content to lay out.
table.AddCell("Name");
table.AddCell("Description");
table.AddCell("Status");
// Add the remaining rows here.

table.TotalWidth = 400f; // Example width in user units, not a universal value.
table.LockedWidth = true;
float tableHeight = table.TotalHeight;

The example width is not a recommendation for every document. Choose a width that fits the usable page area and the intended column proportions. iText’s page-size guidance describes dimensions in user units; one user unit equals one point by default. In a coordinate-based placement call, also account for the page margins and the origin of the coordinate system.

Make a custom-height page when scale should stay unchanged

If the width is suitable but the table is too tall for a standard page, calculate a page height from the measured table height plus the space you want above and below it. Then place the table on the new page with WriteSelectedRows (the C# spelling of Java’s writeSelectedRows()). This keeps the table at its original scale instead of shrinking its type.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
float tableWidth = 400f;
table.TotalWidth = tableWidth;
table.LockedWidth = true;
float tableHeight = table.TotalHeight;

float leftMargin = 36f;
float rightMargin = 36f;
float topMargin = 36f;
float bottomMargin = 36f;

Rectangle pageSize = new Rectangle(
    tableWidth + leftMargin + rightMargin,
    tableHeight + topMargin + bottomMargin);

using (var stream = new FileStream("table.pdf", FileMode.Create))
using (var document = new Document(pageSize, leftMargin, rightMargin,
                                   topMargin, bottomMargin))
{
    var writer = PdfWriter.GetInstance(document, stream);
    document.Open();
    table.WriteSelectedRows(0, -1, leftMargin,
                            pageSize.Height - topMargin,
                            writer.DirectContent);
    document.Close();
}

Here 0, -1 selects all rows. The starting Y coordinate is measured down from the top in this placement pattern; begin at page height minus the top margin so the table starts inside the intended area. Confirm the final table height after all cells and content have been added. If the table content changes after measuring, recalculate the page dimensions.

Keep the distinction between width and height clear

A table can be too wide, too tall, or both. The custom-page pattern above gives the page a width based on the table width and margins; it does not make an over-wide table fit a fixed-width sheet. Reduce column widths, wrap content, or select a wider page if width is the limiting constraint. For a fixed standard page, use a scaling strategy only if shrinking the complete table remains legible.

Scale a whole table onto a fixed-size page

When a standard page size is mandatory and a single table must appear on one page, iText 5 documents rendering the table to a PdfTemplate, wrapping the template as an Image, and calling ScaleToFit. This scales the table as a whole, including its text; it does not reflow the cells at a different font size. See iText’s PdfPTable resizing example.

float tableWidth = 500f;
table.TotalWidth = tableWidth;
table.LockedWidth = true;
float tableHeight = table.TotalHeight;

PdfTemplate template = writer.DirectContent.CreateTemplate(
    tableWidth, tableHeight);
table.WriteSelectedRows(0, -1, 0, tableHeight, template);

Image tableImage = Image.GetInstance(template);
tableImage.ScaleToFit(usableWidth, usableHeight);
tableImage.SetAbsolutePosition(leftMargin, bottomMargin);
writer.DirectContent.AddImage(tableImage);

This is the core of the documented pattern; it assumes writer is the open document’s PdfWriter, and that usableWidth, usableHeight, leftMargin, and bottomMargin have been calculated for the target page. Create the template only after the table has a known width and height. Place the scaled image within the page’s usable rectangle, not over the margins.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Inspect the resulting PDF at its intended viewing or print size. A table that technically fits may have type too small to read. If the minimum readable size cannot be maintained, use a larger page or split the table across pages instead.

Fit text in a fixed rectangle

For a text phrase or paragraph that must stay within a bounded rectangle, use text layout rather than drawing text directly and hoping it fits. iTextSharp’s FitText is described in the source as fitting text to a rectangle by adjusting font size. It can help when reducing type is an acceptable policy; if the required font size is too small, the content still needs another treatment. The source is PdfSignatureAppearance.cs.

ColumnText is useful when you want to lay out text in a rectangle and determine whether anything remains. The iText 5 positioning example uses a simulation pass and checks ColumnText.HasMoreText before continuing: positioning different text snippets on a page.

var column = new ColumnText(writer.DirectContent);
column.SetSimpleColumn(left, bottom, right, top);
column.AddText(new Phrase("Text to place in the rectangle", font));

int status = column.Go(true); // Simulate layout; do not draw.
bool hasOverflow = ColumnText.HasMoreText(status);

In the simulation call, Go(true) lays out the content without drawing it. If hasOverflow is true, the text did not fit the defined rectangle. You can then reduce the font, enlarge the rectangle, or write the remaining content in another column or page. If you perform a simulation and then draw, create a fresh ColumnText instance or reset the content and column state according to the API you use; do not assume the simulated pass has already produced visible text.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose an explicit overflow policy

Reduce type size only to a readable limit

Font fitting is appropriate when the content is short and the range of acceptable sizes is known, such as a short label in a fixed box. Establish a minimum readable size. If the text still overflows at that size, increase the available area or paginate it rather than letting an automatic fit produce illegible type.

Clip or truncate only when omission is acceptable

Clipping and truncation can be intentional for compact displays, but they do not fit all content: some of it becomes invisible. The iText 5 fit-text-in-cell examples illustrate cell-level approaches. Use this policy only when the reader does not need the full value, and consider an indication such as an ellipsis or a separate full-value field if the document design requires it.

Paginate to preserve complete content

For content that must remain complete and legible, allow it to continue. The iText positioning example checks whether ColumnText has more text and places the remaining content on a subsequent page. The same principle applies to tables: if forcing an entire long table onto one standard sheet makes it unreadable, let rows continue over pages rather than scaling without regard to legibility.

Common fitting problems and fixes

  • TotalHeight is zero: set TotalWidth first, and add the table’s cells before measuring. Lock the width when the table should retain that explicit width.
  • The measured height does not match the output: check that the measurement and output use the same width and content. A different width can change wrapping and therefore height.
  • The table overlaps a margin or is misplaced: calculate the usable page rectangle, then set the placement coordinates relative to the page dimensions and margins. WriteSelectedRows uses explicit coordinates rather than automatically laying out content inside a document flow.
  • The template does not contain the whole table: ensure its dimensions are based on the table’s measured width and height, and that the selected row range includes all rows. Confirm that the table was fully populated before measurement and rendering.
  • Text is missing from a cell: determine whether the chosen cell technique clips or truncates. If all text is required, use layout with overflow detection and continue it elsewhere.
  • Text fits but is hard to read: the page-fitting method may have reduced the whole table or font too far. Prefer a larger page or additional pages when readability is a requirement.
  • Content appears to fit only until it changes: content length, cell padding, font metrics, and column widths affect wrapping. Re-measure after data or layout changes; do not rely on a height measured for different input.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Performance, reliability, and cost considerations

These techniques change PDF layout; they do not guarantee that arbitrary content will remain readable on one page. Table measurement requires a known width, and the result is only meaningful for the table content and layout being measured. Simulation is useful for detecting overflow before drawing, while pagination avoids forcing content into a smaller rectangle. The cited iText documentation does not publish a topic-specific performance benchmark or success rate, so choose based on the document’s constraints rather than an assumed speed advantage.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Also distinguish the page canvas from an automatic fitting mechanism: iText’s PdfContentByte API describes it as an object containing user-positioned text and graphic content for a page. It is a drawing surface, not a global switch that resizes all document content. See the iText 5.5.13.6 PdfContentByte API.

Or skip the browser setup

If your task is to capture a web page as an image or PDF rather than lay out PDF content with iTextSharp, ScreenshotNeo offers a one-request screenshot API. It does not replace iTextSharp’s PDF layout APIs.

For a screenshot, replace the example URL with the page you want to capture:

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

See the ScreenshotNeo documentation for setup and available parameters. Cookie banners, popups, and chat widgets are removed before the shot; bot checks, blank pages, and failed loads are never billed. An MCP server lets AI agents take screenshots, and the free plan includes 1,000 screenshots a month with no card; paid plans start at $5 for 3,000. Sign up for 1,000 free screenshots a month, with no card.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Frequently Asked Questions

Which iTextSharp version do these examples target?

They target the iText 5/iTextSharp API family. Check the API available in your project before adapting a snippet.

Can iTextSharp guarantee that every table will fit legibly on one standard page?

No. If the content is too long for the available area, keeping it complete, readable, and on one standard page may not be possible at the same time.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.