Pregunta

Estoy creando un PDF mediante programación con iTextSharp y la generación de una PdfPTable.

En algunas de las células que necesito para tener un hipervínculo.

Sin un hipervínculo puedo alinear el texto en la celda horizontalmente y verticalmente, sin embargo, cuando me agregue en el fragmento que contiene mi hipervínculo, la célula pierde su propiedad de alineación.

Aquí está mi 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);

Alguien me puede ayudar a realinear el enlace?

Muchas gracias.

¿Fue útil?

Solución

Ver esta respuesta para una discusión sobre El Modo De Texto frente a Compuesto De Modo.Básicamente, si usted utiliza AddElement() usted necesita para establecer la alineación, en su objeto, en lugar de la propia célula. Chunk no tiene la alineación, sin embargo, por lo que necesita para envolver el trozo de algo que sí que es el 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 bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top