Setting font to paragraph in pdf using iText java

Nims picture Nims · Apr 28, 2011 · Viewed 38.6k times · Source

I was trying to create pdf using iText in java. And am failed when I tried to set font to paragraph. The exact problem is only the font size is not getting applied. I used the following code.

StringReader strReader = new StringReader(content);
arrList = HTMLWorker.parseToList(strReader, null);

Font font = new Font(BaseFont.createFont("c:\\ARIALUN0.ttf", BaseFont.IDENTITY_H, 
    BaseFont.EMBEDDED), 6, Font.BOLD, new Color(0, 0, 0));

Paragraph para = new Paragraph();  
para.setFont(font);
for (int k = 0; k < arrList.size(); ++k) {                   
    para.add((com.lowagie.text.Element)arrList.get(k)); 
}

Can anyone help me to find a solution?

Answer

Bijin P Thomas picture Bijin P Thomas · Oct 1, 2013

//use this code.Sometimes setfont() willnot work with Paragraph

try
{

    FileOutputStream out=new FileOutputStream(name);
    Document doc=new Document();
    PdfWriter.getInstance(doc, out);
    doc.open();

    Font f=new Font(FontFamily.TIMES_ROMAN,50.0f,Font.UNDERLINE,BaseColor.RED);
    Paragraph p=new Paragraph("New PdF",f);

    p.setAlignment(Paragraph.ALIGN_CENTER);

    doc.add(p);
    doc.close();
    }
    catch(Exception e)
    {
        System.out.println(e);
    }
}