Apache PdfBox如何设置字段字体大小

huangapple go评论63阅读模式
英文:

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&lt;PDField&gt; it = acroForm.getFieldIterator();

    for (PDField f : acroForm.getFields()) {
        System.out.println(f.toString());

        if (f instanceof PDTextField) {
            f.set
            f.setValue(&quot;Some value&quot;);
        }
    };

答案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(&quot;/Helv 12 Tf 0 g&quot;)

huangapple
  • 本文由 发表于 2020年8月31日 17:20:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63668098.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定