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

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

问题描述

这是一个红色框:

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?

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

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恒定的形状和不透明度

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

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.

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

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

在透明组的背景上绘制结果时,也应应用非笔画的alpha常数.

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

应通过图形状态参数字典中的 CA ca 条目分别设置描边和不描边的alpha常数(请参见图形状态参数字典") .如先前针对软掩膜所述,图形状态下的alpha源标志应确定alpha常数是解释为形状值( true )还是不透明度值( false )

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:

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

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

此处的颜色设置操作介于路径定义(162 ... l h)和路径填充操作(f)之间.比较这是无效的,请比较规范中的图9 –图形对象,在构建路径(和可选的剪切路径运算符)之后,必须立即执行路径绘制操作. (许多PDF查看器确实接受无效的操作顺序,但是仍然无效).

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

即将进行的操作的alpha值不必恒定.相反,它可以例如由带有放射状阴影的遮罩控制.

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

对象1 0是这个XObject:

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天全站免登陆