2

我正在使用 iTextSharp 以编程方式创建 PDF 并生成 PdfPTable。

在某些单元格中,我需要有一个超链接。

如果没有超链接,我可以水平和垂直对齐单元格中的文本,但是当我添加包含我的超链接的块时,单元格会失去其对齐属性。

这是我的代码...

var webAddress = new Chunk(row["product_code"].ToString(), linkFont);
webAddress.SetAnchor(row["url"].ToString());

PdfPCell ProductCodeCell = new PdfPCell();
ProductCodeCell.AddElement(webAddress);
ProductCodeCell.VerticalAlignment = Element.ALIGN_MIDDLE;
ProductCodeCell.HorizontalAlignment = Element.ALIGN_CENTER;
ProductCodeCell.Padding = 3f;
table.AddCell(ProductCodeCell);

谁能帮我重新调整链接?

非常感谢。

4

1 回答 1

7

有关Text ModeComposite Mode的讨论,请参阅此答案。基本上,如果您使用,则需要在对象而不是单元格本身上设置对齐方式。但是,没有对齐,因此您需要将块包装在.AddElement()ChunkParagraph

var table = new PdfPTable(1);

//Create our chunk with anchor
var webAddress = new Chunk("hello");
webAddress.SetAnchor("http://www.example.net/");

//Create a paragraph holding the chunk and set the alignment on _it_
var para = new Paragraph(webAddress);
para.Alignment = Element.ALIGN_CENTER;

//Create our cell
PdfPCell ProductCodeCell = new PdfPCell();

//Add the paragrph to it
ProductCodeCell.AddElement(para);

//Padding on the cell still works
ProductCodeCell.Padding = 30f;

//Add cell to table
table.AddCell(ProductCodeCell);
于 2014-07-30T12:49:58.550 回答