itextpdf 中文字体设置
生活随笔
收集整理的這篇文章主要介紹了
itextpdf 中文字体设置
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
itextpdf 中文字體設置
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? ??
*******************
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\fontsMode 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;}************ 內部枚舉:EmbeddingStrategypublic 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:輸出中文
? ? ? ? ? ? ? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? ? ? ??
總結
以上是生活随笔為你收集整理的itextpdf 中文字体设置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: TEM014 - 新版阿里云网站界面高保
- 下一篇: 惠普台式计算机耳机插口,hp电脑耳机插孔