在MigraDoc表中创建一个整个单元格链接

时间:2015-04-07 01:15:56

标签: c#-4.0 migradoc

MigraDoc中有没有办法让整个表格单元成为链接?我有一个表格目录,页码很难点击。如果整个单元格可以单击导航到指定页面,我更愿意。以下是我的代码示例:

// Create the table within the document
var document = new Document();
var section = document.AddSection();
var table = section.AddTable();

// Create a column in the table
var column = table.AddColumn();
column.Width = "2cm";

// Create a row in the table
var row = table.AddRow();
row.Height = "1cm";

// Add a hyperlink to the appropriate page
var paragraph = row.Cells[0].AddParagraph();
var hyperlink = paragraph.AddHyperlink("MyBookmarkName");
hyperlink.AddPageRefField("MyBookmarkName");

...
// Create the bookmark later in the document

2 个答案:

答案 0 :(得分:1)

我担心没有简单的方法可以使整个单元格可点击。我自己没有尝试过,但您可以在超链接中添加图像(可见或透明)或文本。

这会使可点击区域变大 - 如果您使用例如带有蓝色下划线的文本会有一个视觉提示,说明文本是可点击的。

答案 1 :(得分:0)

我受到了PDFsharp团队的答案的启发,尝试用空白超链接图像填充单元格,文本在超链接上。由于我的最终目标是实际上将整行作为超链接,我想出了以下解决方案。

首先,在表中要作为超链接的第一列之前添加一个额外的零宽度列。接下来,为每个零宽度单元格添加一个段落,超链接和透明的1像素图像。指定图像高度和宽度以填充您想要成为链接的多个表格单元格。此外,请务必将包含链接的段落的字体大小设置为接近零(零会引发异常,但图像在字体基线上对齐,因此您需要一个非常小的数字以防止段落大于图像)。

请注意,即使使用边框,零宽度列也不会在查看生成的PDF时更改明显的边框宽度。以下代码说明了我的方法:

// Declare some constants
var _rowHeight = new Unit(.75, UnitType.Centimeter);

// Create the document, section, and table
var document = new Document();
var section = document.AddSection();
var table = section.AddTable();

// Format the table
table.Rows.Height            = _rowHeight;
table.Rows.VerticalAlignment = VerticalAlignment.Center;

// Define the column titles and widths
var columnInfos = new[] {
    new { Title = "Non-Link Column", Width = new Unit(8, UnitType.Centimeter) },
    new { Title = ""               , Width = new Unit(0                     ) },
    new { Title = "Link Column 1"  , Width = new Unit(8, UnitType.Centimeter) },
    new { Title = "Link Column 2"  , Width = new Unit(8, UnitType.Centimeter) },
};

// Define the column indices
const int colNonLink   = 0;
const int colHyperlink = 1;
const int colLink1     = 2;
const int colLink2     = 3;

// Create all of the table columns
Unit tableWidth = 0;
foreach (var columnInfo in columnInfos)
{
    table.AddColumn(columnInfo.Width);
    tableWidth += columnInfo.Width;
}

// Remove the padding on the link column
var linkColumn = table.Columns[colHyperlink];
linkColumn.LeftPadding  = 0;
linkColumn.RightPadding = 0;

// Compute the width of the summary links
var linkWidth = tableWidth -
    columnInfos.Take(colHyperlink).Sum(ci => ci.Width);

// Create a row to store the column headers
var headerRow = table.AddRow();
headerRow.Height           = ".6cm";
headerRow.HeadingFormat    = true;
headerRow.Format.Font.Bold = true;

// Populate the header row
for (var colIdx = 0; colIdx < columnInfos.Length; ++colIdx)
{
    var columnTitle = columnInfos[colIdx].Title;
    if (!string.IsNullOrWhiteSpace(columnTitle))
    {
        headerRow.Cells[colIdx].AddParagraph(columnTitle);
    }
}

// In the real code, the following is done in a loop to dynamically add rows
var row = table.AddRow();

// Populate the row header
row.Cells[colNonLink].AddParagraph("Not part of link");

// Change the alignment of the link cell
var linkCell = row.Cells[colHyperlink];
linkCell.VerticalAlignment = VerticalAlignment.Top;

// Add a hyperlink that fills the remaining cells in the row
var linkParagraph = linkCell.AddParagraph();
linkParagraph.Format.Font.Size = new Unit(.001, UnitType.Point);
var hyperlink = linkParagraph.AddHyperlink("MyBookmarkName");
var linkImage = hyperlink.AddImage("Transparent.gif");
linkImage.Height = _rowHeight;
linkImage.Width  = linkWidth;

// Populate the remaining two cells
row.Cells[colLink1].AddParagraph("Part of link 1");
row.Cells[colLink2].AddParagraph("Part of link 2");

// Add a border around the cells
table.SetEdge(0, 0, columnInfos.Length, table.Rows.Count,
    Edge.Box | Edge.Interior, BorderStyle.Single, .75, Colors.Black);

上述代码的结果是一个文档,其中包含一个包含2行,3个可见列的表,其中最后一行中最后两个单元格的全部是指向“MyBookmarkName”的超链接。仅供参考,我根据建议here修改了PDFSharp源代码,以删除超链接周围的边框,这些边界在Adobe Acrobat Reader中的某些缩放级别看起来很糟糕。