有没有办法使用ColumnText垂直对齐iText(iTextSharp)文本?
有没有办法使用ColumnText垂直对齐文本?我正在使用iTextSharp有没有办法使用ColumnText垂直对齐iText(iTextSharp)文本?,itext,Itext,有没有办法使用ColumnText垂直对齐文本?我正在使用iTextSharp Chunk c = new Chunk(text, this.detailFont); Phrase myText = new Phrase(c); ct.SetSimpleColumn(myText, llx, lly, urx, ury, lineheight, Element.ALIGN_BOTTOM); ct.Go(); 我的文本显示在我指定的框的顶部。我希望它与盒子底部对齐 as it is
Chunk c = new Chunk(text, this.detailFont);
Phrase myText = new Phrase(c);
ct.SetSimpleColumn(myText, llx, lly, urx, ury, lineheight, Element.ALIGN_BOTTOM);
ct.Go();
我的文本显示在我指定的框的顶部。我希望它与盒子底部对齐
as it is as it should be
|---------------| |---------------|
| some text | ---> | |
| | | some text |
|---------------| |---------------|
解决方案:
Chunk c = new Chunk(text, this.detailFont);
Phrase myText = new Phrase(c);
ct.SetSimpleColumn(myText, llx, lly, urx, ury, lineheight, Element.ALIGN_BOTTOM);
ct.Go(true);
if (ct.LinesWritten == 1)
{
ury = ury - (this.rowheight);
}
ct.SetSimpleColumn(myText, llx, lly, urx, ury, lineheight, Element.ALIGN_BOTTOM);
ct.Go();
ct.Go(true)
——这里的“true”显然表示列文本是在“模拟”模式下创建的。在对我的问题发表评论之前,我不知道这种模式
if(ct.lineswrited==1)
检查写入的行数。如果只写了一行,我会缩小矩形右上角y的大小
ct.SetSimpleColumn(myText、llx、lly、urx、ury、lineheight、Element.ALIGN_-BOTTOM)
当我第二次调用此行时,ury
(右上角)位置已更改,框变小
ct.Go()
-第二次调用此函数时,我没有传递“true”参数,因此第二次调用不是模拟模式,而是将数据写入文档。birwin的答案就是不起作用。ColumnText对齐方式为仅接受水平对齐。直到ColumnText文本处于文本模式时,这才停止工作。调用ColumnText.AddElement时,ColumnText将切换到复合模式,对齐方式无效
如果您确实想在ColumnText中垂直对齐,则必须创建一个包含对齐内容的内容,并且必须添加到ColumnText中。只有表格和单元格才是垂直对齐有效的正确对象
因此,首先我们必须创建一个表,包含一列和一个单元格。并将单元格大小设置为与列文本大小相同。
其次,必须将该表添加到ColumntText
这是我的密码:
// create doc
var pdfDoc = new Document( PageSize.A4 );
var pdfWriter = PdfWriter.GetInstance( pdfDoc, new FileStream( Path.Combine( Path.GetTempPath(), "test.pdf" ), FileMode.Create ) );
pdfDoc.Open();
var canvas = pdfWriter.DirectContent;
canvas.SaveState();
canvas.Rectangle( 100, 100, 100, 100 );
canvas.SetRgbColorFill( 255, 128, 128 );
canvas.Fill();
canvas.RestoreState();
// create font
BaseFont bfTimes = BaseFont.CreateFont( BaseFont.TIMES_ROMAN, BaseFont.CP1252, false );
Font times = new Font( bfTimes, (float)10, Font.ITALIC, new BaseColor( (int)232434 ) );
Phrase myText = new Phrase( new Chunk( "Hello World", times ) );
ColumnText ct = new ColumnText( canvas );
/* wrong birwin code :
ct.SetSimpleColumn( myText, 100, 100, 200, 200, ct.Leading, Element.ALIGN_BOTTOM );
*/
// new working code: crreate a table
PdfPTable table = new PdfPTable( 1 );
table.SetWidths( new[] { 1 } );
table.WidthPercentage = 100;
table.AddCell( new PdfPCell( myText )
{
HorizontalAlignment = Element.ALIGN_RIGHT,
VerticalAlignment = Element.ALIGN_BOTTOM,
FixedHeight = 100
} );
ct.SetSimpleColumn( 100, 100, 200, 200 );
ct.AddElement( table );
ct.Go();
pdfDoc.Close();
列文本
自上而下填充。不过,您可以先在模拟模式下填充列文本
,读取填充区域的高度,然后在列文本
中填充相应向下移动的矩形。谢谢,这非常有效!如果您不希望单元格周围显示实心边框,我只会将Border=0
添加到PdfPCell初始化中。