使MigraDoc表中的整个单元成为链接 [英] Make an Entire Cell in a MigraDoc Table a Link

查看:292
本文介绍了使MigraDoc表中的整个单元成为链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MigraDoc中有一种方法使整个表单元格链接?我有一个表格式的目录,页号很难点击。我更喜欢整个单元格可点击导航到指定的页面。下面是我的代码示例:

Is there a way in MigraDoc to make an entire table cell a link? I have a tabular table of contents, and the page number is difficult to click. I would prefer if the entire cell was clickable to navigate to a specified page. Here is an example of my code:

// 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


推荐答案

我的灵感来自PDFsharp团队的答案,尝试用一个空白的超链接图像填充单元格,文本超链接。由于我的最终目标是实际上使整个行成为一个超链接,我想出了以下解决方案。

I was inspired by the answer from the PDFsharp Team to try and fill the cell with a blank hyperlink image, with text over the hyperlink. Since my ultimate goal was to actually make an entire row a hyperlink, I came up with the following solution.

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

First, add an additional zero-width column prior to the first column in the table that you want to be a hyperlink. Next, add a paragraph, hyperlink, and transparent 1-pixel image to each zero-width cell. Specify the image height and width to fill however many table cells you want to be a link. Also, be sure to set the font size of the paragraph containing the link to nearly zero (zero throws an exception, but images are aligned on the font baseline, so you need a very small number to prevent the paragraph from being larger than the image).

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

Note that a zero-width column, even with borders, does not change the apparent border width when viewing the resulting PDF. The following code illustrates my approach:

// 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的超链接。为了参考,我根据这里删除超链接周围的边框,在Adobe Acrobat Reader中的某些缩放级别看起来很不爽。

The result of the above code is a document containing a table with 2 rows, 3 visible columns, where the entirety of the last two cells in the final row are a hyperlink to "MyBookmarkName". Just for reference, I did modify the PDFSharp source code according to the advice here to remove borders around hyperlinks, which looked wonky at certain zoom levels in Adobe Acrobat Reader.

这篇关于使MigraDoc表中的整个单元成为链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆