有没有办法在MigraDoc中为表格添加边框并隐藏单元格边框?
边框的默认宽度为0,不可见。要启用边框,请设置大于0的值。
如果table
是您的Table对象,则可以编写table.Borders.Width = 0.5;
您可以为表格和每个单元格设置边框。单元格从表格、列、行继承边框属性,除非在较低级别上被覆盖。
还要检查Table
类的SetEdge
方法。
这里讨论的示例代码:
http://www.pdfsharp.net/wiki/Invoice-sample.ashx
我的测试代码:
private static void TabelWithBorderTest()
{
var document = new Document();
// Add a section to the document.
var section = document.AddSection();
Table table = section.AddTable();
table.Borders.Width = 0.25;
table.Rows.LeftIndent = 0;
// Before you can add a row, you must define the columns
Column column = table.AddColumn("7cm");
column.Format.Alignment = ParagraphAlignment.Left;
Row row = table.AddRow();
row.Cells[0].AddParagraph("Text in table");
// Create a renderer for the MigraDoc document.
var pdfRenderer = new PdfDocumentRenderer(false) { Document = document };
// Associate the MigraDoc document with a renderer.
// Layout and render document to PDF.
pdfRenderer.RenderDocument();
// Save the document...
const string filename = "TableTest.pdf";
pdfRenderer.PdfDocument.Save(filename);
// ...and start a viewer.
Process.Start(filename);
}
var document = new Document();
var page = document.AddSection();
Table table = page.AddTable();
table.Borders.Visible = true;
Column col = table.AddColumn("3cm");
col = table.AddColumn("10cm");
col = table.AddColumn("3cm");
col.Format.Alignment = ParagraphAlignment.Left;
Row row = table.AddRow();
Paragraph p = row.Cells[0].AddParagraph();
p.AddFormattedText("Top header row");
row.Cells[0].MergeRight = 2;
// then set it in visible as false like this, you can do top, left and right as well
row.Cells[0].Borders.Bottom.Visible = false;
看起来不太好,但如果有更好的解决方案,请发布出来
p.AddFormattedText(“顶部标题行”)
的目的是什么? - I liked the old Stack Overflow