Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting the font size in Itextpdf

I am working on a program where I am taking an ASCII file as input and converting it to PDF using Itext library.

I am able to convert and print it, but the font size appears too small. Currently I have set the font size to 6 but, if I change it to 7, It doesn't work, it doesn't fit on the PDF properly.

Here is a part of my code snippet:

  Document doc= new Document();
    Rectangle test = new Rectangle(531,666);
    doc = new Document(test); 
    doc.setMargins(0,0,0,0);
    p = new Paragraph(new Phrase(lineSpacing,line,
                   FontFactory.getFont(FontFactory.COURIER, fntSize)));
    doc.add(p);

I am not able to use double with this method. Is there any other way?

like image 667
Techidiot Avatar asked Jan 16 '14 06:01

Techidiot


1 Answers

So, my final snippet looks like this:

Document doc = new Document(PageSize.A4, 0f, 0f, 0f, 0f);
float fntSize, lineSpacing;
fntSize = 6.7f;
lineSpacing = 10f;
Paragraph p = new Paragraph(new Phrase(lineSpacing,line,
               FontFactory.getFont(FontFactory.COURIER, fntSize)));
doc.add(p);

It gives a perfect format for an A4 size paper with no margins and a good font size.

I hope it helps someone!

like image 97
Techidiot Avatar answered Oct 27 '22 23:10

Techidiot