问题描述
我有一个Java应用程序,必须在itextPdf中使用FontFactory使用"Bodoni MT Black"字体,我应该如何修改我的代码?这是我的代码,
I have a java application where i have to use "Bodoni MT Black" font using FontFactory in itextPdf how should i modify my code?This is my code,
Font base = FontFactory.getFont(FontFactory.TIMES_ROMAN, 6);
如何将字体更改为"Bodoni MT Black"(FontFactory不支持)而不是TIMES_ROMAN?请帮忙.
how to change font to "Bodoni MT Black" (Not supported in FontFactory) instead of TIMES_ROMAN? please help.
推荐答案
问题中的代码是使用iText 5的代码.但是,不再支持iText 5. iText的当前版本是iText 7.1.2:
The code in your question is code that uses iText 5. However, iText 5 is no longer supported. The current version of iText is iText 7.1.2:
如果您正在开发新项目,则应该放弃iText 5并升级到iText 7,因为所有新开发都将在iText 7上完成,而不是在iText 5上完成.假设某个时候您需要对PDF 2.0的支持,那么您将需要丢弃iText 5代码,因为iText 5中将永远不支持PDF 2.0.假设某个时候您需要在HTML到PDF转换的上下文中支持SVG,删除所有iText代码,并从iText 7重新开始.
If you are working on a new project, you should abandon iText 5 and upgrade to iText 7 because all new development will be done on iText 7, not on iText 5. Suppose that at some point you need support for PDF 2.0, then you'll need to throw away your iText 5 code, because support for PDF 2.0 will never be supported in iText 5. Suppose that at some point you need support for SVG in the context of HTML to PDF conversion, you will need to throw away all your iText code and start anew with iText 7.
如果您按照我的建议进行升级,则应该阅读有关字体的iText 7教程章节: https://developers.itextpdf.com/content/itext-7-building-blocks/chapter-1
If you follow my advice and upgrade, then you should read the iText 7 tutorial chapter about fonts: https://developers.itextpdf.com/content/itext-7-building-blocks/chapter-1
本教程说明,如果您不想使用诸如Times Roman的标准Type 1字体之一,则需要一个字体程序.特别是Mpre,如果要使用"Bodoni MT Black",则需要文件 BodoniMTBlack. ttf 在您计算机上的某个位置,例如:
This tutorial explains that you need a font program if you don't want to use one of the standard Type 1 fonts such as times roman. Mpre specifically, if you want to use "Bodoni MT Black", you need the file BodoniMTBlack.ttf somewhere on your computer, for instance:
public static final String BODONIBLACK = "src/main/resources/fonts/BodoniMTBlack.ttf";
接下来,您可以使用此路径创建从FontProgramFactory
获取的FontProgram
对象:
Next, you can use this path to create a FontProgram
object obtained from the FontProgramFactory
:
FontProgram fontProgram = FontProgramFactory.createFont(BODONIBLACK);
使用FontProgram
实例,可以创建PdfFont
对象.
Using the FontProgram
instance, you can create a PdfFont
object.
PdfFont font = PdfFontFactory.createFont(fontProgram, PdfEncodings.WINANSI, true);
font
实例可用作setFont()
方法的参数:
The font
instance can be used as a parameter for the setFont()
method:
Paragraph bodoni = new Paragraph().setFont(font).add("Bodoni");
iText 5解决方案
在不太可能的情况下,除了使用iText 5之外您别无选择,那么您应该阅读在PDF和iText中使用字体
就像使用iText 7一样,您需要一个字体程序:
Just like with iText 7 you need a font program:
public static final String BODONIBLACK = "src/main/resources/fonts/BodoniMTBlack.ttf";
现在您可以创建一个Font
对象,如下所示:
Now you can create a Font
object like this:
BaseFont baseFont = BaseFont.createFont(BODONIBLACK, BaseFont.WINANSI, BaseFont.EMBEDDED);
Font bodoni = new Font(basefont, 12);
附加说明:
Stack Overflow引入了新的行为准则,旨在在网站上营造更健康的氛围(友善,贡献,展示尊重是其副标题中突出显示的某些方面.
Stack Overflow has introduced a new Code of Conduct that aims to create a healthier atmosphere on the web site (be kind, contribute, show respect are some of the aspects highlighted in its subtitle).
在此行为准则的背景下,我想通知您,在提出问题之前,可以使用iText官方网站上提供的信息来为将来的更好的氛围做出贡献.
In the context of that Code of Conduct, I want to inform you that you can contribute to a better atmosphere in the future by using the information that is provided on the official iText web site before asking a question.
当您使用iText时,遇到与iText相关的问题时,您的第一个反应应该是访问iText官方网站,您将在其中找到我上面概述的信息.人们做出了巨大的努力来编写教程,以回答与您类似的问题.通过忽略这些出色的内容,您将无法欣赏所做的辛苦工作.请将来考虑.
When you are using iText and you are confronted with an iText-related question, your first reflex should be to visit the official iText web site where you will find the information I summarized above. People have done a great effort writing tutorials in answer to questions similar to yours. By ignoring that great content, you fail to appreciate the hard work that was done. Please take that into consideration in the future.
这篇关于如何在iText PDF中使用字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!