How to use Fonts in iText PDF

天涯浪子 提交于 2021-02-11 14:25:27

问题


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);

how to change font to "Bodoni MT Black" (Not supported in FontFactory) instead of TIMES_ROMAN? please help.


回答1:


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:

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.

The iText 7 solution

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

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";

Next, you can use this path to create a FontProgram object obtained from the FontProgramFactory:

FontProgram fontProgram = FontProgramFactory.createFont(BODONIBLACK);

Using the FontProgram instance, you can create a PdfFont object.

PdfFont font = PdfFontFactory.createFont(fontProgram, PdfEncodings.WINANSI, true);

The font instance can be used as a parameter for the setFont() method:

Paragraph bodoni = new Paragraph().setFont(font).add("Bodoni");

The iText 5 solution

In the unlikely event that you don't have any other choice than to use iText 5, then you should read the Using fonts in PDF and iText

Just like with iText 7 you need a font program:

public static final String BODONIBLACK = "src/main/resources/fonts/BodoniMTBlack.ttf";

Now you can create a Font object like this:

BaseFont baseFont = BaseFont.createFont(BODONIBLACK, BaseFont.WINANSI, BaseFont.EMBEDDED);
Font bodoni = new Font(basefont, 12);

Additional note:

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).

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.

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.




回答2:


One important note on Bruno's answer above. (my rep<50, so cannot comment on his answer).

FontProgramFactory (at least in C#) is found in 'iText.IO.Font.FontProgramFactory'. I was initially looking in a different 'Font' namespace : 'iText.Kernel.Font'. And as Bruno says, all of this can be found on their website. This particular tibdit is at:

https://api.itextpdf.com/iText7/7.0.1/com/itextpdf/io/font/FontProgramFactory.html

So perhaps my note should just be deleted based on the Code of Conduct. Someone else can decide. But as I found Bruno's info helpful here, perhaps this note will save someone a few minutes.

Also, turns out that (at least for me in C#) the 'C' in .createFont needs to be capitalized. So... 'PdfFontFactory.CreateFont' rather than 'PdfFontFactory.createFont'



来源:https://stackoverflow.com/questions/51743430/how-to-use-fonts-in-itext-pdf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!