Kamal kannan Kamal kannan - 2 months ago 14
Groovy Question

How to set particular cell in right side alignment in itext table

I m creating PDF File using Itext.I am having to cell in my table, and i can able to set cell text in left alignment or right alignment, But I want to set First Cell in left side and second cell in right side.

Code:

builder {
document(name: fileName,
pageSize: PageSize.A4,
leftMargin:3, rightMargin:3, topMargin:3, bottomMargin:3) {
// paragraph( "Test" )
table(columns:2) {
record([
{ "Item : 1" }, // Cell 1
{ "Amount : 100" }, // cell 2
])
.setHorizontalAlignment(Element.ALIGN_LEFT) //it align only left side or right side.
.font_n(0, "HELVETICA",10,Font.BOLD)
.font_n(1, "HELVETICA",10,Font.NORMAL)
}
}
}


Output:

enter image description here

How can i set Item:1 in left side alignment and Amount:100 in right side alignment.

Thanks,

Answer

Use This Code:

   builder {
    document(name: fileName,
        pageSize: PageSize.A4,
        leftMargin:3, rightMargin:3, topMargin:3, bottomMargin:3) {
        //                paragraph( "Test" )
        table(columns:2) {
                record([
                        { "Item : 1" }, // Cell 1
                        { "Amount : 100" }, // cell 2            
                    ])
                .setHorizontalAlignment_n(0,Element.ALIGN_LEFT)
                .setHorizontalAlignment_n(1,Element.ALIGN_RIGHT)  
                .font_n(0, "HELVETICA",10,Font.BOLD)
                .font_n(1, "HELVETICA",10,Font.NORMAL)
        }
    }
}
Comments