当前位置: 代码迷 >> 综合 >> The CMap com/itextpdf/io/font/cmap/UniGB-UTF16-H was not found 异常信息
  详细解决方案

The CMap com/itextpdf/io/font/cmap/UniGB-UTF16-H was not found 异常信息

热度:59   发布时间:2024-02-22 12:01:13.0

The CMap com/itextpdf/io/font/cmap/UniGB-UTF16-H was not found 异常信息

出现错误原因:在做根据坐标填充pdf时,填充中文时报错

		<dependency><groupId>com.itextpdf</groupId><artifactId>font-asian</artifactId><version>7.1.7</version><scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf --><dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf</artifactId><version>5.5.10</version></dependency><!-- https://mvnrepository.com/artifact/com.itextpdf/kernel --><dependency><groupId>com.itextpdf</groupId><artifactId>kernel</artifactId><version>7.1.2</version></dependency><!-- https://mvnrepository.com/artifact/com.itextpdf/layout --><dependency><groupId>com.itextpdf</groupId><artifactId>layout</artifactId><version>7.1.2</version></dependency>
 try (PdfReader reader = new PdfReader("C:\\Users\\xx\\Desktop\\d\\xin.pdf");PdfWriter writer = new PdfWriter("D:\\test.pdf");PdfDocument pdfDoc = new PdfDocument(reader, writer)) {Document document = new Document(pdfDoc);//页数从1开始int pageNumber = 1;//单位应该是pxfloat left = 148;float bottom = 622;float width = 216;PdfFont font = PdfFontFactory.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);//PdfFont font = PdfFontFactory.createFont("C:\\Users\\xx\\Desktop\\d\\songti.TTF", PdfEncodings.IDENTITY_H, false);document.add(new Paragraph("中文").setFont(font).setFixedPosition(pageNumber, 148, 622, 100));document.add(new Paragraph("身份证").setFont(font).setFixedPosition(1, 102, 606, 100));document.add(new Paragraph("123").setFont(font).setFixedPosition(2, left, bottom, width));document.add(new Paragraph("456").setFont(font).setFixedPosition(2, 500, bottom, width));System.out.println("————————————————————————————————————");document.close();} catch (IOException e) {System.out.println(e.getMessage());}

解决方法

加上这一行中文字体报错

  PdfFont font = PdfFontFactory.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

改为引入本地字体资源

PdfFont font = PdfFontFactory.createFont("C:\\Users\\xx\\Desktop\\d\\songti.TTF", PdfEncodings.IDENTITY_H, false);
  相关解决方案