如何使用ItextSharp / C#设置表的位置?

时间:2013-03-22 19:34:16

标签: c# pdf position itext

我正在编写一个Windows窗体代码,用于为我的客户提供优惠。我想要它制作pdf文件。 我知道如何定位文字。我知道如何创建一个表,并用我的datagridview中的内容填充它。

但是如何将此表垂直放置在页面中间(不是水平对齐 - 我知道这个),还是在任何预定位置?以下是我的代码。结果是一个表直接位于页面顶部,即使我将一些文本放在页面中间的SetTextMatrix somwhere中。我应该制作一些大而透明的行吗?

PdfPTable tabela = new PdfPTable(dataGridView1.Columns.Count);

    float[] szerokości = new float[] 
    {
      dataGridView1.Columns[0].Width,
      dataGridView1.Columns[1].Width,
      dataGridView1.Columns[2].Width,
      dataGridView1.Columns[3].Width,
      dataGridView1.Columns[4].Width,
      dataGridView1.Columns[5].Width,
      dataGridView1.Columns[6].Width
    };

    tabela.SetWidths(szerokości);
    tabela.HorizontalAlignment = 1; // 0 - lewo, 1 - środek , 2 - prawo
    tabela.SpacingBefore = 10.0F;

    PdfPCell komorka = null;

    foreach (DataGridViewColumn c in dataGridView1.Columns)
    {
      komorka = new PdfPCell(new Phrase(new Chunk(c.HeaderText, czcionkaNaglowek)));
      komorka.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
      komorka.VerticalAlignment = PdfPCell.ALIGN_CENTER;
      tabela.AddCell(komorka);
    }

    if (dataGridView1.Rows.Count > 0)
    {
      for (int i = 0; i < dataGridView1.Rows.Count; i++)
      {
        for (int j = 0; j < dataGridView1.Columns.Count; j++)
        {
          komorka = new PdfPCell(new Phrase(dataGridView1.Rows[i].Cells[j].Value.ToString(), czcionkaTabela));
          komorka.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
          komorka.VerticalAlignment = PdfPCell.ALIGN_CENTER;
          tabela.AddCell(komorka);
        }

      }

    }

    oferta.Add(tabela);
    oferta.Close();
  }
  catch (DocumentException dex)
  {
    MessageBox.Show(dex.ToString());
  }
  catch (Exception ex)
  {
    MessageBox.Show(ex.ToString());
  }

0 个答案:

没有答案