【问题标题】:How to size a table to the page width in MigraDoc?如何将表格调整为 MigraDoc 中的页面宽度?
【发布时间】:2014-07-27 22:49:12
【问题描述】:

我正在尝试将表格自动调整为页面的全宽。该表应该有 2 列,每列 50% 的宽度。

我怎样才能做到这一点?我尝试了 LeftIndent 和 RightIndent 属性,但没有成功。

【问题讨论】:

  • 发布你们当前的一些代码可能对我们有所帮助.....

标签: c# pdfsharp migradoc


【解决方案1】:

这是一种避免硬编码宽度并允许更灵活的纸张格式的方法。确保在您的课程中包含 using MigraDoc.DocumentObjectModel; 语句。

Document document = new Document();

Section section = document.AddSection();
section.PageSetup.PageFormat = PageFormat.A4;

Table table = section.AddTable();

float sectionWidth = section.PageSetup.PageWidth - section.PageSetup.LeftMargin - section.PageSetup.RightMargin;
float columnWidth = sectionWidth / 2;

Column column = table.AddColumn();
column.Width = columnWidth;
Column column2 = table.AddColumn();
column2.Width = columnWidth;

Row row = table.AddRow();
row.Cells[0].AddParagraph("Row 1, Column A");
row.Cells[1].AddParagraph("Row 1, Column B");

【讨论】:

  • 需要注意的是,这只有在 PageSetup.PageWidthPageSetup.LeftMarginPageSetup.RightMargin 之前分配的情况下才有效。如果要使用PageSetup 的默认值,请将DefaultPageSetup.Clone() 分配给该部分的PageSetup
  • section.PageSetup.PageFormat = PageFormat.A4;之前添加section.PageSetup = document.DefaultPageSetup.Clone();
【解决方案2】:

您不能在 MigraDoc 中使用百分比值。
您可以设置每列的绝对宽度。

因此,当使用每边 2.5 厘米边距的 DIN A4 时,表格还剩下 16 厘米,因此您必须创建两列,每列 8 厘米。

您可以设置表格的左缩进水平移动表格。

【讨论】:

    【解决方案3】:

    如果 Kidquick 的回答不起作用,请使用 document.DefaultPageSetup:

    document.DefaultPageSetup.PageFormat = PageFormat.A4;
    
    int sectionWidth = (int)document.DefaultPageSetup.PageWidth - (int)document.DefaultPageSetup.LeftMargin - (int)document.DefaultPageSetup.RightMargin;
    
    Table table = section.AddTable();
    table.AddColumn(sectionWidth);
    Row row = table.AddRow();
    row.Height = 60;
    

    【讨论】:

    • 永远不要修改DefaultPaegSetup。可以将DefaultPageSetupClone() 分配给会话的PageSetup,并且可以根据需要进行修改。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-12
    • 1970-01-01
    • 1970-01-01
    • 2010-12-01
    • 1970-01-01
    • 2017-05-25
    相关资源
    最近更新 更多