【问题标题】:acroform field.setRichTextValue is not workingacroform field.setRichTextValue 不起作用
【发布时间】:2019-03-04 17:28:22
【问题描述】:

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

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

【问题讨论】:

  • “不起作用”是什么意思?什么都没有发生(应该发生什么?)、崩溃、异常、死机、PC 关机、核爆炸,或者您是否以时间循环结束?
  • 啊哈哈@TilmanHausherr nop 什么都没发生...我不知道它需要如何工作?我正在尝试在 pdf 输出上设置基本字符串值,关于它的工作方式和 qhat 被称为富文本值的任何文档都没有改变,谢谢您的回复
  • 请分享您的PDF;您能否澄清一下“在 pdf 输出上没有任何关于它的工作方式和 qhat 被称为富文本值的文档”的意思。什么是“pdf 输出” - 使用 Adob​​e Reader 查看?用别的软件?通常只调用field.setValue() 并设置视觉外观,除非acroForm.getNeedAppearances() 为真。另请参阅示例子项目中源代码下载中的许多示例。
  • 所以你的问题真的是“但第二个对 Chrome 浏览器没有影响”。 (你为什么不在你的问题中写这个?)所以也许它是一个 pdfium 错误。顺便说一句,您还需要设置富文本标志setRichText(true)。当您没有 PDF、没有代码、没有详细信息时,很难理解问题。设置该值将告诉“合格阅读器”(Adobe Reader 和其他一些阅读器)如何生成外观流。 setRichTextValue() 的 javadoc 中甚至对此进行了解释。
  • @bee \rtpararichtext 包中的一个 LaTeX 命令,它在内部创建 PDF 理解的富文本。为此,请查看(惊喜,惊喜!)PDF 规范!例如。 Adobe here 发布的 ISO 32000-1 副本,第 12.7.3.4 节“富文本字符串”。

标签: pdfbox richtext


【解决方案1】:

总结一下 cmets 对问题的看法以及对工作版本的一些研究......

富文本格式错误

他原始代码中的 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 富文本,而是而不是一个 LaTeX 命令,生成 PDF 富文本(如果在 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);

让 Adob​​e Acrobat (Reader) 从富文本中生成外观

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

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

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

    不是符合 PDF 规范的。

    可能是某些软件产品曾经将富文本错误地放入 V 值中,而 Adob​​e Acrobat 开始支持这种误用以获得更大的兼容性。

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

这解释了为什么 OP 的原始方法只使用了

field.setRichTextValue(val);

显示没有变化 - Adob​​e Acrobat 忽略了富文本值。

这也解释了他的观察

然后,而不是 setRichTextValue 只需使用 field.setValue("&lt;body xmlns=\"http://www.w3.org/1999/xhtml\"&gt;&lt;p style=\"color:#FF0000;\"&gt;Red&amp;#13;&lt;/p&gt;&lt;p style=\"color:#1E487C;\"&gt;Blue&amp;#13;&lt;/p&gt;&lt;/body&gt;") 就可以了!在 acrobat 阅读器中(没有展平)该字段的格式正确

但请注意,这超出了 PDF 规范。如果要生成有效的 PDF,则必须同时设置 RVV 并让后者包含前者的富文本的纯文本版本。

例如使用

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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-03
    • 2018-05-29
    • 1970-01-01
    • 2010-10-21
    • 2019-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多