Itextsharp Pdfpcell Allineamento con blocco
-
21-12-2019 - |
Domanda
Sto creando un PDF a livello programmatico usando ITEXTSHARP e generando un PDFPTable.
In alcune delle celle ho bisogno di avere un collegamento ipertestuale.
Senza un collegamento ipertestuale posso allineare il testo nella cella orizzontalmente e verticalmente, tuttavia quando aggiungo il pezzo che contiene il collegamento ipertestuale, la cella perde la sua proprietà di allineamento.
Ecco il mio codice ...
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);
.
Qualcuno può aiutarmi a riallineare il link?
Molte grazie.
Soluzione
Vedi Questa risposta per una discussione su modalità testo contro modalità composito .Fondamentalmente, se si utilizza AddElement()
è necessario impostare l'allineamento sul tuo oggetto invece della cella stessa.Chunk
non ha allineamento, tuttavia, quindi è necessario avvolgere il pezzo in qualcosa che fa il quale è il 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);
.