首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将动态PdfPCell宽度分配给动态生成的PdfPCell

将动态PdfPCell宽度分配给动态生成的PdfPCell
EN

Stack Overflow用户
提问于 2016-02-22 10:06:20
回答 1查看 2.2K关注 0票数 0

我使用的是iTextSharp版本5.4.5.0。

我正在尝试用多个PdfPTable打印PdfPCell。PdfPCell的数量将是动态的。那么,如何为动态生成的PdfPCell分配宽度呢?

我知道如何为单元格的静态和固定数量分配宽度。但是对于动态单元格,我如何为每个动态生成的单元分配宽度?PdfPCell的数量不是固定的。

请帮帮我?

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2016-02-22 15:46:43

即使在对原来的问题作了反复的评论之后,我也不能完全确定我是否正确地理解了这个问题,但让我们试试:

因此,让我们假设您事先不知道列的数量,而是需要获取第一行的单元格来了解列的数量及其宽度。在这种情况下,您可以简单地执行以下操作:

代码语言:javascript
运行
复制
public void CreatePdfWithDynamicTable()
{
    using (FileStream output = new FileStream(@"test-results\content\dynamicTable.pdf", FileMode.Create, FileAccess.Write))
    using (Document document = new Document(PageSize.A4))
    {
        PdfWriter writer = PdfWriter.GetInstance(document, output);
        document.Open();

        PdfPTable table = null;
        List<PdfPCell> cells = new List<PdfPCell>();
        List<float> widths = new List<float>();
        for (int row = 1; row < 10; row++)
        {
            // retrieve the cells of the next row and put them into the list "cells"
            ...
            // if this is the first row, determine the widths of these cells and put them into the list "widths"
            ...
            // Now create the table (if it is not yet created)
            if (table == null)
            {
                table = new PdfPTable(widths.Count);
                table.SetWidths(widths.ToArray());
            }
            // Fill the table row
            foreach (PdfPCell cell in cells)
                table.AddCell(cell);
            cells.Clear();
        }

        document.Add(table);
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35550776

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档