iTextSharp 异常:"Dimensions of a Cell can't be calculated."
iTextSharp Exception : "Dimensions of a Cell can't be calculated."
尝试 运行 使用 iTextSharp 库创建 Pdf 文档的简单示例(稍作修改)。获取异常 "Dimensions of a Cell can't be calculated."
valueCell 函数中的第 "cell.Left = 10f;" 行,抛出异常 "Dimensions of a Cell can't be calculated."。评论这一行一切正常。出现此异常的原因可能是什么?
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
class 被更强大的 PdfPTable
class 取代。我很确定它存在于较旧的系列中,但无论如何你总是想将它用于 table 相关的工作。
第三,如果将双列 table 的 "left side of the default cell" 设置为固定位置,第二列是否会位于第一列之上?有了这种理解,您可能不应该设置这些属性,这应该是有意义的,让 iText 为您处理。
尝试 运行 使用 iTextSharp 库创建 Pdf 文档的简单示例(稍作修改)。获取异常 "Dimensions of a Cell can't be calculated."
valueCell 函数中的第 "cell.Left = 10f;" 行,抛出异常 "Dimensions of a Cell can't be calculated."。评论这一行一切正常。出现此异常的原因可能是什么?
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
class 被更强大的 PdfPTable
class 取代。我很确定它存在于较旧的系列中,但无论如何你总是想将它用于 table 相关的工作。
第三,如果将双列 table 的 "left side of the default cell" 设置为固定位置,第二列是否会位于第一列之上?有了这种理解,您可能不应该设置这些属性,这应该是有意义的,让 iText 为您处理。