我受到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中的某些縮放級別上看起來不可思議。
在文本後面添加一個透明圖像看起來像一個有趣的想法...有沒有什麼辦法可以給出一個例子如何添加透明圖像超鏈接到單元格,用段落覆蓋?我不知道如何將元素放置在彼此之上。 –