更改字段文本的PDFbox字体

时间:2019-02-03 19:52:26

标签: android pdf fonts pdfbox hebrew

我正在编写一个使用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$4$MainActivity(MainActivity.java:128)
    at com.package.app.MainActivity$$Lambda$2.run(Unknown Source:18)
    at java.lang.Thread.run(Thread.java:764)

我试图在Stack Overflow上找到有关它的所有信息,但是我发现的答案都与填写表格无关。这都与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在表格中填写希伯来字母吗?

1 个答案:

答案 0 :(得分:0)

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

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());
}