如何调整单元格中文本的垂直位置?
How to tweak the vertical position of text in a cell?
table 中的垂直对齐有问题。文本太靠近底部边框:
我的代码如下所示:
nested = new PdfPTable(3);
nested.DefaultCell.VerticalAlignment = Element.ALIGN_MIDDLE;
nested.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
nested.WidthPercentage = 100;
nested.AddCell(new Phrase("blablabla"));
nested.AddCell(new Phrase("blablabla"));
nested.DefaultCell.HorizontalAlignment = Element.ALIGN_RIGHT;
nested.AddCell(new Phrase("Stand: " +
pdfdoc.Add(nested);
添加或删除行 DefaultCell.VerticalAlignment = Element.ALIGN_MIDDLE;
没有任何效果。
您正在创建只有一行文本的 PdfCell
对象。单元格的高度将根据该行文本确定。文本将自动居中对齐。这解释了为什么添加或删除 DefaultCell.VerticalAlignment = Element.ALIGN_MIDDLE;
没有效果:就 iText 而言,文本已经居中对齐。
您声称这不是真的,因为您认为文本的基线离底部太近了。我理解这种说法,但如果你阅读了我对问题 How does a PdfPCell's height relate to the font size? 的回答,你应该明白是哪些因素造成了这种看法:
- leading:默认字号为12pt;默认行距为 18 pt。 18 pt 的前导有点高,会导致基线上方额外 space。如果减少行距,您会发现顶部的 space 变少了。
- 不同的字体有不同的升序和降序值;添加单元格的方式,iText 不会考虑这些值。
我的建议:告诉 iText 使用您正在使用的字体的升序和降序:
nested.DefaultCell.UseAscender = true;
nested.DefaultCell.UseDescender = true;
您会注意到文本的位置已经好多了。如果不是更好,您可能需要添加一些填充。当然,所有这些都在 official documentation where you'll find an example called Spacing.cs 中进行了解释。试试这个例子,你会看到内容的位置是如何通过调整 UseAscender
、UseDescender
、Padding
等值而改变的。
table 中的垂直对齐有问题。文本太靠近底部边框:
我的代码如下所示:
nested = new PdfPTable(3);
nested.DefaultCell.VerticalAlignment = Element.ALIGN_MIDDLE;
nested.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
nested.WidthPercentage = 100;
nested.AddCell(new Phrase("blablabla"));
nested.AddCell(new Phrase("blablabla"));
nested.DefaultCell.HorizontalAlignment = Element.ALIGN_RIGHT;
nested.AddCell(new Phrase("Stand: " +
pdfdoc.Add(nested);
添加或删除行 DefaultCell.VerticalAlignment = Element.ALIGN_MIDDLE;
没有任何效果。
您正在创建只有一行文本的 PdfCell
对象。单元格的高度将根据该行文本确定。文本将自动居中对齐。这解释了为什么添加或删除 DefaultCell.VerticalAlignment = Element.ALIGN_MIDDLE;
没有效果:就 iText 而言,文本已经居中对齐。
您声称这不是真的,因为您认为文本的基线离底部太近了。我理解这种说法,但如果你阅读了我对问题 How does a PdfPCell's height relate to the font size? 的回答,你应该明白是哪些因素造成了这种看法:
- leading:默认字号为12pt;默认行距为 18 pt。 18 pt 的前导有点高,会导致基线上方额外 space。如果减少行距,您会发现顶部的 space 变少了。
- 不同的字体有不同的升序和降序值;添加单元格的方式,iText 不会考虑这些值。
我的建议:告诉 iText 使用您正在使用的字体的升序和降序:
nested.DefaultCell.UseAscender = true;
nested.DefaultCell.UseDescender = true;
您会注意到文本的位置已经好多了。如果不是更好,您可能需要添加一些填充。当然,所有这些都在 official documentation where you'll find an example called Spacing.cs 中进行了解释。试试这个例子,你会看到内容的位置是如何通过调整 UseAscender
、UseDescender
、Padding
等值而改变的。