iTextSharp无法循环工作

时间:2017-01-04 08:52:58

标签: c# asp.net itext

我有两段代码。第一个是工作,第二个是循环内部,它没有显示值。

任何人都有任何想法?如果memorystream无法读取,那么循环是否过快?

我正在写一切memorystream并回复下载文件。 如果我一个接一个地做,如下所示,一切正常。

        var phraseinvoice = new Phrase();
        phraseinvoice.Add(new Chunk("Invoice to:", FontFactory.GetFont(FontFactory.TIMES, 12)));              

        Invoicetable.AddCell(phraseinvoice);
        phraseinvoice = new Phrase();
        phraseinvoice.Add(new Chunk("BCD Meetings & Events Asia Pacific", FontFactory.GetFont(FontFactory.TIMES_BOLD, 12)));
        PdfPCell inheader = new PdfPCell(phraseinvoice);
        inheader.PaddingBottom = 4;
        inheader.Border = Rectangle.NO_BORDER;
        inheader.FixedHeight=20f;
        Invoicetable.AddCell(inheader);

如果我将它们放在数组中并从for循环中读取,则PDF将不会显示任何文本。

 string[] tbText = {" ","Pte.Ltd"," ", "20 Anson Road, #06-01"," ", "Twenty Anson 079912","",
                "Singapore"," "," ","Tel", "1234567", "Fax","123"," "," ","Delivery to:", "BCD Meetings & Events Asia Pacific"," ",
            "Pte,Ltd"," ","20 Anson Road, #06-01"," ", "Twenty Anson 079912"," ","Singapore"};

  Invoicetable.AddCell(inheader);
  for (int i = 0; i < 25; i++)
  {
      var inputstring = tbText[i];
      phraseinvoice = new Phrase();
      phraseinvoice.Add(new Chunk(inputstring, FontFactory.GetFont(FontFactory.TIMES_BOLD, 12)));
      PdfPCell cellbox = new PdfPCell(phraseinvoice);
      cellbox = new PdfPCell(phraseinvoice);
      cellbox.Border = Rectangle.NO_BORDER;
      cellbox.Padding= -4;
      Invoicetable.AddCell(cellbox);
  }

您可以看到-4填充后的第一个图像与没有填充的第二个图像之间的差异 before padding

after padding

2 个答案:

答案 0 :(得分:2)

您正在创建一个带有12pt字体文本的Phrase,但您将单元格的高度限制为10pt。这就解释了为什么没有显示出来。

更改您的代码:

for (int i = 0; i < 25; i++)
{
    var inputstring = tbText[i];
    phraseinvoice = new Phrase(inputstring,
        FontFactory.GetFont(FontFactory.TIMES_BOLD, 12)));
    PdfPCell cellbox = new PdfPCell(phraseinvoice);
    cellbox.Border = Rectangle.NO_BORDER;
    cellbox.FixedHeight = 20f;
    Invoicetable.AddCell(cellbox);
}

20个用户单位应足以显示12pt字体的文本。

<强>更新

另一种选择是缩小字体大小,例如:

for (int i = 0; i < 25; i++)
{
    var inputstring = tbText[i];
    phraseinvoice = new Phrase(inputstring,
        FontFactory.GetFont(FontFactory.TIMES_BOLD, 8)));
    PdfPCell cellbox = new PdfPCell(phraseinvoice);
    cellbox.Border = Rectangle.NO_BORDER;
    cellbox.FixedHeight = 15f;
    Invoicetable.AddCell(cellbox);
}

但还有更多:PhrasePdfPCell所需的身高取决于:

  • 字体大小
  • 领先,
  • 字体的上升和下降。

例如:

for (int i = 0; i < 25; i++)
{
    var inputstring = tbText[i];
    phraseinvoice = new Phrase(inputstring,
        FontFactory.GetFont(FontFactory.TIMES_BOLD, 12)));
    PdfPCell cellbox = new PdfPCell(phraseinvoice);
    cellbox.Leading = 14;
    cellbox.UseAscender = true;
    cellbox.UseDescender = true;
    cellbox.Border = Rectangle.NO_BORDER;
    cellbox.FixedHeight = 18f;
    Invoicetable.AddCell(cellbox);
}

请注意我们如何将Leading从默认值(字体大小的1.5倍)缩减为14,以及我们如何告诉cellBox采用 ascender descender

答案 1 :(得分:0)

看起来您正在尝试将字符串添加为pdfpcell

  

var inputstring = tbText [i];

Invoicetable.AddCell(inputstring);

tbText是一个字符串数组