c# PDFSharp - what's the point of Table.Clone?

霸气de小男生 提交于 2019-12-08 04:00:31

问题


I am generating a PDF document using PDFSharp which uses the same (small) table over and over.

I saw that it is possible to clone an entire table. I made a master table with all the core settings I would need and a couple of rows, thinking i could simply use the Table.Clone() method on the master table to bring be back a nice new table that could be midified and then drawn to the document.

Cloning works, but when it comes to actually draw the table to the page, an System.ArgumentFormat exception is thrown:

"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" (in System.Drawing.dll)

It seems every table has to be added to the page using the page.AddTable() method.

If that's the case, what is the purpose of Table.Clone() if its impossible to then draw the cloned table to a page?

Here is a greatly simplified test case:

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

回答1:


A table that was added to a section has a parent. When you try to add the table again, you will get an exception.

Using Clone() you get a copy of the table that has no parent and can be added to the section using Add(tableClone).

You do not use Add(), instead you use RenderObject(). I don't know why your code isn't working.

Typically you add the table to a section and have MigraDoc create the document for you - this way the table will automatically break across multiple pages when needed.

Exception problem not yet solved, but purpose of Clone is explained.



来源:https://stackoverflow.com/questions/27170832/c-sharp-pdfsharp-whats-the-point-of-table-clone

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!