我正在使用 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);

谁能帮我重新调整链接吗?

非常感谢。

有帮助吗?

解决方案

这个答案 进行讨论 文本模式 相对 复合模式. 。基本上,如果你使用 AddElement() 您需要在对象而不是单元格本身上设置对齐方式。 Chunk 然而,没有对齐,所以你需要将你的块包装在有对齐的东西中,这就是 Paragraph.

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);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top