使用iTextSharp ColumnText的居中多行文字 [英] Centered, multiline text using iTextSharp ColumnText

查看:2674
本文介绍了使用iTextSharp ColumnText的居中多行文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用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();
}

您可以查看上述代码的输出:
http://imgur.com/Ty3oD7w

You can view the output for the above code: http://imgur.com/Ty3oD7w

推荐答案

我错过了ColumnText.Alignment属性:

I missed the ColumnText.Alignment property:

column2.Alignment = Element.ALIGN_CENTER;

因此,要使用ColumnText在一个指定尺寸的框中居中获取Multiline文本:

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();

这篇关于使用iTextSharp ColumnText的居中多行文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆