2015-04-07 53 views
1

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 

回答

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中的某些缩放级别上看起来不可思议。

1

恐怕没有简单的方法可以让整个单元点击。我没有尝试过,但可以将图像(可见或透明)或文本添加到超链接。

这会使可点击面积变大 - 如果您使用例如蓝色下划线的文字将会有一个视觉提示文字是可点击的。

+0

在文本后面添加一个透明图像看起来像一个有趣的想法...有没有什么办法可以给出一个例子如何添加透明图像超链接到单元格,用段落覆盖?我不知道如何将元素放置在彼此之上。 –