Exception in thread "main" java.lang.IllegalArgumentException: U+6570 ('.notdef') is not available in the font Helvetica-Bold, encoding: WinAnsiEncoding 问题解决

简介: 【5月更文挑战第26天】Exception in thread "main" java.lang.IllegalArgumentException: U+6570 ('.notdef') is not available in the font Helvetica-Bold, encoding: WinAnsiEncoding 问题解决

这个异常表明 Helvetica-Bold 字体在 WinAnsiEncoding 编码中不包含字符 U+6570('数')。

为了解决这个问题,你可以尝试更换字体,使用支持中文字符的字体。例如,你可以使用支持中文的 TrueType 字体(.ttf)。

以下是修改代码以使用 TrueType 字体的示例:

import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType0Font;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class ExcelToPdfConverter {
   
    public static void main(String[] args) {
   
        try {
   
            // Load the Excel document
            FileInputStream excelFile = new FileInputStream("input.xlsx");
            Workbook workbook = new XSSFWorkbook(excelFile);

            // Create PDF document
            PDDocument pdfDocument = new PDDocument();
            PDPage page = new PDPage();
            pdfDocument.addPage(page);

            // Load a TrueType font with Chinese characters support
            PDType0Font font = PDType0Font.load(pdfDocument, new FileInputStream("path/to/your/chinese-font.ttf"));

            // Write Excel content to PDF
            PDPageContentStream contentStream = new PDPageContentStream(pdfDocument, page);
            contentStream.setFont(font, 12);

            // Rest of the code remains the same...

            contentStream.close();

            // Save the PDF document
            FileOutputStream pdfFile = new FileOutputStream("output.pdf");
            pdfDocument.save(pdfFile);
            pdfDocument.close();
            excelFile.close();
            pdfFile.close();

            System.out.println("Excel to PDF conversion successful.");
        } catch (IOException e) {
   
            e.printStackTrace();
        }
    }
}

请替换 "path/to/your/chinese-font.ttf" 为你系统中支持中文字符的 TrueType 字体文件的路径。确保字体文件包含了需要的字符集。此示例中,使用了 PDType0Font 类加载 TrueType 字体。

目录
相关文章
|
4天前
|
Java 开发者
JAVA多线程初学者必看:为何选择继承Thread还是Runnable,这其中有何玄机?
【6月更文挑战第19天】在Java中创建线程,可选择继承Thread类或实现Runnable接口。继承Thread直接运行,但限制了多重继承;实现Runnable更灵活,允许多线程共享资源且利于代码组织。推荐实现Runnable接口,以保持类的继承灵活性和更好的资源管理。
|
4天前
|
Java 开发者
告别单线程时代!Java 多线程入门:选继承 Thread 还是 Runnable?
【6月更文挑战第19天】在Java中,面对多任务需求时,开发者可以选择继承`Thread`或实现`Runnable`接口来创建线程。`Thread`继承直接但限制了单继承,而`Runnable`接口提供多实现的灵活性和资源共享。多线程能提升CPU利用率,适用于并发处理和提高响应速度,如在网络服务器中并发处理请求,增强程序性能。不论是选择哪种方式,都是迈向高效编程的重要一步。
|
4天前
Exception in thread "main" java.lang.IllegalArgumentException: U+6570 ('.notdef') is not available in the font Helvetica-Bold, encoding: WinAnsiEncoding 这个问题如何解决
【6月更文挑战第19天】Exception in thread "main" java.lang.IllegalArgumentException: U+6570 ('.notdef') is not available in the font Helvetica-Bold, encoding: WinAnsiEncoding 这个问题如何解决
12 2
|
2天前
|
Scala
scala 读取文件(中文)异常 thread "main" java.nio.charset.MalformedInputException: Input length = 1
scala 读取文件(中文)异常 thread "main" java.nio.charset.MalformedInputException: Input length = 1
4 0
|
4天前
|
Java
揭秘!为何Java多线程中,继承Thread不如实现Runnable?
【6月更文挑战第19天】在Java多线程中,实现`Runnable`比继承`Thread`更佳,因Java单继承限制,`Runnable`可实现接口复用,便于线程池管理,并分离任务与线程,提高灵活性。当需要创建线程或考虑代码复用时,实现`Runnable`是更好的选择。
|
3天前
|
安全 Java
JAVA多线程通信新解:wait()、notify()、notifyAll()的实用技巧
【6月更文挑战第20天】Java多线程中,`wait()`, `notify()`和`notifyAll()`用于线程通信。在生产者-消费者模型示例中,它们确保线程同步。`synchronized`保证安全,`wait()`在循环内防止虚假唤醒,`notifyAll()`避免唤醒单一线程问题。关键技巧包括:循环内调用`wait()`,优先使用`notifyAll()`以保证可靠性,以及确保线程安全和正确处理`InterruptedException`。
|
3天前
|
安全 Java
深入解读JAVA多线程:wait()、notify()、notifyAll()的奥秘
【6月更文挑战第20天】JAVA多线程中,wait(), notify(), notifyAll()是Object类的关键同步机制。wait()让线程等待并释放锁,直到被notify()或notifyAll()唤醒或超时。它们必须在同步块中使用,持有锁的线程调用。notify()唤醒一个等待线程,notifyAll()唤醒所有。最佳实践包括:与synchronized结合,循环检查条件,避免循环内notify(),通常优先使用notifyAll()。
|
2天前
|
Java 程序员
从菜鸟到大神:JAVA多线程通信的wait()、notify()、notifyAll()之旅
【6月更文挑战第21天】Java多线程核心在于wait(), notify(), notifyAll(),它们用于线程间通信与同步,确保数据一致性。wait()让线程释放锁并等待,notify()唤醒一个等待线程,notifyAll()唤醒所有线程。这些方法在解决生产者-消费者问题等场景中扮演关键角色,是程序员从新手到专家进阶的必经之路。通过学习和实践,每个程序员都能在多线程编程的挑战中成长。
|
2天前
|
安全 Java 程序员
Java多线程详解
Java多线程详解
|
2天前
|
缓存 安全 Java
Java线程面试题含答案
Java线程面试题含答案