Make an Entire Cell in a MigraDoc Table a Link

大憨熊 提交于 2019-12-11 10:36:57

问题


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

回答1:


I'm afraid there is no easy way to make the whole cell clickable. I haven't tried it myself, but you can add images (visible or transparent) or text to the hyperlink.

This will make the clickable area bigger - and if you use e.g. blue underlined text there will be a visual hint that the text is clickable.




回答2:


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.

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).

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);

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.



来源:https://stackoverflow.com/questions/29482167/make-an-entire-cell-in-a-migradoc-table-a-link

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!