Pergunta

Estou criando um PDF programaticamente usando o iTextSharp e gerando um PdfPTable.

Em algumas células preciso ter um hiperlink.

Sem um hiperlink posso alinhar o texto na célula horizontal e verticalmente, porém quando adiciono o pedaço que contém meu hiperlink, a célula perde sua propriedade de alinhamento.

Aqui está o meu código ...

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);

Alguém pode me ajudar a realinhar o link?

Muito obrigado.

Foi útil?

Solução

Ver esta resposta para uma discussão sobre Modo Texto contra Modo Composto.Basicamente, se você usar AddElement() você precisa definir o alinhamento em seu objeto em vez da própria célula. Chunk não tem alinhamento, entretanto, você precisa agrupar seu pedaço em algo que tenha, que é o 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);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top