如何在 PDF 中设置填充 alpha [英] How to set fill alpha in PDF

查看:40
本文介绍了如何在 PDF 中设置填充 alpha的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个红框:

162 86 m 162 286 l 362 286 l 362 86 l h1 0 0 rg f

如何为其添加部分透明度?

我已经阅读了 PDF 规范的透明度部分,但我似乎只能找到模型和公式,而不是如何实际将 alpha 添加到填充中.

解决方案

正如 OP 所指出的,PDF 规范,主题是透明度.这是由于应用透明度的多种方法.以下部分解释了最适合 OP 上下文的方式:

<块引用>

11.6.4.4 恒定形状和不透明度

<块引用>

图形状态(参见图形状态")中的当前 alpha 常数参数应是两个标量值——一个用于笔画,一个用于所有其他绘画操作——用于常量形状(f_k) 或颜色合成公式中的恒定不透明度 (q_k) 分量.

<块引用>

注意 1 此参数类似于绘制基本对象时使用的当前颜色.

<块引用>

在将透明度组的结果绘制到其背景上时,也应应用非描边 alpha 常数.

<块引用>

描边和非描边 alpha 常量应分别由图形状态参数字典中的 CAca 条目设置(参见图形状态参数字典").如之前针对软蒙版所述,图形状态中的 alpha 源标志应确定将 alpha 常量解释为形状值 (true) 还是不透明度值 (false).

因此,您首先必须在页面资源中定义合适的图形状态参数字典,例如:

/Resources<<

现在您可以在内容流中使用这些命名的图形状态参数:

/GS1 gs1 0 0 rg162 86 米162 286 升362 286 升362 86 升HF

如果绘制在绿色格子上,结果如下所示:


顺便说一下,OP的原始内容流片段有错误

162 86 m 162 286 l 362 286 l 362 86 l h1 0 0 rg f

这里的颜色设置操作介于路径定义(162 ... l h)和路径填充操作(f)之间.这是无效的,比较规范中的图 9 – 图形对象,在路径构建(和一个可选的剪切路径运算符)之后,路径绘制操作必须立即跟随.(许多PDF阅读器确实接受了无效的操作命令,但它仍然无效).


即将进行的操作的 alpha 值不必是恒定的.相反,它可以例如由具有径向阴影的遮罩控制.

确实,如果您像这样定义图形状态参数:

/Resources<<>>>>>>>

对象 1 0 就是这个 XObject:

1 0 对象<</组<>/类型/X对象/资源<

你得到了上面绘制在绿色格子上的内容流片段:

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.

解决方案

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<<
  /GS1 << /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:

这篇关于如何在 PDF 中设置填充 alpha的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆