无法在iTextsharp中更改字体? [英] Unable to change font in iTextsharp?
问题描述
我正在使用iTextSharp生成表格并以pdf格式打印。
我正在尝试更改字体,但它的效果为零。
I am using iTextSharp to generate a table and print it on in a pdf. I am trying to change the font but it has zero effect what so ever.
Font tablefont = new Font();
tablefont=FontFactory.GetFont(FontFactory.HELVETICA, 8,BaseColor.RED) ;
table.AddCell("Name :");
PdfPCell cell = new PdfPCell(new Phrase("Star Diamonds") {Font=tablefont});
推荐答案
如果你要开始一个新项目,我会建议将iText 7用于iTextSharp因为我们在该版本中引入了字体继承;请参阅第1章:介绍PdfFont类
If you are starting a new project, I'd suggest using iText 7 for iTextSharp because we've introduced font inheritance in that version; see Chapter 1: Introducing the PdfFont class
如果您坚持使用iTextSharp 5.5.9,则应阅读旧教程,并创建短语
像这样:
If you insist on using iTextSharp 5.5.9, you should read the old tutorials, and create your Phrase
like this:
BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
Font tablefont = new Font(bfTimes, 8);
PdfPCell cell = new PdfPCell(new Phrase("Star Diamonds", tablefont));
或者像这样:
Font tablefont = new Font(FontFamily.TIMES_ROMAN, 8);
PdfPCell cell = new PdfPCell(new Phrase("Star Diamonds", tablefont));
或者像这样:
Font tablefont = FontFactory.GetFont("Times-Roman", 8);
PdfPCell cell = new PdfPCell(new Phrase("Star Diamonds", tablefont));
您会在第11章:在动作书中选择iText的正确字体。该页面上显示的示例是Java格式,但如果向下滚动,您将找到所有相应 .cs
文件的链接。
You will find plenty of old font examples in chapter 11: Choosing the right font of the iText in Action book. The examples presented on that page are in Java, but if you scroll down, you'll find links to all the corresponding .cs
files.
还有一个广泛的有关字体的常见问题解答部分,包括对过去在StackOverflow上发布的问题的回答。
There's also an extensive FAQ section about fonts, consisting of answers to questions that have been posted on StackOverflow in the past.
虽然不是完全重复,但我通过参考如何使用VB.net中的iTextSharp中使用系统字体
Although not an exact duplicate, I closed the question by referring to How to use System Font in iTextSharp with VB.net
您正在使用C#,但以下行与您需要的非常相似:
You are using C#, but the following line is very similar to what you need:
document.Add(New Paragraph("Hello World, Arial.", font))
段落
类是您正在使用的 Phrase
类的子类。就像段落
一样,你需要传递字体
(在你的情况下 tablefont
)作为构造函数的第二个参数。
The Paragraph
class is a subclass of the Phrase
class that you are using. Just like with Paragraph
, you need to pass the font
(in your case tablefont
) as the second parameter of the constructor.
我提到如何在iTextSharp中使用VB.net中的系统字体:在回答这个问题时,你会得到有关如何使用计算机上可用字体的详细信息。
There's a second reason why I refer to How to use System Font in iTextSharp with VB.net : in the answer to that question, you get very detailed information on how to use any font that is available on your machine.
这篇关于无法在iTextsharp中更改字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!