赞
踩
itext7 字体(默认不输出中文)
内置中文字体:itext7-core包含了中文字体包,可用来创建字体
- # 常用字体程序及对应编码
- STSong-Light ==> UniGB-UCS2-H
- HeiseiKakuGo-W5 ==> UniJIS-UCS2-H
- HeiseiMin-W3 ==> UniJIS-UCS2-H
windows中文字体库:c:/windows/fonts
- # 部分
- PS C:\windows\fonts> ls
-
-
- 目录: C:\windows\fonts
-
-
- Mode LastWriteTime Length Name
- ---- ------------- ------ ----
- -a---- 2019/12/7 17:08 261872 segmdl2.ttf
- -a---- 2019/12/7 17:08 168404 segoepr.ttf
- -a---- 2019/12/7 17:08 167800 segoeprb.ttf
- -a---- 2019/12/7 17:08 596948 segoesc.ttf
- -a---- 2019/12/7 17:08 581252 segoescb.ttf
- -a---- 2019/12/7 17:08 955804 segoeui.ttf
- -a---- 2019/12/7 17:08 951724 segoeuib.ttf
- -a---- 2019/12/7 17:08 529712 segoeuii.ttf
- -a---- 2019/12/7 17:08 913712 segoeuil.ttf
- -a---- 2019/12/7 17:08 854140 segoeuisl.ttf
- -a---- 2019/12/7 17:08 541468 segoeuiz.ttf
- -a---- 2019/12/7 17:08 324260 seguibl.ttf
- -a---- 2019/12/7 17:08 356008 seguibli.ttf
- -a---- 2019/12/7 17:08 2072388 seguiemj.ttf
- -a---- 2019/12/7 17:08 1400724 seguihis.ttf
- -a---- 2019/12/7 17:08 459940 seguili.ttf
- -a---- 2019/12/7 17:08 971080 seguisb.ttf
- -a---- 2019/12/7 17:08 457892 seguisbi.ttf
- -a---- 2019/12/7 17:08 467180 seguisli.ttf
- -a---- 2019/12/7 17:08 2454728 seguisym.ttf
- -a---- 2019/12/7 17:08 120560 webdings.ttf
- -a---- 2019/12/7 17:08 82180 wingding.ttf
- -a---- 2019/12/7 17:08 120560 webdings.ttf
- -a---- 2019/12/7 17:08 82180 wingding.ttf
- -a---- 2019/12/7 17:09 14522408 YuGothB.ttc
- -a---- 2019/12/7 17:09 13805400 YuGothL.ttc
- -a---- 2019/12/7 17:08 13742180 YuGothM.ttc
- -a---- 2019/12/7 17:09 13674828 YuGothR.ttc
- -a---- 2021/1/7 21:12 2774928 方正粗黑宋简体.ttf

PdfFontFactory:创建字体
- public final class PdfFontFactory {
- private static final String DEFAULT_ENCODING = "";
- private static final PdfFontFactory.EmbeddingStrategy DEFAULT_EMBEDDING;
- private static final boolean DEFAULT_CACHED = true;
-
- public PdfFontFactory() {
- }
-
- public static PdfFont createFont() throws IOException {
- return createFont("Helvetica", "");
- }
-
- public static PdfFont createFont(PdfDictionary fontDictionary) {
-
- public static PdfFont createFont(FontProgram fontProgram) {
- public static PdfFont createFont(FontProgram fontProgram, String encoding) {
- public static PdfFont createFont(FontProgram fontProgram, String encoding, PdfFontFactory.EmbeddingStrategy embeddingStrategy) {
-
- public static PdfFont createFont(String fontProgram) throws IOException {
- public static PdfFont createFont(String fontProgram, String encoding) throws IOException {
- public static PdfFont createFont(String fontProgram, String encoding, PdfDocument cacheTo) throws IOException {
- public static PdfFont createFont(String fontProgram, String encoding, PdfFontFactory.EmbeddingStrategy embeddingStrategy) throws IOException {
- public static PdfFont createFont(String fontProgram, String encoding, PdfFontFactory.EmbeddingStrategy embeddingStrategy, PdfDocument cacheTo) throws IOException {
- public static PdfFont createFont(String fontProgram, PdfFontFactory.EmbeddingStrategy embeddingStrategy) throws IOException {
- public static PdfFont createFont(String fontProgram, String encoding, PdfFontFactory.EmbeddingStrategy embeddingStrategy, boolean cached) throws IOException {
-
- public static PdfFont createFont(byte[] fontProgram, String encoding) throws IOException {
- public static PdfFont createFont(byte[] fontProgram, PdfFontFactory.EmbeddingStrategy embeddingStrategy) throws IOException {
- public static PdfFont createFont(byte[] fontProgram, String encoding, PdfFontFactory.EmbeddingStrategy embeddingStrategy) throws IOException {
- public static PdfFont createFont(byte[] fontProgram, String encoding, PdfFontFactory.EmbeddingStrategy embeddingStrategy, boolean cached) throws IOException {
-
- public static PdfFont createTtcFont(byte[] ttc, int ttcIndex, String encoding, PdfFontFactory.EmbeddingStrategy embeddingStrategy, boolean cached) throws IOException {
- public static PdfFont createTtcFont(String ttc, int ttcIndex, String encoding, PdfFontFactory.EmbeddingStrategy embeddingStrategy, boolean cached) throws IOException {
-
- public static PdfType3Font createType3Font(PdfDocument document, boolean colorized) {
- public static PdfType3Font createType3Font(PdfDocument document, String fontName, String fontFamily, boolean colorized) {
-
- public static PdfFont createRegisteredFont(String fontName, String encoding, PdfFontFactory.EmbeddingStrategy embeddingStrategy, int style, boolean cached) throws IOException {
- public static PdfFont createRegisteredFont(String fontName, String encoding, PdfFontFactory.EmbeddingStrategy embeddingStrategy, boolean cached) throws IOException {
- public static PdfFont createRegisteredFont(String fontName, String encoding, PdfFontFactory.EmbeddingStrategy embeddingStrategy) throws IOException {
- public static PdfFont createRegisteredFont(String fontName, String encoding, PdfFontFactory.EmbeddingStrategy embeddingStrategy, int style) throws IOException {
- public static PdfFont createRegisteredFont(String fontName, String encoding) throws IOException {
- public static PdfFont createRegisteredFont(String fontName) throws IOException {
-
- public static void registerFamily(String familyName, String fullName, String path) {
- public static void register(String path) {
- public static void register(String path, String alias) {
-
- public static int registerDirectory(String dirPath) {
- public static int registerSystemDirectories() {
-
- public static Set<String> getRegisteredFonts() {
- public static Set<String> getRegisteredFamilies() {
-
- public static boolean isRegistered(String fontName) {
-
-
- private static PdfType1Font createFontFromType1FontProgram(Type1Font fontProgram, String encoding, PdfFontFactory.EmbeddingStrategy embeddingStrategy) {
- private static PdfType0Font createType0FontFromTrueTypeFontProgram(TrueTypeFont fontProgram, String encoding, PdfFontFactory.EmbeddingStrategy embeddingStrategy) {
- private static PdfTrueTypeFont createTrueTypeFontFromTrueTypeFontProgram(TrueTypeFont fontProgram, String encoding, PdfFontFactory.EmbeddingStrategy embeddingStrategy) {
- private static PdfType0Font createType0FontFromCidFontProgram(CidFont fontProgram, String encoding, PdfFontFactory.EmbeddingStrategy embeddingStrategy) {
-
-
- static {
- DEFAULT_EMBEDDING = PdfFontFactory.EmbeddingStrategy.PREFER_EMBEDDED;
- }
-
-
- ************
- 内部枚举:EmbeddingStrategy
-
- public static enum EmbeddingStrategy {
- FORCE_EMBEDDED,
- FORCE_NOT_EMBEDDED,
- PREFER_EMBEDDED,
- PREFER_NOT_EMBEDDED;
-
- private EmbeddingStrategy() {
- }
- }
- }

示例
- public class Test3 {
-
- private static final String dest = "./test/1.pdf";
- private static final String dest2 = "./test/2.pdf";
- private static final String dest3 = "./test/3.pdf";
-
- public static void fun() throws Exception{
- PdfDocument pdfDocument = new PdfDocument(new PdfWriter(dest));
- Document document = new Document(pdfDocument);
-
- Paragraph paragraph = new Paragraph("hello 瓜田李下");
- document.add(paragraph);
-
- document.close();
- }
-
- public static void fun2() throws Exception{ //font-asian中文包
- PdfDocument pdfDocument = new PdfDocument(new PdfWriter(dest2));
- Document document = new Document(pdfDocument);
-
- PdfFont font = PdfFontFactory.createFont("STSong-Light","UniGB-UCS2-H");
- document.setFont(font);
-
- Paragraph paragraph = new Paragraph("hello 瓜田李下");
- document.add(paragraph);
-
- document.close();
- }
-
- public static void fun3() throws Exception{ //系统中文包
- PdfDocument pdfDocument = new PdfDocument(new PdfWriter(dest3));
- Document document = new Document(pdfDocument);
-
- PdfFont font = PdfFontFactory.createFont("./fonts/simkai.ttf");
- document.setFont(font);
-
- Paragraph paragraph = new Paragraph("hello 瓜田李下");
- document.add(paragraph);
-
- document.close();
- }
-
- public static void main(String[] args) throws Exception{
- File file = new File(dest);
- file.getParentFile().mkdirs();
-
- fun();
- fun2();
- fun3();
- }
- }

fun:不输出中文
fun2:输出中文
fun3:输出中文
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。