iTextSharp PdfPCell Alignment with Chunk



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

最新更新