英文:
Apache PdfBox how to set field font size
问题
我正在使用org.apache.pdfbox读取PDF并填充一些字段。
现在我有一个问题,那就是字体大小太大了。
我以为将字体大小设置为12会很容易。但事实证明它非常复杂。
实际上这很糟糕。有人知道如何做吗?以下是我的无样式代码:
final PDDocument document = PDDocument.load(template);
PDPage page = new PDPage(PDRectangle.A4);
document.addPage(page);
final PDAcroForm acroForm = document.getDocumentCatalog().getAcroForm();
final Iterator<PDField> it = acroForm.getFieldIterator();
for (PDField f : acroForm.getFields()) {
System.out.println(f.toString());
if (f instanceof PDTextField) {
f.set // 此处省略了一些代码
f.setValue("Some value");
}
};
英文:
I am using org.apache.pdfbox to read pdf and outfill some fields.
Now I have to problem that the font size is much too big.
I thought it would be easy to set font size to 12 . But it is very complicated .
Actually it is awful. Does anyone know how to do it ? This is my code without styling.
final PDDocument document = PDDocument.load(template);
PDPage page = new PDPage(PDRectangle.A4);
document.addPage(page);
final PDAcroForm acroForm = document.getDocumentCatalog().getAcroForm();
final Iterator<PDField> it = acroForm.getFieldIterator();
for (PDField f : acroForm.getFields()) {
System.out.println(f.toString());
if (f instanceof PDTextField) {
f.set
f.setValue("Some value");
}
};
答案1
得分: 1
根据评论讨论,以下是结果:
((PDTextField) f).getDefaultAppearance()
是
/Helv 0 Tf 0 g
这表示Helvetica字体,大小为0(可变大小),颜色为黑色(灰色色彩空间,0为黑色,1为白色)。
因此,要设置特定大小,请调用
((PDTextField) f).setDefaultAppearance("/Helv 12 Tf 0 g")
英文:
As discussed in the comments, the result of
((PDTextField) f).getDefaultAppearance()
is
/Helv 0 Tf 0 g
which means Helvetica, size 0 (= variable size), and color black (grey colorspace, 0 is black, 1 is white).
Thus to set a specific size, call
((PDTextField) f).setDefaultAppearance("/Helv 12 Tf 0 g")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论