Aada
Aada

Reputation: 1640

How to change the font-size in PdfPTable?

I am using itextsharp to dynamically write on the pdf. I am creating a table in the pdf document which contains the values from the database. Can someone please tell how to modify the font-size of the values in the table.

Upvotes: 17

Views: 63270

Answers (4)

Amol Patil
Amol Patil

Reputation: 248

@Pabloker I am not sure but I am getting error while using your solution. I am not able to decide which font to use(com.itextpdf.text.Font or com.lowagie.text.Font or java.awt.Font or org.apache.poi.ss.usermodel.Font). Whichever I use when I try to put it in cell it gives error that no such constructor exists. I am sorry but I am novice to iText.

Fortunately I have figured out following code that worked for me.

BaseFont bf = BaseFont.createFont(
                        BaseFont.TIMES_ROMAN,
                        BaseFont.CP1252,
                        BaseFont.EMBEDDED);
                Font font = new Font(bf, 12);
                PdfPCell pdfCell = new PdfPCell(new Phrase(sCellVal,font));

note the font used is of type com.itextpdf.text.Font and basefont is of type com.itextpdf.text.pdf.BaseFont This solved the compilation problem.

Upvotes: 1

Pablo Claus
Pablo Claus

Reputation: 5920

Try this:

Font fontH1 = new Font(Currier, 16, Font.NORMAL);

PdfPTable table = new PdfPTable(1);

table.AddCell(new PdfPCell(new Phrase(yourDatabaseValue,fontH1)));

Upvotes: 28

HatSoft
HatSoft

Reputation: 11201

Please try by setting font to the PdfPTable.DefaultCell property

Example:

pdfTable.DefaultCell.Phrase = new Phrase() { Font = fontNormal };

I have already answer this before : Set font for all text from Pdfptable with Itextsharp

Upvotes: 2

Andrew Walters
Andrew Walters

Reputation: 4803

Shouldn't you change the font size using the Font object that's passed when creating the text?

If you haven't read it yet, this iText book is exceptional and will answer pretty much any question you have: http://itextpdf.com/book/index.php

Upvotes: 0

Related Questions