使用PDFBox更改行距

问题描述

我必须用PDFBox填写PDF表单中的多行字段。我需要在此字段中输入的值由多行组成,并由换行符分隔。我还需要更改行之间的间距。

当我尝试以下代码时,结果是:

一个地址行
第二地址行

行间距不适用,第二行的第一个字母丢失。

当我将相同的代码用于一长行文本而没有换行符时,该文本将换行到下一行,并遵守行距。而且没有字符丢失。

    File file = new File("c:/Temp/Test.pdf");

    try(PDDocument pdfDocument = PDDocument.load(file))
    {
      PDAcroForm acroForm = pdfDocument.getDocumentCatalog().getAcroForm();

      acroForm.setNeedAppearances(true);

      PDTextField field = (PDTextField)acroForm.getField("Address");
      field.setRichText(true);
      field.setValue("First address line\nSecond address line");
      field.setRichTextValue("<?xml version=\"1.0\"?>"
          + "<body xfa:APIVersion=\"Acroform:2.7.0.0\""
          + "      xfa:spec=\"2.1\""
          + "      xmlns=\"http://www.w3.org/1999/xhtml\""
          + "      xmlns:xfa=\"http://www.xfa.org/schema/xfa-data/1.0/\">"
          + "<p dir=\"ltr\" style=\"line-height:0.12\">"
          + "First address line<br>Second address line"
          + "</p>"
          + "</body>");

      pdfDocument.save(file);
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)