如何在MigraDoc中为表格添加边框?

3
有没有办法在MigraDoc中为表格添加边框并隐藏单元格边框?

2
你有尝试过任何方法吗? - PaulF
1
我尝试过table.Borders.Visible = true;,并且对于每一行,我尝试将其设置为visible false,尝试将顶部颜色更改为空。 - user629283
2个回答

6

边框的默认宽度为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);
}

我尝试过,但没有成功,不过我最终还是找到了解决方法。 - user629283

2
我成功实现了此操作,通过将每行边框的可见性设置为false。
  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
@PDFsharpNovice 我尝试过了,但对我没用。 - user629283
@PDFsharpNovice 当我尝试了你的方法后,它移除了包括表格周围的所有边框线。addformattedText部分-那只是一个快速复制粘贴,但我打算将其加粗。 - user629283

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接