Centered, multiline text using iTextSharp ColumnText

I am trying to center a block of multiline text using iTextSharp. I thought that using the ColumnText would do the trick, but I'm having some trouble getting both center alignment, as well as proper wrapping working at the same time.

Here is some code which shows the 2 methods I'm trying:

private void PrintLocationAddress(PdfContentByte Canvas, string Address)
{
//define the regions for our ColumnText objects
Rectangle rect1 = new Rectangle(150f, 300, 350f, 450f);
Rectangle rect2 = new Rectangle(150f, 50f, 350f, 200f);

//outline the rectangles so we can visualize placement of the ColumnText
Canvas.Rectangle(rect1.Left, rect1.Bottom, rect1.Width, rect1.Height);
Canvas.Rectangle(rect2.Left, rect2.Bottom, rect2.Width, rect2.Height);
Canvas.SetColorStroke(BaseColor.CYAN);
Canvas.Stroke();

//define the text and style
Chunk c = new Chunk(Address, new Font(Font.FontFamily.COURIER, 12, Font.NORMAL, BaseColor.MAGENTA));
c.SetTextRenderMode(PdfContentByte.TEXT_RENDER_MODE_FILL, 0, BaseColor.PINK);
Phrase LongText = new Phrase(c);

//this text is centered, but will wrap onto itself on the same line
ColumnText column1 = new ColumnText(Canvas);
column1.SetSimpleColumn(LongText, rect1.Left, rect1.Bottom, rect1.Right, rect1.Top, 0, PdfContentByte.ALIGN_CENTER);
column1.Go();

//this text will wrap, but how to center it?!
ColumnText column2 = new ColumnText(Canvas);
column2.SetSimpleColumn(rect2);
column2.SetText(LongText);
column2.Go();
}

You can view the output for the above code: http://imgur.com/Ty3oD7w
pdf-generation itextsharp
share|improve this question

asked Mar 1 '13 at 19:28
mgasparel
31839





I missed the ColumnText.Alignment property:

column2.Alignment = Element.ALIGN_CENTER;

So, to get Multiline text, centered, in a box of specified dimensions using ColumnText:

ColumnText column2 = new ColumnText(Canvas);
column2.SetSimpleColumn(rect2);
column2.SetText(LongText);
column2.Alignment = Element.ALIGN_CENTER;
column2.Go();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值