Nims
Nims

Reputation: 101

Setting font to paragraph in pdf using iText java

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?

Upvotes: 5

Views: 47486

Answers (5)

Sujeet Kumar
Sujeet Kumar

Reputation: 1942

I'm new in this.
but i'm showing u a simplest way to set font to paragraph.

document.open();
document.add(new Paragraph(" Hello World ", FontFactory.getFont(FontFactory.TIMES_ROMAN,18, Font.BOLD, BaseColor.BLACK)));
document.close();

By this way i'm changing the font, font size , color etc.

this worked for me..

Happy coding..

Upvotes: 1

CyberAbhay
CyberAbhay

Reputation: 534

For adding Font to itextpdf Paragraph you can simply use a Chunk then you can set Font to Chunk add that Chunk to Paragraph afterwards.

Example:

Font f3 = new Font(Font.FontFamily.TIMES_ROMAN, 18.0f, Font.BOLD, BaseColor.BLACK);
Chunk c3 = new Chunk("INVOICE", f3);
c3.setBackground(BaseColor.WHITE);       
Paragraph p3 = new Paragraph(c3);
p3.setAlignment(Element.ALIGN_CENTER);

Upvotes: 0

Bijin P Thomas
Bijin P Thomas

Reputation: 121

//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);
    }
}

Upvotes: 12

user590586
user590586

Reputation: 3050

Try this ,It will save the style of the text:

 Paragraph _p = new Paragraph();

         _p.setFont(regular);

        ArrayList htmlObjs = (ArrayList) HTMLWorker.parseToList(new StringReader(text_), null);

        for (int k = 0; k < htmlObjs.size(); ++k)
        {
            ArrayList<Chunk> chunk = ((Paragraph) htmlObjs.get(k)).getChunks();
            for (int l = 0; l < chunk.size(); l++)
            {
                Font _original_chunk_font = chunk.get(l).getFont();
                Font _newchunk_font = new Font(unicode);

                _newchunk_font.setFamily(_original_chunk_font.getFamilyname());
                _newchunk_font.setStyle(_original_chunk_font.getStyle());
                _newchunk_font.setSize(_original_chunk_font.getSize());
                _newchunk_font.setColor(_original_chunk_font.getColor());

                chunk.get(l).setFont(_newchunk_font);
            }
            _p.add((Element)htmlObjs.get(k));
        }

Upvotes: 2

Zach
Zach

Reputation: 897

I was pretty confused and almost posted the wrong answer to this.

Your paragraph is having its font set correctly. Just try inserting a String to see.

Your problem lies in your for loop. To the paragraph, you're adding a Element objects. An Element is composed of Chunk objects, which each have their own Font data.

Try setting the Font of the Chunks in your Elements when they are instantiated. That should solve your problem.

Upvotes: 3

Related Questions