C# 如何更改 PdfPTable 中的字体大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11619176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change the font-size in PdfPTable?
提问by Aada
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.
我正在使用 itextsharp 在 pdf 上动态写入。我正在 pdf 文档中创建一个表,其中包含来自数据库的值。有人可以告诉如何修改表中值的字体大小。
采纳答案by Pablo Claus
Try this:
尝试这个:
Font fontH1 = new Font(Currier, 16, Font.NORMAL);
PdfPTable table = new PdfPTable(1);
table.AddCell(new PdfPCell(new Phrase(yourDatabaseValue,fontH1)));
回答by Andrew Walters
Shouldn't you change the font size using the Font object that's passed when creating the text?
您不应该使用创建文本时传递的 Font 对象更改字体大小吗?
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
如果您还没有读过,这本 iText 书籍非常出色,几乎可以回答您的任何问题:http: //itextpdf.com/book/index.php
回答by HatSoft
Please try by setting font to the PdfPTable.DefaultCell property
请尝试将字体设置为 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
我之前已经回答过这个问题:使用 Itextsharp 为 Pdfptable 中的所有文本设置字体
回答by Amol Patil
@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.
@Pabloker 我不确定,但在使用您的解决方案时出现错误。我无法决定使用哪种字体(com.itextpdf.text.Font 或 com.lowagie.text.Font 或 java.awt.Font 或 org.apache.poi.ss.usermodel.Font)。当我尝试将它放入单元格时,无论我使用哪个,它都会给出不存在这样的构造函数的错误。对不起,我是 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.Fontand basefont is of type com.itextpdf.text.pdf.BaseFontThis solved the compilation problem.
注意使用的字体是com.itextpdf.text.Font类型,basefont 是com.itextpdf.text.pdf.BaseFont类型 这解决了编译问题。

