Unable to vertically align text in table cell with itextsharp

后端 未结 3 665
别跟我提以往
别跟我提以往 2021-01-18 19:07

I can not figure out how to vertically align text in table cell. horizontal alignment is ok. I use itextsharp to generate pdf. Alignment should be applied to cells in table

3条回答
  •  孤城傲影
    2021-01-18 20:06

    It seems Element.ALIGN_MIDDLE only works, when the cell height is large i.r.t the text height. In a PDF, margins for text in cells are large by default, see iText developers

    You could append a \n and a space to your string to solve this, but the cell height will become much larger.

    For normal text, one way to lift text in a chunk a few pixels is:

     myChunk.SetTextRise(value);
    

    Only drawback: when the text is underlined (like a link) it only raises the text ! not the underline..

    The following seems to work for underlined text also,

     myCell.PaddingBottom = value;
    

    The idea was to put a link in a table cell.. blue font, underlined, vertical centering. My code now:

     iTextSharp.text.Font linksFont =
        FontFactory.GetFont(FontFactory.HELVETICA, 
                 10, Font.UNDERLINE, BaseColor.BLUE);
     PdfPTable myTable = new PdfPTable(1);                   
     Chunk pt = new Chunk("Go Google Dutch", linksFont);
     pt.SetAnchor(new Uri("https://www.google.nl"));
     Phrase ph1 = new Phrase(pt);
     PdfPCell cellP = new PdfPCell();
     cellP.PaddingBottom = linksFont.CalculatedSize/2;
     cellP.AddElement(ph1);
     myTable.AddCell(cellP);
    

提交回复
热议问题