簡體   English   中英

如何在MigraDoc中的表格周圍添加邊框?

[英]How do you add a border around a table in MigraDoc?

有沒有一種方法可以在表格周圍添加邊框並在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);
}

我設法通過將每行邊框的可見性設置為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;

看起來不太好,但是如果有人有更好的解決方案,請張貼它

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM