iTextSharp PdfPCell与块对齐

Ada*_*m92 2 c# itextsharp pdfptable

我正在使用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);
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮我重新调整链接吗?

非常感谢.

Chr*_*aas 7

有关文本模式复合模式的讨论,请参阅此答案.基本上,如果使用,则需要在对象上设置对齐而不是单元格本身.然而,没有对齐,所以你需要将你的块包装在那些做的东西中.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);
Run Code Online (Sandbox Code Playgroud)