10

How can I print output in bold using printf? "[1m testing bold" does not do anything.

String format = "%-20s %-15s %-20s %-15s %-10s";

System.out.printf(format, "Name", "Group_name", "Java_Prof_Level", "Cpr_Nr", "Gender", "[1m testing bold");
4
  • You need to make the bold argument first, not last. Mar 17, 2015 at 21:13
  • see also github.com/fusesource/jansi
    – vandale
    Mar 17, 2015 at 21:25
  • @Malik does not work due to the format "[1m testing bold" is printed as txt
    – Na Dia
    Mar 17, 2015 at 21:28
  • This must have been a mega duplicate in 2015. Why wasn't there any effort to find the canonical question? E.g. "[1m" is a dead giveaway that it is about ANSI escape codes (e.g., (virtual) terminals). Visitors from search engines may end up shipwrecked here. Dec 28, 2022 at 17:49

3 Answers 3

21

You cannot print bold with Java System.out. It just streams to the standard output stream so, in principle, it is unformatted text only.

However, some software packages interpret special character sequences (the so-called ANSI escape sequences) to allow formatting.

Note that ANSI escape sequences start with an escape character, so you need to add that to your string also. (Try "\u001B[1m I am bold" or "\033[0;1m" + "I am bold".)

Most Unix terminals interpret ANSI escape sequences by default. In old DOS times, you needed to use ANSI.SYS for the escape sequences to work.

In Windows and the Eclipse terminal the codes do not work.

0
6

This really depends on what kind of console is being used. For IDEs like NetBeans and Eclipse, I'm not sure if you can affect the font. But for most terminals, the following escape character works:

String textInBold = "Java_Prof_Level";
System.out.print("\033[0;1m" + textInBold);
1
  • Thank you, just tried: "\033[0;1m". It does not work in Netbeans
    – Na Dia
    Mar 17, 2015 at 21:25
0

I found this. It is for Epson ECS/POS.

string ESC = "\u001B";
string GS = "\u001D";
string InitializePrinter = ESC + "@";
string BoldOn = ESC + "E" + "\u0001";
string BoldOff = ESC + "E" + "\0";
string DoubleOn = GS + "!" + "\u0011";  // 2x sized text (double-high + double-wide)
string DoubleOff = GS + "!" + "\0";

printJob.Print(InitializePrinter);
printJob.PrintLine("Here is some normal text.");
printJob.PrintLine(BoldOn + "Here is some bold text." + BoldOff);
printJob.PrintLine(DoubleOn + "Here is some large text." + DoubleOff);

First page of the PDF document: here is a reference link

I must use "" before the command. Otherwise it doesn't work:

oStream.print("" + (char)27 + (char)97 + (char)1); // For centering text
2
  • can you add some reference links for escape characters, it will be helpful to find other escape characters too Feb 13, 2021 at 7:18
  • The link is broken: "404 - File or directory not found." Dec 28, 2022 at 17:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.