Itext New Font with Bold and Underline

fonts, itext, java

Solution

Read up on HERE since you are using the IText library.

Basically use chunks then add those chunks to the document.

Chunk underline = new Chunk("Underline. ");
underline.setUnderline(0.1f, -2f); //0.1 thick, -2 y-location
document.add(underline);

I haven't tried it myself so I don't know how it turns out yet. Further reading up on the iText documentation, it seems that you have to define a bold font first and then implement it. THIS TUTORIAL shows an example of bold font usage with iText and making a pdf with bold text. From there, I'm sure you can implement the code above to the bold text and walah!, bold-underlined text :D

Problem

I am trying to add new font to my pdf which I need to make it bold and underlined.. I can add the new Font, but cannot make it bold and underlined at same time. I tried the following way.. ``` public class PdfGenerator { private static final String BASE_FONT = "Trebuchet MS"; private static final String BASE_FONT_BOLDITALIC = "Trebuchet MS BI"; private static final String BASE_FONT_BOLD = "Trebuchet MS B"; private static final Font titlefontSmall = FontFactory.getFont( BASE_FONT_BOLD, 10, Font.UNDERLINE); static { String filePath = "..my font directory"; String fontPath = filePath + "\\" + "trebuc.ttf"; String fontPathB = filePath + "\\" + "TREBUCBD.TTF"; String fontPathBI = filePath + "\\" + "TREBUCBI.TTF"; FontFactory.register(fontPath, BASE_FONT); FontFactory.register(fontPathB, BASE_FONT_BOLD); FontFactory.register(fontPathBI, BASE_FONT_BOLDITALIC); } public void genMyPdf(String filePath) { try { Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filePath)); document.open(); Paragraph p = new Paragraph("This should be bold & underline", titlefontSmall); document.add(p); document.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } } } ``` What am I doing wrong ?

Original source