Vignesh Vino
Vignesh Vino

Reputation: 1238

How to set the size of font while writing to pdf file

I'm reading some text from a .txt file and writing those to a pdf file.But in my pdf the texts are showing as some bigger fonts. Even though I've changed the font size of the .txt file to bigger or too small the fonts in pdf will be same.

Part of my code which is working with reading writing in text file

Document document = new Document(PageSize.A4.rotate(), 36, 36, 80, 160);
 ........
 .........
           document.newPage();

            //Writing in a default page from the txt file

            document.add(new com.lowagie.text.Paragraph("\n"));
            iStream = new FileInputStream(path1); //path1 is location of .txt file
            in = new DataInputStream(iStream);
            is = new InputStreamReader(in);
            br = new BufferedReader(is);
            String strLine;
            while ((strLine = br.readLine()) != null)
            {
                Paragraph para = new com.lowagie.text.Paragraph(strLine + "\n");
                para.setAlignment(Element.ALIGN_JUSTIFIED);
                document.add(new com.lowagie.text.Paragraph(strLine + "\n"));

            }
            br.close();
            is.close();
            in.close();
            iStream.close();
            document.close();
            writer.close(); 

Sorry I'm using the lower version of itext because right now I can't add any third party library to my windchill.This version of itext already my windchill have in it.

How to set the font size inside pdf file when writing into it.?Any help would be really helpful.

Upvotes: 0

Views: 3223

Answers (1)

AndMim
AndMim

Reputation: 550

Quite simple :D

Fist, i would change one thing:

while ((strLine = br.readLine()) != null) {
    Paragraph para = new com.lowagie.text.Paragraph(strLine + "\n");
    para.setAlignment(Element.ALIGN_JUSTIFIED);
    document.add(para);
}

This way, the paragraph is justified in the document. The way you did it, you changed the text alignment of one paragaph-object, but then added a new, totally different one to the PDF.

Now to your problem:
You can specify the Font in each Paragraph` contructor as follows:

Paragraph para = new com.lowagie.text.Paragraph(strLine + "\n", FontFactory.getFont("Arial", 14f /*This is the size, but as a float!*/);

The entire documentation of the lowagie API can be found here.

Best Regards

Upvotes: 2

Related Questions