gpt4 book ai didi

android - 更改字段文本的 PDFbox 字体

转载 作者:行者123 更新时间:2023-11-29 02:22:29 30 4
gpt4 key购买 nike

我正在编写一个使用 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 在表格中填写希伯来字母吗?

最佳答案

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

关于android - 更改字段文本的 PDFbox 字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54506916/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com