birwin
birwin

Reputation: 2684

Is there any way to vertically-align iText (iTextSharp) text using ColumnText?

Is there any way to vertically-align text using ColumnText? I am using 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();

My text appears at the top of the box I specify. I would like it to align with the bottom of the box.

 as it is                 as it should be
|---------------|        |---------------|
|   some text   | --->   |               |
|               |        |   some text   |
|---------------|        |---------------|

Upvotes: 1

Views: 4054

Answers (2)

György Gulyás
György Gulyás

Reputation: 1588

The answer of birwin is just not working. The ColumnText alignment is accept only horizontal alignments. And this is working till ColumnText text is in Text mode. When you call the ColumnText.AddElement the ColumnText switches to CompositMode and the Alignment has no effect.

If you really want to Vertical Alignment in ColumnText then you have to create a content which has aligned content and must add to ColumnText. Only the Table and Cell the correct object where vertical alignment has any effect.

So first we have to create a table, with one column and one cell. And set the size of the cell to same as column text size. And second, this table must be added to ColumntText

Here is my code:

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

Upvotes: 2

birwin
birwin

Reputation: 2684

The Solution:

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) -- Apparently the "true" here indicates the ColumnText is created in "Simulation" mode. I was not aware of this mode before comments to my question were posted.

if (ct.LinesWritten == 1) checks the number of lines that were written. If only one line was written, I shrink the size of the upper-right-y corner of my rectangle.

ct.SetSimpleColumn(myText, llx, lly, urx, ury, lineheight, Element.ALIGN_BOTTOM); when I call this line the second time, the ury (upper-right-hand corner) location has been changed and the box is smaller.

ct.Go() - The second time I call this, I don't pass the "true" param so the second call is not simulation mode and writes the data to the document.

Upvotes: 0

Related Questions