You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET下使用iTextSharp生成PDF表格时如何设置不同单元格宽度

How to Set Custom Cell/Column Widths with iTextSharp PDF Tables

Hey there! I’ve messed around with iTextSharp a lot for PDF generation, so let’s break down exactly how to get those custom column widths you need (matching your reference image).

Two Main Ways to Set Column Widths

iTextSharp lets you define widths either by proportion (relative to the total table width) or absolute values. Here’s how both work:

1. Proportional Widths (Most Common)

This is perfect if you want columns to scale with the page or available space. Just define a float array where each value represents the ratio (or percentage) of the total width each column should take.

// Create a table with 3 columns
PdfPTable table = new PdfPTable(3);

// Set proportional widths (sum doesn't have to be 100, but it makes calculations easier)
// Example: Column 1 = 20%, Column 2 = 50%, Column 3 = 30%
table.Widths = new float[] { 20f, 50f, 30f };

// Add your cells as usual
table.AddCell(new PdfPCell(new Phrase("Column 1")));
table.AddCell(new PdfPCell(new Phrase("Column 2 (Widest)")));
table.AddCell(new PdfPCell(new Phrase("Column 3")));

iTextSharp will automatically calculate the actual width based on the total available space for the table.

2. Absolute Widths (Fixed Size)

If you need exact, fixed widths (in points—1 inch = 72 points), use this method. You’ll set the total table width first, define each column’s fixed width, and lock the table to prevent auto-scaling.

// Create a 3-column table
PdfPTable table = new PdfPTable(3);

// Set the total fixed width of the table
table.TotalWidth = 500f;

// Define exact widths for each column (sum must equal TotalWidth)
table.Widths = new float[] { 100f, 250f, 150f };

// Lock the width to enforce your settings
table.LockedWidth = true;

// Add cells
table.AddCell(new PdfPCell(new Phrase("100pt Wide")));
table.AddCell(new PdfPCell(new Phrase("250pt Wide")));
table.AddCell(new PdfPCell(new Phrase("150pt Wide")));

Bonus: Adjusting Individual Cells (Column Spanning)

If you need a single cell to span multiple columns (taking up the combined width of those columns), use the Colspan property:

PdfPCell fullWidthCell = new PdfPCell(new Phrase("This cell spans all 3 columns"));
fullWidthCell.Colspan = 3;
table.AddCell(fullWidthCell);

This cell will automatically use the total width of all 3 columns you defined earlier.

Key Notes

  • Always set your column widths before adding cells to the table—otherwise, your settings might not apply.
  • For proportional widths, you don’t need to set TotalWidth or LockedWidth; iTextSharp handles scaling automatically.
  • When using absolute widths, double-check that the sum of your Widths array equals the TotalWidth to avoid unexpected layout issues.

内容的提问来源于stack exchange,提问作者Rajasree Jakkampudi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 03:55:05