iTextSharp 例外 : "Dimensions of a Cell can't be calculated."



尝试运行使用iTextSharp库创建Pdf文档的简单示例(略有修改)。获取异常"无法计算单元格的尺寸"。

线"细胞。Left = 10f;"在valueCell函数中,抛出异常" Cell的尺寸无法计算"。注释这一行,一切正常。这个例外的原因是什么?

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

几件事

首先,你使用的是Table,这意味着你可能使用的是旧的、过时的、不受支持的iTextSharp版本,可能是4.1.6。如果是,您应该升级到最新的5。X系列的兼容性和潜在的法律原因

第二,Table级被更强大的PdfPTable级所取代。我很确定在旧的系列中存在,但无论如何,您总是希望在与表相关的工作中使用它。

第三,如果您将一个两列表的"默认单元格的左侧"设置为固定位置,那么第二列是否会位于第一列的顶部?有了这样的理解,你应该明白你可能不应该设置这些属性,只是让ittext为你照顾它。

相关内容

最新更新