1

Trying to run simple example(with slight modifications) of creating Pdf document using iTextSharp Library. Getting exception "Dimensions of a Cell can't be calculated."

Line "cell.Left = 10f;" in valueCell function, throws exception "Dimensions of a Cell can't be calculated.". Commenting this line everything works fine. What could be the reason for this exception?

        private static Cell valueCell(string content)
        {
            var cell = new Cell(content);

            cell.BackgroundColor = new iTextSharp.text.Color(SystemColor.AliceBlue);
            cell.Left = 10f;

            return cell;
        }

        private static void TestPdfExport_iTextSharp()
        {
            var filePath = string.Format("D:\\Temp\\GeneratedPdf_iTs_{0}.pdf", DateTime.Now.ToString("yy-MM-dd hh mm ss"));

            // step 1: creation of a document-object
            Document document = new Document();

            try
            {
                // step 2: we create a writer that listens to the document
                // and directs a PDF-stream to a file

                PdfWriter writer = PdfWriter.GetInstance(document,
                                   new FileStream(filePath, FileMode.Create));

                // step 3: we open the document
                document.Open();

                // step 4: we create a table and add it to the document
                Table aTable = new Table(2, 2);    // 2 rows, 2 columns
                aTable.DefaultCell.Left = 10f;
                aTable.DefaultCell.Bottom = 10f;

                aTable.AddCell(valueCell("Metric"));
                aTable.AddCell(valueCell("Current Value"));

                aTable.AddCell(valueCell("Leverage"));
                aTable.AddCell(valueCell("3.2"));

                document.Add(aTable);   
            }
            catch (DocumentException de)
            {

            }


            // step 5: we close the document
            document.Close();
        }

1 Answer 1

2

A couple of things.

First, you are using a Table which means you are probably using an old, obsolete and unsupported version of iTextSharp, probably 4.1.6. If so, you should upgrade to the most recent 5.x series for both compatibility and potentially legal reasons.

Second, the Table class was replaced with a much more powerful PdfPTable class. I'm pretty sure that that existed in the older series but regardless you'll always want to use that for table-related work.

Third, if you set the "left side of the default cell" of a two column table to a fixed location, would the second column just sit on top of the first column? With that understanding it should hopefully make sense that you probably shouldn't be setting these properties, just let iText take care of it for you.

3
  • 1
    1. I am using older version delibrately for legal issues. This older version is under LGPL Licence. Commented Sep 2, 2015 at 19:02
  • 2. I was using Left property for Padding, Replacing it with PdfTable fixed this as it has PaddingLeft property. Thanks. Commented Sep 2, 2015 at 19:03
  • Good to hear! As for LGPL I'd encourage you to read the fourth block here as you might now actually be covered
    – Chris Haas
    Commented Sep 2, 2015 at 20:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.