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

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

Apache PdfBox how to set field font size

问题

我正在使用org.apache.pdfbox读取PDF并填充一些字段。

现在我有一个问题,那就是字体大小太大了。

我以为将字体大小设置为12会很容易。但事实证明它非常复杂。

实际上这很糟糕。有人知道如何做吗?以下是我的无样式代码:

  1. final PDDocument document = PDDocument.load(template);
  2. PDPage page = new PDPage(PDRectangle.A4);
  3. document.addPage(page);
  4. final PDAcroForm acroForm = document.getDocumentCatalog().getAcroForm();
  5. final Iterator<PDField> it = acroForm.getFieldIterator();
  6. for (PDField f : acroForm.getFields()) {
  7. System.out.println(f.toString());
  8. if (f instanceof PDTextField) {
  9. f.set // 此处省略了一些代码
  10. f.setValue("Some value");
  11. }
  12. };
英文:

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.

  1. final PDDocument document = PDDocument.load(template);
  2. PDPage page = new PDPage(PDRectangle.A4);
  3. document.addPage(page);
  4. final PDAcroForm acroForm = document.getDocumentCatalog().getAcroForm();
  5. final Iterator&lt;PDField&gt; it = acroForm.getFieldIterator();
  6. for (PDField f : acroForm.getFields()) {
  7. System.out.println(f.toString());
  8. if (f instanceof PDTextField) {
  9. f.set
  10. f.setValue(&quot;Some value&quot;);
  11. }
  12. };

答案1

得分: 1

根据评论讨论,以下是结果:

  1. ((PDTextField) f).getDefaultAppearance()

  1. /Helv 0 Tf 0 g

这表示Helvetica字体,大小为0(可变大小),颜色为黑色(灰色色彩空间,0为黑色,1为白色)。

因此,要设置特定大小,请调用

  1. ((PDTextField) f).setDefaultAppearance("/Helv 12 Tf 0 g")
英文:

As discussed in the comments, the result of

  1. ((PDTextField) f).getDefaultAppearance()

is

  1. /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

  1. ((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:

确定