如何使用iTextSharp设置等宽字体? [英] How to set monospaced font by using iTextSharp?

查看:238
本文介绍了如何使用iTextSharp设置等宽字体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA,BaseFont。 CP1250,BaseFont.EMBEDDED,false); 
字体titleFont = new Font(bf,20);

现在,我想为字符串格式化设置等宽字体(固定宽度)。我需要下载一些ttf文件(正如我正在阅读的那样)或iTextSharp中已经包含了等宽字体 解决方案

如果你不想嵌入字体,你可以使用这个:

  BaseFont bf = BaseFont.createFont(
BaseFont.COURIER,BaseFont.CP1250,BaseFont.NOT_EMBEDDED);
字体titleFont = new Font(bf,20);

Helvetica是比例字体。如果您需要等宽字体,则需要使用Courier等字体。有关等宽字体的维基百科页面



<请注意,你的代码并没有创建嵌入的字体:Helvetica(就像Courier那样)是所谓的Standard Type 1字体之一。标准Type 1字体永远不会嵌入,因为iText只能访问这些字体的AFM文件,而不能访问PFB文件。阅读例如:为什么我创建PDFA / 1a时出现字体嵌入错误?在其他一些情况下,iText嵌入一个字体,即使你不想。请参阅:为什么iText嵌入字体,甚至当我指定不嵌入?



如果你想嵌入一个等宽字体,或者,如果你不喜欢信使,你需要一个字体文件,例如一个ttf文件。我谷歌搜索性感等宽字体,我发现这些网页:十大最流行的等宽字体 10大免费等宽字体编程。如果您在Windows上工作,您可以选择Courier New和Lucida Sans打字机,如本知识库文章

一旦你有一个TTF文件,只需使用标准的iText代码。在Lucida Sans Typewriter Regular的情况下,你需要这样的东西:

  BaseFont bf = BaseFont.createFont(
c:/windows/fonts/LTYPE.TTF,BaseFont.CP1250,BaseFont.EMBEDDED);
字体titleFont = new Font(bf,20);

注意:请务必检查您要使用的编码是否被您使用的字体支持。不要以为每个字体都知道每一种编码。



请注意,大多数字体不是免费的。另请参阅我是否需要Windows的许可证使用iText时的字体?。您可以下载字体的事实并不意味着您可以免费使用它(iText也是如此;如果您正在为客户构建应用程序,则必须购买iText许可证)。 p>

This is how I managed my font so far:

BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1250, BaseFont.EMBEDDED, false);
Font titleFont = new Font(bf, 20);

Now, I want to set monospaced (fixed width) font for purpose of string formatting. Do I have to download some ttf file (as I was reading about it) or there is monospaced font already included in iTextSharp

解决方案

If you don't want to embed a font, you can use this:

BaseFont bf = BaseFont.createFont(
    BaseFont.COURIER, BaseFont.CP1250, BaseFont.NOT_EMBEDDED);
Font titleFont = new Font(bf, 20);

Helvetica is a proportional font. If you need a monospaced font, you need to use a font such as Courier. See the Wikipedia page about monospaced fonts.

Note that your code didn't create a font that was embedded either: Helvetica is (just like Courier for that matter) one of the so-called Standard Type 1 fonts. Standard Type 1 fonts are never embedded because iText only has access to the AFM files of these fonts, not to the PFB files. Read for instance: Why do I get a font embedding error when creating PDFA/1a?. In some other cases, iText embeds a font even if you don't want to. See for instance: Why is iText embedding a font even when I specify not to embed?

If you want to embed a monospaced font or, if you don't like Courier, you need a font file, for instance a ttf file. I Googled for "sexier" monospaced fonts and I found these pages: Top 10 Most Popular Monospaced Fonts and 10 great free monospaced fonts for programming. If you work on Windows, you have the choice between Courier New and Lucida Sans Typewriter as described in this knowledge base article.

Once you have a TTF file, just use the standard iText code. In the case of Lucida Sans Typewriter Regular, you'd need something like this:

BaseFont bf = BaseFont.createFont(
    "c:/windows/fonts/LTYPE.TTF", BaseFont.CP1250, BaseFont.EMBEDDED);
Font titleFont = new Font(bf, 20);

Note: always check if the encoding you want to use is supported by the font you're using. Don't assume that every font knows every encoding.

Be aware that most fonts aren't free. See also Do I need a license for Windows fonts when using iText?. The fact that you can download a font doesn't automatically mean you can use it for free (the same is true for iText; if you are building an application for a customer, you'll have to purchase an iText license).

这篇关于如何使用iTextSharp设置等宽字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆