【问题标题】:How to set the cell width in itextsharp pdf creation如何在itextsharp pdf创建中设置单元格宽度
【发布时间】:2012-03-01 18:47:46
【问题描述】:

如何使用 c# 在 itextsharp pdf 单元格认证中设置单元格宽度和高度。我只是用

cell.width = 200f;

但它应该显示错误消息。

无法设置宽度。

我该怎么办?..

【问题讨论】:

    标签: c# asp.net itextsharp


    【解决方案1】:

    您不设置单元格的宽度。

    您应该设置列的宽度。您可以通过将它们应用于表格对象来做到这一点:

    float[] widths = new float[] { 1f, 2f };
    table.SetWidths(widths);
    

    Neha 的回答是设置表格对象的宽度

    更多参考资料在这里:http://www.mikesdotnetting.com/Article/86/iTextSharp-Introducing-Tables

    【讨论】:

      【解决方案2】:

      http://indaravind.blogspot.in/2009/02/itextsharp-table-column-width.html

      VB:

      Dim intTblWidth() As Integer = {12, 10, 26, 10}
      

      C#:

      int[] intTblWidth = { 12, 10, 26, 10 };
      

      【讨论】:

      • 大声笑,那是VB,问题是C#
      • 逻辑是一样的..他基本上必须定义单元格宽度
      【解决方案3】:
      int  count=Gridview1.Columns.Count
      PdfPTable table = new PdfPTable(count);
      float[] columnWidths = new float[count];
      for (int v = 0; v < count; v++)
      {
          if (v == 0) { 
          columnWidths[v] = 10f;
          }
          else if (v == 2)
          {
              columnWidths[v] = 30f;
          }
          else if(v == 3)
          {
              columnWidths[v] = 15f;
          }
          else if(v == 4)
          {
              columnWidths[v] = 18f;
          }
          else if(v == 5|| v == 6|| v == 7)
          {
              columnWidths[v] = 22f;
          }
          else
          {
              columnWidths[v] = 20f;
          }
      }
      
      table.SetWidths(columnWidths);
      

      【讨论】:

      • 虽然这可能是也可能不是答案,但解释一下您在这里所做的事情会很有帮助,例如,您不知从何而来的所有这些随意数字是什么。你有没有用于此的资源?也许您可以链接到它以提供更多帮助?
      • 我使用 c# 在 itextsharp pdf 单元格认证中设置列宽。我只是使用上面的代码它对动态列很有帮助
      【解决方案4】:
      #region Name..!!
      
                  PdfPTable tblName = new PdfPTable(3);
                  tblName.WidthPercentage = 98f;
                  float[] colWidthsaccing4 = { 100, 500, 700 };
                  tblName.SetWidths(colWidthsaccing4);
                  PdfPCell celladdingo4;
                  celladdingo4 = new PdfPCell(new Phrase("  ", Smallspace));
                  celladdingo4.HorizontalAlignment = 1;
                  celladdingo4.BorderWidth = 0;
                  celladdingo4.Colspan = 2;
                  tblHeader6.AddCell(celladdingo4);
      
                  celladdingo4 = new PdfPCell(new Phrase("1.", TableFontmini_ARBold8Nor));
                  celladdingo4.HorizontalAlignment = 1;
                  
                  celladdingo4.PaddingBottom = 5f;
                  celladdingo4.BorderWidth = 0.5f;
                  tblName.AddCell(celladdingo4);
      
                  celladdingo4 = new PdfPCell(new Phrase("  Name :", TableFontmini_ARBold8Nor));
                  celladdingo4.HorizontalAlignment = 0;
                  celladdingo4.PaddingBottom = 5f;
                  celladdingo4.BorderWidth = 0.5f;
                  tblName.AddCell(celladdingo4);
      
                  celladdingo4 = new PdfPCell(new Phrase(" " +dt.Rows[0]["EmpName"].ToString(), TableFontmini_ARBold8Nor));
                  celladdingo4.HorizontalAlignment = 0;
                  celladdingo4.PaddingBottom = 5f;
                  celladdingo4.BorderWidth = 0.5f;
                  tblName.AddCell(celladdingo4);
      
                  celladdingo4 = new PdfPCell(new Phrase("  ", Smallspace));
                  celladdingo4.HorizontalAlignment = 1;
                  celladdingo4.BorderWidth = 0;
                  celladdingo4.Colspan = 2;
                  tblHeader6.AddCell(celladdingo4);
      
                  celladdingo4 = new PdfPCell(new Phrase("  ", Smallspace));
                  celladdingo4.HorizontalAlignment = 1;
                  celladdingo4.BorderWidth = 0;
                  celladdingo4.Colspan = 2;
                  tblHeader6.AddCell(celladdingo4);
      
                  celladdingo4 = new PdfPCell(new Phrase("  ", Smallspace));
                  celladdingo4.HorizontalAlignment = 1;
                  celladdingo4.BorderWidth = 0;
                  celladdingo4.Colspan = 2;
                  
                  tblHeader6.AddCell(celladdingo4);
      
      
                  doc.Add(tblName);
      
                  #endregion
      

      【讨论】:

      • 设置单个单元格的宽度和高度是否需要所有这些代码?
      【解决方案5】:

      cell.width = 200f;你必须在宽度上加上大写W,正确的是cell.Width = 200f;

      【讨论】:

      • 不允许在单元格上设置宽度
      • 没有设置单元格宽度的选项。
      猜你喜欢
      • 2014-08-08
      • 2014-02-10
      • 2016-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-14
      • 2017-08-20
      • 1970-01-01
      相关资源
      最近更新 更多