【问题标题】:iTextSharp table width 100% of pageiTextSharp 表格宽度为页面的 100%
【发布时间】:2010-12-01 15:43:37
【问题描述】:

我正在尝试使用 iTextSharp 将表格添加到文档中。这是一个例子:

Document document = new Document(PageSize.LETTER,72, 72, 72, 72);
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("C:\\test.pdf", FileMode.Create));

document.Open();
Table table = new Table ( 2, 1 );
table.Width = document.RightMargin - document.LeftMargin;

// Cell placeholder
Cell cell = new Cell ( new Paragraph ( "Some Text" ) );
table.AddCell ( cell );
cell = new Cell ( new Paragraph ( "More Text" ) );
table.AddCell ( cell );
document.Add ( table );
document.Close ( );

我正在设置表格的宽度,以便它可以扩展页面的边距。但是当创建 pdf 时,表格只占用了边距之间大约 80% 的空间。我在这里做错了吗?

【问题讨论】:

    标签: c# pdf itext width


    【解决方案1】:

    想通了。显然table.Width 是百分比而不是像素宽度。所以使用:

    table.Width = 100;
    

    工作就像一个魅力。

    【讨论】:

    • 我没有选择宽度,所以我使用 table.WidthPercentage = 100f;
    【解决方案2】:

    在 iTextSharp 最新版本 (5.0.4) 中,PdfPTable 具有 WidthPercentage 属性。

    要设置静态值,属性是TotalWidth

    【讨论】:

    • [5.5.9] TotalWidth 不是静态值 - 它只是对象属性。
    【解决方案3】:

    用户还可以按百分比设置表格宽度。

    t.WidthPercentage = 100f;
    

    【讨论】:

      【解决方案4】:

      WidthPercentage 属性在 iText7 中不再可用。请改用以下内容

      table.SetWidth(new UnitValue(UnitValue.PERCENT, 100));
      

      【讨论】:

        【解决方案5】:

        在Java中table.setWidthPercentage(100); 适用于 5.5.8 版本

        【讨论】:

        • 问题和其他答案集中在 c# 语法中的 .Net 的 itext 上。您在 java 语法中使用 itext for java。这就是所有的区别。
        【解决方案6】:

        在 c# 中为 itext7

        Table details = new Table(4, false).UseAllAvailableWidth();
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-08-14
          • 2012-02-18
          • 1970-01-01
          • 1970-01-01
          • 2011-03-03
          相关资源
          最近更新 更多