更改字段文本的 PDFbox 字体

Change PDFbox font of field text

我正在编写一个应用程序,它使用 PDFbox 库来填充 PDF 文件中的字段。
在其中一个字段中,我将文本设置为用希伯来字母书写。
当我 运行 我的 Android 设备上的代码时,我得到以下日志:

java.lang.IllegalArgumentException: This font type only supports 8-bit code points
    at com.tom_roush.pdfbox.pdmodel.font.PDType1Font.encode(PDType1Font.java:317)
    at com.tom_roush.pdfbox.pdmodel.font.PDFont.encode(PDFont.java:264)
    at com.tom_roush.pdfbox.pdmodel.font.PDFont.getStringWidth(PDFont.java:293)
    at com.tom_roush.pdfbox.pdmodel.interactive.form.PlainTextFormatter.format(PlainTextFormatter.java:183)
    at com.tom_roush.pdfbox.pdmodel.interactive.form.AppearanceGeneratorHelper.insertGeneratedAppearance(AppearanceGeneratorHelper.java:360)
    at com.tom_roush.pdfbox.pdmodel.interactive.form.AppearanceGeneratorHelper.setAppearanceContent(AppearanceGeneratorHelper.java:224)
    at com.tom_roush.pdfbox.pdmodel.interactive.form.AppearanceGeneratorHelper.setAppearanceValue(AppearanceGeneratorHelper.java:128)
    at com.tom_roush.pdfbox.pdmodel.interactive.form.PDTextField.constructAppearances(PDTextField.java:247)
    at com.tom_roush.pdfbox.pdmodel.interactive.form.PDTerminalField.applyChange(PDTerminalField.java:221)
    at com.tom_roush.pdfbox.pdmodel.interactive.form.PDTextField.setValue(PDTextField.java:202)
    at com.package.app.MainActivity.lambda$checkPdf$MainActivity(MainActivity.java:128)
    at com.package.app.MainActivity$$Lambda.run(Unknown Source:18)
    at java.lang.Thread.run(Thread.java:764)

我试图在 Stack Overflow 上找到一些关于它的信息,但我找到的 none 答案与填写表格有关。都和PDPageContentStream.

有关

这是我在代码中填写表格的方式:

try {
    PDDocument document = PDDocument.load(getAssets().open("file.pdf"));
    PDDocumentCatalog docCatalog = document.getDocumentCatalog();
    PDAcroForm acroForm = docCatalog.getAcroForm();

    // Fill the text field
    ((PDTextField) acroForm.getField("name")).setValue("בדיקה");

    File root = android.os.Environment.getExternalStorageDirectory();

    String path = root.getAbsolutePath() + "/test.pdf";
    document.save(path);
    document.close();
} catch (IOException e) {
    Log.e("e", e.getMessage());
}

你能帮我解决这个错误并使用 PDFbox 在表格中填写希伯来字母吗?

我使用 更改了字段文本的字体。 唯一的问题是现在文本朝向错误的方向,所以我改变了字符串的方向:

try {
    PDDocument document = PDDocument.load(getAssets().open("file.pdf"));
    PDDocumentCatalog docCatalog = document.getDocumentCatalog();
    PDAcroForm acroForm = docCatalog.getAcroForm();
    PDResources dr = acroForm.getDefaultResources();

    PDFont liberationSans = PDType0Font.load(document, getAssets().open("com/tom_roush/pdfbox/resources/ttf/LiberationSans-Regular.ttf"));
    COSName fontName = dr.add(liberationSans);
    Iterator<PDField> it = acroForm.getFields().iterator();
    while (it.hasNext()) {
        PDField field = it.next();
        if (field instanceof PDTextField) {
            PDTextField textField = (PDTextField) field;
            String da = textField.getDefaultAppearance();

            // replace font name in default appearance string
            Pattern pattern = Pattern.compile("\/(\w+)\s.*");
            Matcher matcher = pattern.matcher(da);
            String oldFontName = matcher.group(1);
            da = da.replaceFirst(oldFontName, fontName.getName());

            textField.setDefaultAppearance(da);
        }
    }

    // Fill the text field
    ((PDTextField) acroForm.getField("name")).setValue(new StringBuilder("בדיקה").reverse().toString());

    File root = android.os.Environment.getExternalStorageDirectory();

    String path = root.getAbsolutePath() + "/test.pdf";
    document.save(path);
    document.close();
} catch (IOException e) {
    Log.e("e", e.getMessage());
}