Acroform Field.SetrichTextValue不起作用



我有一个来自Acroform的字段,我看到field.setValue()field.setRichTextValue(...)。第一个设置正确的值,但第二个值似乎不起作用,不显示丰富的文本值。这是代码IM使用:

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表格:下载PDF表单 在运行此代码并平坦表单数据下载

之后,您可以下载输出。

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

错误的文本格式错误

他的原始代码中的OP用作丰富的文本

String val = "\rtpara[size=12]{para1}{This is 12pt font, while \span{size=8}{this is 8pt font.} OK?}";

他从本文档中获取的。但是该文档是乳胶 richtext 的手册,提供了"轻松"生成如此丰富的字符串所需的命令和文档。 I.E.上面的rtpara...不是 pdf富文本,而是而不是生成 pdf富文本(如果在乳胶上下文中执行)。p>该文档实际上甚至使用示例

进行了证明
rtpara[indent=first]{para1}{Now is the time for
    span{style={bold,italic,strikeit},color=ff0000}{J374rgen}
    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;
    ">J374rgen</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 J374rgen 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规范,例如第12.7.3.4节在Adobe出版的ISO 32000-1副本中的"丰富文本字符串"

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 do 为该字段创建适当的外观流。为了发出信号,它必须生成外观的PDF的下一个处理器(尤其是查看器或表单平坦器),因此,需要使用

acroForm.setNeedAppearances(true);

制作Adobe Acrobat(读取器)生成来自Rich Text

的外观

当被要求生成丰富文本字段的字段外观时,Adobe Acrobat可以选择基于Rich Text Value RV 或平坦的文本值 V 。我进行了一些快速检查,Adobe Acrobat似乎使用了这些策略:

  1. 如果设置了 rv ,并且 v 的值等于 RV 的值 rv 的值是最新的,并根据PDF规范从此丰富的文本字符串中生成外观。否则,假定 RV 的值被认为已过时并忽略了!

  2. 否则,如果 v 值包含丰富的文本标记,则Adobe Acrobat假定此值是丰富的文本,并根据此样式创建外观。

    这是不是根据PDF规范。

    可能有些软件产品用于错误地将丰富的文本放入 v value和Adobe Acrobat开始支持此滥用以实现更大的兼容性。

  3. 否则 v 值用作普通字符串,并相应地生成外观。

这解释了为什么仅使用

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

没有变化 - 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读取器(不弄平)中,该字段的格式正确

不过,请注意,这超出了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 = "RedrBluer";
field.setValue(valClean);
field.setRichTextValue(val);

最新更新