Reputation: 2404
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 ?
Upvotes: 6
Views: 39807
Reputation: 131
You can define your FONT in your class as such:
public final static Font CUSTOM_FONT = new Font(Font.FontFamily.TIMES_ROMAN, 8, Font.BOLD | Font.UNDERLINE);
and then use it as :
paragraph.add(new Phrase( " YOUR TEXT HERE ",PDFCreator.CUSTOM_FONT));
Upvotes: 2
Reputation: 1669
You can just use the following to get both bold and underline:-
Chunk buyerOrder = new Chunk("Buyer's Order Number", FontFactory.getFont(FontConstants.TIMES_ROMAN,8,Font.BOLD));
buyerOrder.setUnderline(0.1f, -2f);
Hope it helps! Thanks
Upvotes: 0
Reputation: 221
you can define a font object with both styles :
private static final Font SUBFONT = new Font(Font.getFamily("TIMES_ROMAN"), 12, Font.BOLD|Font.UNDERLINE);
Upvotes: 22
Reputation: 991
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
Upvotes: 8
Reputation: 2496
try it
Font fontbold = FontFactory.getFont("Times-Roman", 12, Font.BOLD);
document.add(new Paragraph("Times-Roman, Bold", fontbold));
and Font.UNDERLINE for undeline
Upvotes: 1