Itext7 study notes miscellaneous talk series 2-add Chinese (other fonts) and font-related matters in itext7

    In this chapter, we will discuss how to display Chinese or other non-ASCII characters such as CJK (Chinese/Japan/Koera) in itext7, and interpret the function of the font-asian.jar package.

Font encoding

    If we want to really understand how the font is stored on the computer, what the font file is, and the encoding problem, you can refer to the relationship between the Chinese encoding TTF fonts.
    By the way, check the default code page of the machine (that is, ANSI) , Enter chcp in cmd to view the code page number

CJK font in PDF

    We can conclude that there are three popular fonts on the market:

  • Postscript/Type1 is a set of vector font standards proposed by Adobe in 1985. When the CJK fonts are copyrighted, charged, and CJK fonts are expanded, the CID-keyed font technology is used. It is more troublesome to use it in itext7. Yes, it’s a lot of things.
  • Truetype TrueType is another set of vector font standards jointly proposed by Apple and Microsoft in 1991
  • OpenType In 995, Adobe and Microsoft began to jointly develop a font compatible with Type1 and TrueType, and truly support Unicode. Later, when it was released, it was officially named OpenType. OpenType can embed Type1 and TrueType, so it has the characteristics of both, whether it is viewed on the screen or printed, the quality is very good.

    If we want to embed CJK fonts in PDF, the itext7 or other non-Adobe software we use cannot embed CJK into PDF because of license protection. For details, we can look at cmap_info in font-asian-7.xxjar. txt information:

itext-0-2-1

    Here, I mainly translate and summarize the content in cmap_info.txt (because some of the URLs in it are out of date, so I summarize some of the information I found online):

  • The copyright of CJK or CE fonts belongs to Adobe and can only be used in software such as Adobe Reader
  • iText7 cannot embed CJK or CE fonts , because it infringes Adobe's copyright. For example, embedding CJK fonts can be viewed with other PDF viewers, which will affect its interests
  • There are two types of files in font-asian.jar, one is the cmap file (encoding file), and the other is the .properties file (related to font programs)
  • The font metrics used to describe the font metrics in Type 1 fonts are stored in Adobe font metrics (AFM) and Adobe composite font metrics (ACFM) files. These files are used by font programs. Put these in itext. The information is stored in the .properties file and stored in the form of key-value, so that we don't need the actual font program, just put the font information in the file created by itext.

itext7 embedded fonts

Create Type 1 font

    When we expand the cmap in the above figure, we will find some font programs (the .properties file name represents the name of the font program). Of course, we also need to determine the encoding supported by the font program. The following are commonly used font programs and corresponding encodings from Adobe :

Font program coding
STSong-Light UniGB-UCS2-H
MHei-Medium UniCNS-UCS2-H
MSung-Light UniCNS-UCS2-H
HeiseiKakuGo-W5 UniJIS-UCS2-H
HeiseiMin-W3 UniJIS-UCS2-H
HYGoThic-Medium UniKS-UCS2-H
HYSMyeongJo-Medium UniKS-UCS2-H

The codes corresponding to the rest of the font programs are temporarily unavailable, and the information on the adobe official website is different. If you want to use a specific font, you can check it out at that time.

    Then is to create a specific font, the core code is as follows, suppose we use STSong-Light to create a font:

......
PdfFont f2 = PdfFontFactory.createFont("STSong-Light", "UniGB-UCS2-H",true);
document.add(new Paragraph("hellos你好").setFont(f2)); //表格、list其他方式也是这种方式
......

If you have ever used itext5, you will find that the way to create fonts is different from itext5, it doesn’t matter, I will write another article to write about the differences between itex7 and itext5 when creating fonts

    After creating the pdf, we press crtl+d to see the font properties, as shown in the figure:

itext-0-2-2

    We can find that the actual font is AdobeSongStd-Light. This is because I chose Simplified Chinese when I installed it on this machine. The font programs for Song Ti and Hei Ti are included in the "C:\Program Files (x86)\Adobe\ In Acrobat Reader DC\Resource\CIDFont" (installation directory), you can see that we are using CID fonts, and there are two font programs:

itext-0-2-3

    What if we use font programs that are not in this folder? For example, HeiseiKakuGo-W5, etc., we can use itext7 to file, but when opening pdf, such a dialog box will pop up, let us download the font, as shown in the figure:

itext-0-2-4

Create other fonts

    It is easier to embed other fonts. You can use your own defined font file, support ttf file, or use the font file that comes with windows, as follows:

PdfFont f2 = PdfFontFactory.createFont("STSong-Light", "UniGB-UCS2-H",true);
PdfFont f3 = PdfFontFactory.createFont("C:/Windows/Fonts/simhei.ttf", PdfEncodings.IDENTITY_H,true);
//Add paragraph to the document
document.add(new Paragraph("hellos你好").setFont(f2));
document.add(new Paragraph("hellos你好").setFont(f3));

    The display effect is as follows:

itext-0-2-5

PdfEncodings.IDENTITY_H is the Unicode encoding. Generally ttf files use this encoding. I will talk about it in detail in the future

Code sample packaging

    I was in the first chapter and the first chapter of the practice based on the addition of Chinese output, please rest assured download

Guess you like

Origin blog.csdn.net/u012397189/article/details/78471319