在 PS 文件中使用转换 | Java

Contents
[ Hide Show ]

在 PS 文档中转换内容

本文将探讨如何在添加到 PsDocument 的矩形路径上进行不同的转换:平移、缩放、旋转和剪切。

我们将一个代码片段拆分成几段代码:开头、结尾和每个转换部分。PostScript 中的转换始终在受 “gsave”“grestore” 运算符绑定的图形状态下进行。因此,在我们的 PsDocument 中,有 “writeGraphicsSave()”“writeGraphicsRestore()” 方法。 在这些方法之间,我们可以添加任何内容,包括嵌套的图形状态,并进行任何转换或剪切。这些转换不会影响外部图形状态,但会影响嵌套的图形状态。

如果我们不使用**“writeGraphicsSave()”“writeGraphicsRestore()”**方法进行转换,则转换将基于上层的图形状态,PsDocument 中的所有内容都将进行此转换。

一种从头开始对文档内容进行任何转换的算法包括以下步骤:

  1. 为生成的 PS 文件创建输出流。
  2. 创建 PsSaveOptions
  3. 使用已创建的输出流和保存选项创建 PsDocument
  4. 保存图形状态。这样,我们创建了一个新的图形状态,并将之前的图形状态放入图形状态堆栈中。
  5. 添加必要的转换:平移、缩放、旋转、剪切或其任意组合。在我们的代码中,我们分别展示了每个变换组件的影响,最后一次展示了三个变换组件的影响。
  6. 添加变换所需的必要内容。在我们的例子中,我们创建了 java.awt.geom.Rectangle2D 并填充它。我们在进行任何变换之前创建一个矩形,并在当前图形状态下每次变换后填充它。
  7. 恢复图形状态,使其返回到之前应用的变换不受影响的状态。在我们的例子中,它处于上层图形状态。

在这段代码中,我们从输出流和PsSaveOptions创建PsDocument,将上层图形状态转换为点100,100以偏移第一个矩形,最后创建第一个矩形:

 1//Create an output stream for PostScript document
 2FileOutputStream outPsStream = new FileOutputStream(dataDir + "Tranformations_outPS.ps");
 3//Create a save options with A4 size
 4PsSaveOptions options = new PsSaveOptions();
 5
 6// Create new PS Document with the page opened
 7PsDocument document = new PsDocument(outPsStream, options, false);
 8
 9document.translate(100, 100);
10
11//Create a rectangle
12Shape shape = new Rectangle2D.Float(0, 0, 150, 100);

这里我们将橙色设置为上层图形状态的当前绘制颜色,并填充此矩形。

生成的 PS 文件将显示位于上层图形状态且未经过任何变换的第一个形状:

1////////////////////////////////////// No transformations ///////////////////////////////////////////////////////////////
2//Set paint in graphics state on upper level
3document.setPaint(Color.ORANGE);
4
5//Fill the first rectangle that is on on upper level graphics state and that is without any transformations.
6document.fill(shape);
7/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

现在我们创建一个新的图形状态,该状态将相对于上一级图形状态沿 X 轴平移 250 个点,并在新图形状态中添加一个相同的矩形,该矩形以蓝色绘制。最后,我们从当前图形状态退出到上一级图形状态:

 1////////////////////////////////////// Translation //////////////////////////////////////////////////////////////////////
 2//Save graphics state in order to return back to this state after transformation
 3document.writeGraphicsSave();
 4
 5//Displace current graphics state on 250 to the right. So we add translation component to the current transformation.
 6document.translate(250, 0);
 7
 8//Set paint in the current graphics state
 9document.setPaint(Color.BLUE);
10
11//Fill the second rectangle in the current graphics state (has translation transformation)
12document.fill(shape);
13
14//Restore graphics state to the previus (upper) level
15document.writeGraphicsRestore();
16/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

转换上层图形状态以放置下一个矩形:

1//Displace on 200 to the bottom.
2document.translate(0, 200);

这里我们创建一个图形状态,其 X 轴缩放 0.5,Y 轴缩放 0.75,并将相同的矩形添加到这个新的图形状态中,该矩形以红色绘制。 最后,我们从当前图形状态退出,并跳转到上一级图形状态:

 1////////////////////////////////////// Scaling //////////////////////////////////////////////////////////////////////////
 2//Save the graphics state in order to return back to this state after transformation
 3document.writeGraphicsSave();
 4
 5//Scale current graphics state on 0.5 in X axis and on 0.75f in Y axis. So we add scale component to the current transformation.
 6document.scale(0.5f, 0.75f);
 7
 8//Set paint in the current graphics state
 9document.setPaint(Color.RED);
10
11//Fill the third rectangle in the current graphics state (has scale transformation)
12document.fill(shape);
13
14//Restore graphics state to the previus (upper) level
15document.writeGraphicsRestore();
16//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

转换上层图形状态以放置下一个矩形:

1//Displace upper level graphics state on 250 to the right.
2document.translate(250, 0);

然后,我们创建一个新的图形状态,该状态将相对于上层图形状态顺时针旋转 45 度,并将相同的矩形(用绿色绘制)添加到该新图形状态中。 最后,我们从当前图形状态退出到上层图形状态:

 1////////////////////////////////////// Rotation //////////////////////////////////////////////////////////////////////
 2//Save graphics state in order to return back to this state after transformation
 3document.writeGraphicsSave();
 4
 5//Rotate current graphics state on 45 degrees around origin of current graphics state (350, 300). So we add rotation component to the current transformation.
 6document.rotate(45);
 7
 8//Set paint in the current graphics state
 9document.setPaint(Color.GREEN);
10
11//Fill the fourth rectangle in the current graphics state (has rotation transformation)
12document.fill(shape);
13
14//Restore graphics state to the previus (upper) level
15document.writeGraphicsRestore();
16//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

翻译上层图形状态以便将下一个矩形放置在页面的空白处。

1//Returns upper level graphics state back to the left and displace on 200 to the bottom.
2document.translate(-250, 200);

然后,我们创建一个将被剪切的新图形状态,并将相同的矩形(用粉色绘制)添加到这个新的图形状态中。 最后,我们从当前图形状态退出到上一级图形状态:

 1////////////////////////////////////// Shearing //////////////////////////////////////////////////////////////////////
 2//Save graphics state in order to return back to this state after transformation
 3document.writeGraphicsSave();
 4
 5//Shear current graphics state. So we add shear component to the current transformation.
 6document.shear(0.1f, 0.2f);
 7
 8//Set paint in the current graphics state
 9document.setPaint(new Color(255,192,203));
10
11//Fill the fifth rectangle in the current graphics state (has shear transformation)
12document.fill(shape);
13
14//Restore graphics state to the previous (upper) level
15document.writeGraphicsRestore();
16//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

翻译上层图形状态以便将下一个矩形放置在页面的空白处。

1//Displace upper level graphics state on 250 to the right.
2document.translate(250, 0);

现在,我们创建最后一个图形状态,该状态将经历包含平移、缩放和旋转分量的复杂变换,并将用海蓝宝石颜色绘制的相同矩形添加到这个新的图形状态。 最后,我们从当前图形状态退出到上一级图形状态:

 1////////////////////////////////////// Complex transformation ////////////////////////////////////////////////////////
 2//Save graphics state in order to return back to this state after transformation
 3document.writeGraphicsSave();
 4
 5//Transform current graphics state with complex transformation. So we add translation, scale and rotation components to the current transformation.
 6document.transform(new AffineTransform(1.2f, -0.965925f, 0.258819f, 1.5f, 0f, 50));
 7
 8//Set paint in the current graphics state
 9document.setPaint(new Color(127,255,212));
10
11//Fill the sixth rectangle in the current graphics state (has complex transformation)
12document.fill(shape);
13
14//Restore graphics state to the previus (upper) level
15document.writeGraphicsRestore();
16//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

转换上层图形状态,以便将最后一个矩形放在页面的空白处:

1//Returns upper level graphics state back to the left and displace on 200 to the bottom.
2document.translate(-250, 200);

我们再次放入上层图形状态的最后一个填充矩形表明,它没有经历下层图形状态的变换,也没有发生颜色变化。 橙色代表当前绘制的颜色:

1////////////////////////////////////// Again no transformation ////////////////////////////////////////////////////////
2//Demonstrates that current graphics state's color is orange that was set up at the beginning of the code. 
3//Fill the seventh rectangle in the current graphics state (has no transformation)
4document.fill(path);
5//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

最后我们关闭当前页面并保存文档:

1//Close current page
2document.closePage();
3
4//Save the document
5document.save();
6}

请参阅 .NET 中 PS 文档中的转换操作。


运行此代码的结果如下

添加转换

您可以从 GitHub下载示例和数据文件。

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.