How to set fill alpha in PDF

十年热恋 提交于 2019-12-29 07:11:49

问题


This is a red box:

162 86 m 162 286 l 362 286 l 362 86 l h
1 0 0 rg f

How can I add partial transparency to it?

I've read the transparency section of the PDF spec, but I can only seem to find models and formulas, not how to actually add alpha to a fill.


回答1:


As the OP indicated, there is a whole section in the PDF specification on the topic of Transparency. This is due to a multitude of ways to apply transparency. The most appropriate way for the OP's context is explained in the following section:

11.6.4.4 Constant Shape and Opacity

The current alpha constant parameter in the graphics state (see “Graphics State”) shall be two scalar values—one for strokes and one for all other painting operations—to be used for the constant shape (f_k) or constant opacity (q_k) component in the colour compositing formulas.

NOTE 1 This parameter is analogous to the current colour used when painting elementary objects.

The nonstroking alpha constant shall also be applied when painting a transparency group’s results onto its backdrop.

The stroking and nonstroking alpha constants shall be set, respectively, by the CA and ca entries in a graphics state parameter dictionary (see “Graphics State Parameter Dictionaries”). As described previously for the soft mask, the alpha source flag in the graphics state shall determine whether the alpha constants are interpreted as shape values (true) or opacity values (false).

Thus, you first have to define an appropriate graphics state parameter dictionary in the page resources, e.g.:

/Resources<</ExtGState<<
  /GS1 <</ca 0.5>>
>>>>

Now you can use these named graphics state parameters in your content stream:

/GS1 gs
1 0 0 rg
162 86 m
162 286 l
362 286 l
362 86 l
h
f 

If drawn upon a green lattice, the result looks like this:


By the way, there was an error in the OP's original content stream fragment

162 86 m 162 286 l 362 286 l 362 86 l h
1 0 0 rg f

The color setting operation here is between the path definition (162 ... l h) and the path filling operation (f). This is invalid, compare Figure 9 – Graphics Objects in the specification, after path construction (and an optional clipping path operator) the path painting operation must follow immediately. (Numerous PDF viewers do accept the invalid operation order but it's invalid nonetheless).


The alpha value for the upcoming operations need not be constant. Instead it can e.g. be governed by a mask with, say, a radial shading.

Indeed, if you define the graphics state parameters like this:

/Resources<</ExtGState<<
  /SMask<</Type/Mask/S/Luminosity/G 1 0 R
>>>>

and the object 1 0 is this XObject:

1 0 obj
<<
  /Group<</CS/DeviceGray/S/Transparency>>
  /Type/XObject
  /Resources<</Shading<<
    /Sh1<<
      /Coords[262 186 10 262 186 190]
      /ColorSpace/DeviceRGB
      /ShadingType 3
      /Extend[true true]
      /Function <</Domain[0 1]/FunctionType 2/N 1/C1[0 0 0]/C0[1 1 1]>> 
    >> 
  >>>>
  /Subtype/Form
  /BBox[0 0 500 400]
  /Matrix [1 0 0 1 0 0]
  /Length 10
  /FormType 1
>>stream
  /Sh1 sh
endstream 

you get for the above content stream fragment drawn upon a green lattice:



来源:https://stackoverflow.com/questions/28484590/how-to-set-fill-alpha-in-pdf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!