How do I align string print out in left, right, center alignment?

The following code snippet will teach you how to align string in left, right or center alignment when you want to print out string to a console. We will print the string using the printf(String format, Object... args) method. The format specifier / parameter defines how the string will be formatted for output and the args is the value that will be formatted.

The format parameter / specifier include flags, width, precision and conversion-characters in the order shown below. The square brackets in the notation means the part is an optional parameter.

% [flags] [width] [.precision] conversion-character
Flags Description
- left-align the output, when not specified the default is to right-align
+ print (+) or (-) sign for numeric value
0 zero padded a numeric value
, comma grouping separator for number greater that 1000
space will output a (-) symbol for negative value and a space if positive
Conversion Description
s string, use capital S to uppercase the strings
c character, use capital C to uppercase the characters
d integer: byte, short, integer, long
f floating point number: float, double
n new line

Width: Defines the field width for printing out the value of argument. It also represents the minimum number of characters to
be printed out to the output.

Precision: For floating-point conversion the precision define the number of digits of precision in a floating point value. For string value this will extract the substring.

To center the string for output we use the method from the Apache Commons Lang library. This method will center-align the string str in a larger string of size using the default space character (‘ ‘). You can supply the third parameter to define your own space character / string.

package org.kodejava.example.lang;

import org.apache.commons.lang3.StringUtils;

import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.ChronoUnit;

public class StringAlignment {
    private static Object[][] people = {
        {"Alice", LocalDate.of(2000, Month.JANUARY, 1)},
        {"Bob", LocalDate.of(1989, Month.DECEMBER, 15)},
        {"Carol", LocalDate.of(1992, Month.JULY, 24)},
        {"Ted", LocalDate.of(2006, Month.MARCH, 13)},

    public static void main(String[] args) {
        String nameFormat = "| %1$-20s | ";
        String dateFormat = " %2$tb %2$td, %2$tY  | ";
        String ageFormat = " %3$3s |%n";
        String format = nameFormat.concat(dateFormat).concat(ageFormat);
        String line = new String(new char[48]).replace('\0', '-');

  "Name", 22),
  "Birth Date", 16),
  "Age", 6));

        for (Object[] data : people) {
                data[0], data[1],
                ChronoUnit.YEARS.between((LocalDate) data[1],;


Here is the output of our code snippet above:

|         Name         |   Birth Date   | Age  |
| Alice                |  Jan 01, 2000  |   17 |
| Bob                  |  Dec 15, 1989  |   27 |
| Carol                |  Jul 24, 1992  |   24 |
| Ted                  |  Mar 13, 2006  |   10 |

Maven Dependencies


How do I set paragraph alignment in iText?

To set the alignment of a paragraph object we can use the Paragraph.setAlignment() method. We can pass constants such as Paragraph.ALIGN_LEFT, Paragraph.ALIGN_CENTER, Paragraph.ALIGN_RIGHTto the setAlignment() method.

package org.kodejava.example.itextpdf;

import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;


public class ParagraphAlignment {
    private static final String CONTENT =
            "Lorem ipsum dolor sit amet, consectetur adipisicing " +
                    "elit, sed do eiusmod tempor incididunt ut labore et " +
                    "dolore magna aliqua. Ut enim ad minim veniam, quis " +
                    "nostrud exercitation ullamco laboris nisi ut aliquip " +
                    "ex ea commodo consequat. Duis aute irure dolor in " +
                    "reprehenderit in voluptate velit esse cillum dolore eu " +
                    "fugiat nulla pariatur. Excepteur sint occaecat cupidatat " +
                    "non proident, sunt in culpa qui officia deserunt mollit " +
                    "anim id est laborum.";

    public static void main(String[] args) {
        Document document = new Document();
        try {
                    new FileOutputStream("ParaAlign.pdf"));

            // Creates a check for the paragraphs contents
            Chunk chunk = new Chunk(ParagraphAlignment.CONTENT);

            // Creates paragraphs and set the alignment of the paragraph.
            // We use the Paragraph.ALIGN_LEFT, Paragraph.ALIGN_CENTER
            // and Paragraph.ALIGN_RIGHT
            Paragraph para1 = new Paragraph(chunk);

            Paragraph para2 = new Paragraph(chunk);

            Paragraph para3 = new Paragraph(chunk);

        } catch (DocumentException | FileNotFoundException e) {

Maven Dependencies