简体   繁体   中英

Can PDTextField set a text color with an alpha value?

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?

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:

图 9 的摘录

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM