user3819936
user3819936

Reputation: 35

some part of text left aligned and other right aligned in same line in itext

I want to create a pdf that has name and contact number left aligned and email right aligned on the same line as of name in itext. How can I implement that? Should I use rectangle method? If yes, then what values should be assigned to its parameters to place them on same line?

Upvotes: 0

Views: 2512

Answers (2)

Jens
Jens

Reputation: 69440

You can use a table with 2 columns to do so.

To set the border to unvisible use :cell.setBorder(Rectangle.NO_BORDER); And to set table width to 100% use: table.setWidthPercentage(100);

Upvotes: 1

Krishna Kant
Krishna Kant

Reputation: 97

Use a pdfTable to get left and right alling text in same line . below is the approach you can follow .

 Document document = new Document(PageSize.A4, 30, 30, 150, 50);     
    FileOutputStream fos = new FileOutputStream("filepath");
    PdfWriter writer = PdfWriter.getInstance(document,fos );
    document.open();

    Rectangle rect = writer.getPageSize();

    float[] columnWidths = {2f, 1f,2.5f};
    PdfPTable table = new PdfPTable(columnWidths);
    table.setTotalWidth(527);
    table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
    table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
    table.addCell("abc left");
    table.addCell("");
    table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
    table.addCell("abc right");

    table.writeSelectedRows(
                        0, -1, rect.getLeft(30), rect.getTop(20), writer.getDirectContent());
    document.close();

Upvotes: 3

Related Questions