acroform field.setRichTextValue不起作用

时间:2019-03-04 17:28:23

标签: pdfbox richtext

我有一个来自acroform的字段,我看到了field.setValue()field.setRichTextValue(...)。第一个设置正确的值,但是第二个似乎不起作用,无法显示RTF值。 这是我使用的代码:

PDDocument pdfDocument = PDDocument.load(new File(SRC));
            pdfDocument.getDocument().setIsXRefStream(true);
            PDAcroForm acroForm = pdfDocument.getDocumentCatalog().getAcroForm();
            acroForm.setNeedAppearances(false);

            acroForm.getField("tenantDataValue").setValue("Deuxième texte");
            acroForm.getField("tradingAddressValue").setValue("Text replacé");
            acroForm.getField("buildingDataValue").setValue("Deuxième texte");
            acroForm.getField("oldRentValue").setValue("750");
            acroForm.getField("oldChargesValue").setValue("655");
            acroForm.getField("newRentValue").setValue("415");
            acroForm.getField("newChargesValue").setValue("358");
            acroForm.getField("increaseEffectiveDateValue").setValue("Texte 3eme contenu");


            // THIS RICH TEXT NOT SHOW ANYTHING
            PDTextField field = (PDTextField) acroForm.getField("tableData");
            field.setRichText(true);
            String val = "\\rtpara[size=12]{para1}{This is 12pt font, while \\span{size=8}{this is 8pt font.} OK?}";
            field.setRichTextValue(val);

我希望将名为“ tableData”的字段设置为富文本值!

您可以使用以下代码下载我正在使用的PDF表单:download pdf form  并且您可以在运行此代码后下载输出并展平表单数据download output here

1 个答案:

答案 0 :(得分:2)

总结问题评论中所说的内容以及对工作版本的一些研究...

富文本格式错误

OP在其原始代码中将其用作富文本

String val = "\\rtpara[size=12]{para1}{This is 12pt font, while \\span{size=8}{this is 8pt font.} OK?}";
是他从this document那里获得的

。但是该文档是LaTeX richtext 软件包的手册,该软件包提供了“轻松”生成此类丰富字符串所需的命令和文档。上面的\rtpara...不是 PDF富文本格式,而是 生成 PDF富文本格式的LaTeX命令(如果在LaTeX上下文中执行) )。

该文档实际上甚至使用示例进行了演示

\rtpara[indent=first]{para1}{Now is the time for
    \span{style={bold,italic,strikeit},color=ff0000}{J\374rgen}
    and all good men to come to the aid of \it{their}
    \bf{country}. Now is the time for \span{style=italic}
    {all good} women to do the same.}

指令为其生成两个值,一个富文本值和一个纯文本值:

\useRV{para1}: <p dir="ltr" style="text-indent:12pt;
    margin-top:0pt;margin-bottom:0pt;">Now is the time
    for <span style="text-decoration:line-through;
    font-weight:bold;font-style:italic;color:#ff0000;
    ">J\374rgen</span> and all good men to come to the
    aid of <i>their</i> <b>country</b>. Now is the
    time for <span style="font-style:italic;">all
    good</span> women to do the same.</p>
\useV{para1}: Now is the time for J\374rgen and all
    good men to come to the aid of their country. Now
    is the time for all good women to do the same.

\useRV{para1}结果中可以看到,PDF富文本使用(缩减)HTML标记来显示富文本。

有关更多详细信息,请查阅PDF规范,例如由Adobe here

发布的ISO 32000-1副本中的12.7.3.4节“富文本字符串”

PDFBox不会创建富文本外观

原始代码中的OP使用

acroForm.setNeedAppearances(false);

这将设置一个标志,该标志声称所有表单字段都具有外观流(其中详细说明了各个表单字段的视觉外观及其内容),并且这些流代表了该字段的当前值,因此可以有效地告知PDF的下一个处理器,它可以按原样使用这些外观流,而无需自己生成它们。

不过,正如@Tilman在JavaDocs中引用的那样,

/**
 * Set the fields rich text value.
 * 
 * <p>
 * Setting the rich text value will not generate the appearance
 * for the field.
 * <br>
 * You can set {@link PDAcroForm#setNeedAppearances(Boolean)} to
 * signal a conforming reader to generate the appearance stream.
 * </p>
 * 
 * Providing null as the value will remove the default style string.
 * 
 * @param richTextValue a rich text string
 */
public void setRichTextValue(String richTextValue)

因此setRichTextValue不会为该字段创建适当的外观流。为了向PDF的下一个处理器(特别是查看器或表格展平器)发出信号,它必须生成外观,因此,需要使用

acroForm.setNeedAppearances(true);

使Adobe Acrobat(阅读器)从富文本格式生成外观

当系统要求为富文本字段生成字段外观时,Adobe Acrobat可以选择基于富文本值 RV 或纯文本值 V 。我做了一些快速检查,Adobe Acrobat似乎使用了以下策略:

  1. 如果设置了 RV ,并且 V 的值等于不带富文本标记的 RV 的值,则Adobe Acrobat会假定 RV 的值是最新的,并根据PDF规范从该富文本字符串生成外观。 否则,假定 RV 的值(如果存在的话)已经过时并且被忽略了!

  2. 否则,如果 V 值包含富文本标记,则Adobe Acrobat将该值假定为富文本并根据此样式创建外观。

    根据PDF规范,不是

    可能某些软件产品曾经错误地将富文本格式放入 V 值,而Adobe Acrobat开始支持这种滥用,以实现更大的兼容性。

  3. 否则,将 V 值用作纯字符串,并相应地生成外观。

这说明了为什么OP仅使用

的原始方法
field.setRichTextValue(val);

未显示任何更改-RTF文本值被Adobe Acrobat忽略。

这也解释了他的观察

  

然后使用setRichTextValue代替field.setValue("<body xmlns=\"http://www.w3.org/1999/xhtml\"><p style=\"color:#FF0000;\">Red&#13;</p><p style=\"color:#1E487C;\">Blue&#13;</p></body>")即可!在Acrobat Reader中(未展平),该字段的格式正确

但是请注意,这超出了PDF规范。如果要生成有效的PDF,则必须同时设置 RV V ,并使后者包含前者的纯文本格式。

例如使用

String val = "<?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=\"margin-top:0pt;margin-bottom:0pt;font-family:Helvetica;font-size:12pt\">"
        + "This is 12pt font, while "
        + "<span style=\"font-size:8pt\">this is 8pt font.</span>"
        + " OK?"
        + "</p>"
        + "</body>";
String valClean = "This is 12pt font, while this is 8pt font. OK?";
field.setValue(valClean);
field.setRichTextValue(val);

String val = "<body xmlns=\"http://www.w3.org/1999/xhtml\"><p style=\"color:#FF0000;\">Red&#13;</p><p style=\"color:#1E487C;\">Blue&#13;</p></body>";
String valClean = "Red\rBlue\r";
field.setValue(valClean);
field.setRichTextValue(val);