Question

Je suis entrain de créer un PDF par programmation à l'aide iTextSharp et la génération d'un PdfPTable.

Dans certaines cellules, j'ai besoin d'avoir un lien hypertexte.

Sans un lien hypertexte, je peux aligner le texte dans la cellule horizontalement et verticalement, cependant lorsque je l'ai ajouter dans le bloc qui contient mon lien hypertexte, la cellule perd ses propriétés d'alignement.

Voici mon code...

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

Quelqu'un peut-il m'aider à réaligner le lien?

Merci beaucoup.

Était-ce utile?

La solution

Voir cette réponse pour une discussion sur En Mode Texte rapport Le Mode Composite.En gros, si vous utilisez AddElement() vous devez définir l'alignement de l'objet au lieu de la cellule elle-même. Chunk n'a pas d'alignement, cependant, si vous avez besoin d'envelopper votre morceau d'une chose qui ne c'est l' 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);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top