PDTextField可以设置带有alpha值的文本颜色吗?

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

Can PDTextField set a text color with an alpha value?

问题

我目前正在设置默认外观字符串以设置文本颜色,像这样:

String defaultAppearance = "/Helv 12 Tf 0 0 1 rg";

field.setDefaultAppearance(defaultAppearance);

我似乎找不到有关格式化字符串的其他选项,除了用于 RGB 的 rg 或用于黑白的 g。

是否有办法将文本颜色设置为 rgba 颜色,设置文本 alpha,或者有关默认外观字符串格式的文档可以供我查阅?

英文:

I am currently setting the default appearance string to set the text color like this:

String defaultAppearance = "/Helv 12 Tf 0 0 1 rg";

field.setDefaultAppearance(defaultAppearance);

I cant seem to find anywhere if there are other options for the formatting string other than rg for RGB or g for black and white.

Is there a way to set the text color to an rgba color, set the text alpha, or documentation on the format of the default appearance string I could look at?

答案1

得分: 1

你正在寻找有关默认外观字符串格式的文档。你可以在PDF规范ISO 32000中找到它(惊喜!):

> DA 字符串(必需;可继承)默认外观字符串,其中包含一系列有效的页面内容图形或文本状态操作符,用于定义字段的文本大小和颜色。

(ISO 32000-2,表格 228 — 所有包含可变文本的字段的附加条目)

然后更详细地说,

> 默认外观字符串(DA)包含用于建立图形状态参数(例如文本大小和颜色)以显示字段的可变文本所需的任何图形状态或文本状态操作符。在此字符串中,只能出现允许在文本对象内的操作符(参见“图 9 — 图形对象”)。至少,字符串应包括一个 Tf(文本字体)操作符,以及它的两个操作数,字体和大小。指定的字体值应与默认资源字典的 Font 条目中的资源名称匹配(引用自交互表单字典的 DR 条目;参见“表 224 — 交互表单字典中的条目”)。大小为零意味着字体将自动调整大小:其大小将计算为实现相关的函数。

> 默认外观字符串最多应包含一个 Tm(文本矩阵)操作符。如果存在此操作符,则交互式 PDF 处理器应替换水平和垂直平移分量,使用它确定的定位值,这些值基于字段值、对齐(Q)属性和所采用的任何布局规则。如果默认外观字符串不包含 Tm 操作符,则查看器应在默认外观字符串之后、变量文本的文本定位和文本显示操作符之前在外观流中插入一个操作符(带有适当的水平和垂直平移分量)。

(ISO 32000-2 第12.7.4.3节 — 可变文本)

根据图 9,在文本对象中允许的操作类别为:

PDTextField可以设置带有alpha值的文本颜色吗?

在这些操作符中,文本显示和标记内容操作符不是图形状态或文本状态操作符,因此可用的操作符为:

  • 一般图形状态 w、J、j、M、d、ri、i、gs
  • 颜色 CS、cs、SC、SCN、sc、scn、G、g、RG、rg、K、k
  • 文本状态 Tc、Tw、Tz、TL、Tf、Tr、Ts
  • 文本定位 Td、TD、Tm、T*

(ISO 32000-2,表格 50 — 操作符类别)

显然,我不能在这里复制所有这些操作符的规范。

不过特别值得注意的是一般图形状态操作符 gs,它允许您使用 ExtGState 资源来设置透明度,正如 Tilman 在您的问题评论中提议的那样。

不过,需要警告一下,许多 PDF 处理器只会期望字体设置(Tf)和简单的颜色设置(rg / g / k)操作,并会忽略其他所有操作。

英文:

You are looking for documentation on the format of the default appearance string. You can find that (surprise!) in the pdf specification ISO 32000:

>DA string (Required; inheritable) The default appearance string containing a sequence of valid page-content graphics or text state operators that define such properties as the field’s text size and colour.

(ISO 32000-2, Table 228 — Additional entries common to all fields containing variable text)

And thereafter in more detail,

>The default appearance string (DA) contains any graphics state or text state operators needed to establish the graphics state parameters, such as text size and colour, for displaying the field’s variable text. Only operators that are allowed within text objects shall occur in this string (see "Figure 9 — Graphics objects"). At a minimum, the string shall include a Tf (text font) operator along with its two operands, font and size. The specified font value shall match a resource name in the Font entry of the default resource dictionary (referenced from the DR entry of the interactive form dictionary; see "Table 224 — Entries in the interactive form dictionary"). A zero value for size means that the font shall be auto-sized: its size shall be computed as an implementation dependent function.

>The default appearance string shall contain at most one Tm (text matrix) operator. If this operator is present, the interactive PDF processor shall replace the horizontal and vertical translation components with positioning values it determines to be appropriate, based on the field value, the quadding (Q) attribute, and any layout rules it employs. If the default appearance string contains no Tm operator, the viewer shall insert one in the appearance stream (with appropriate horizontal and vertical translation components) after the default appearance string and before the text-positioning and textshowing operators for the variable text.

(ISO 32000-2 section 12.7.4.3 — Variable text)

According to that figure 9 the allowed operation classes in a text object are are:

PDTextField可以设置带有alpha值的文本颜色吗?

Among these the Text Showing and Marked Content operators are not graphics state or text state operators, thus the available operators are:

  • General graphics state w, J, j, M, d, ri, i, gs
  • Color CS, cs, SC, SCN, sc, scn, G, g, RG, rg, K, k
  • Text state Tc, Tw, Tz, TL, Tf, Tr, Ts
  • Text positioning Td, TD, Tm, T*

(ISO 32000-2, Table 50 — Operator categories)

Obviously I cannot copy the specification of all these operators here.

Of special interest, though, is the general graphics state operator gs which allows you to use an use an ExtGState resource to set transparency as already proposed by Tilman in a comment to your question.

One word of warning, though, many pdf processors will merely expect a font setting (Tf) and a simple color setting (rg / g / k) operation and ignore everything else.

huangapple
  • 本文由 发表于 2020年4月11日 06:53:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/61149820.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定