MVC newbie
MVC newbie

Reputation: 599

iTextSharp not working in loop

I have 2 pieces of code. The first one is working, second one is inside a loop and it doesn't show the value.

Anyone have any idea? Could it be looping is too fast for memorystream to read?

I am writing everything to a memorystream and response to download the file. If I do it one by one as below, everything works fine.

        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);

If I put them inside a array and read from a for loop the PDF will not show any text.

 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);
  }

You can see the differences between first image after -4 padding and second image without padding before padding

after padding

Upvotes: 0

Views: 617

Answers (2)

Bruno Lowagie
Bruno Lowagie

Reputation: 77528

You are creating a Phrase with text in a 12pt font, but you are limiting the height of the cell to 10pt. That explains why nothing is shown.

Change your code like this:

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 user units should be sufficient to show text in a 12pt font.

Update:

Another option would be to reduce the font size, for instance:

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);
}

But there's more: the height needed by a Phrase in a PdfPCell depends on:

  • The font size,
  • The leading,
  • The ascender and descender of the font.

For instance:

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);
}

Note how we reduced the Leading from the default (1.5 times the font size) to 14, and how we told the cellBox to take the ascender and descender into account.

Upvotes: 2

user7373379
user7373379

Reputation: 98

Looks like you are trying to add a string as a pdfpcell

var inputstring = tbText[i];

Invoicetable.AddCell(inputstring);

tbText is a string array

Upvotes: 0

Related Questions