【问题标题】:iTextSharp PdfPCell Alignment with ChunkiTextSharp PdfPCell 对齐与块
【发布时间】:2014-09-21 22:29:07
【问题描述】:

我正在使用 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);

谁能帮我重新调整链接?

非常感谢。

【问题讨论】:

    标签: c# itextsharp pdfptable


    【解决方案1】:

    请参阅this answer,了解关于文本模式复合模式的讨论。基本上,如果您使用AddElement(),您需要在对象而不是单元格本身上设置对齐方式。但是,Chunk 没有对齐,因此您需要将块包装在 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);
    

    【讨论】:

    • 谢谢!在 VB.Net 上也能正常工作!
    猜你喜欢
    • 2011-06-20
    • 1970-01-01
    • 2010-12-02
    • 1970-01-01
    • 2012-11-16
    • 2013-11-18
    • 2014-03-04
    相关资源
    最近更新 更多