c# PDFSharp - Table.Clone 的意义何在

本文关键字:Clone PDFSharp Table | 更新日期: 2023-09-27 18:32:23

我正在使用PDFSharp生成一个PDF文档,该文档一遍又一遍地使用相同的(小)表格。

我看到可以克隆整个表。我制作了一个主表,其中包含我需要的所有核心设置和几行,认为我可以简单地在主表上使用 Table.Clone() 方法来带回一个漂亮的新表,可以中间化然后绘制到文档中。

克隆有效,但是当实际将表绘制到页面时,会抛出System.ArgumentFormat异常:

"Value of '0' is not valid for 'emSize'. 'emSize' should be greater than 0 and less than or equal to System.Single.MaxValue.'r'nParameter name: emSize"(在系统绘图中.dll)

似乎每个表都必须使用 page.AddTable() 方法添加到页面中。

如果是这种情况,如果无法将克隆的表绘制到页面,那么 Table.Clone() 的目的是什么?

这是一个大大简化的测试用例:

Table test_table = new Table();
test_table.Style = "Table";
test_table.Borders.Color = Colors.Black;
test_table.AddColumn(50);
test_table.AddColumn(50);
Row table_row_1 = test_table.AddRow();
table_row_1.Format.Font.Name = "Verdana";
table_row_1.Format.Font.Size = 8;
Row table_row_2 = test_table.AddRow();
table_row_2.Format.Font.Name = "Verdana";
table_row_2.Format.Font.Size = 8;
Table cloned_table = test_table.Clone();
cloned_table.Rows[0].Cells[0].AddParagraph("row 1 cell 1");
cloned_table.Rows[0].Cells[1].AddParagraph("row 1 cell 2");
cloned_table.Rows[1].Cells[0].AddParagraph("row 2 cell 1");
cloned_table.Rows[1].Cells[1].AddParagraph("row 2 cell 2");
test_table.SetEdge(0, 0, cloned_table.Columns.Count, cloned_table.Rows.Count, Edge.Box, BorderStyle.Single, 0.75, Colors.Black);
MigraDoc.Rendering.DocumentRenderer cloned_table_renderer = new DocumentRenderer(doc);
cloned_table_renderer.PrepareDocument();
cloned_table_renderer.RenderObject(gfx, 50, 50, 100, cloned_table);

c# PDFSharp - Table.Clone 的意义何在

添加到节的表具有父级。当您尝试再次添加表时,将出现异常。

使用 Clone() 您可以获得没有父级的表的副本,可以使用 Add(tableClone) 添加到该部分。

您不使用Add(),而是使用RenderObject()。我不知道为什么你的代码不起作用。

通常,您将表格添加到一个部分,并让 MigraDoc 为您创建文档 - 这样表格将在需要时自动跨多个页面中断。

异常问题尚未解决,但解释了克隆的目的。