【问题标题】:Centered, multiline text using iTextSharp ColumnText使用 iTextSharp ColumnText 居中的多行文本
【发布时间】:2013-02-16 09:42:09
【问题描述】:

我正在尝试使用 iTextSharp 将多行文本块居中。我认为使用 ColumnText 可以解决问题,但是我在同时进行中心对齐和正确换行时遇到了一些麻烦。

这里有一些代码,显示了我正在尝试的 2 种方法:

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

【问题讨论】:

    标签: pdf-generation itextsharp


    【解决方案1】:

    我错过了 ColumnText.Alignment 属性:

    column2.Alignment = Element.ALIGN_CENTER;
    

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

    ColumnText column2 = new ColumnText(Canvas);
    column2.SetSimpleColumn(rect2);
    column2.SetText(LongText);
    column2.Alignment = Element.ALIGN_CENTER;
    column2.Go();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多