2013-03-01 7 views
1

Я пытаюсь центрировать блок многострочного текста с помощью iTextSharp. Я думал, что использование ColumnText сделает трюк, но у меня возникли проблемы с выравниванием по центру, а также с правильной упаковкой одновременно.Центрированный многострочный текст с использованием 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

ответ

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