在 PostScript 中使用转换 | .NET
在 PS 文档中转换内容
本文将探讨如何在添加到 PsDocument 的矩形路径上进行不同的转换:平移、缩放、旋转和剪切。
我们将一个代码片段拆分成几段代码:开头、结尾和每个转换部分。PostScript 中的转换始终在受 “gsave” 和 “grestore” 运算符绑定的图形状态下进行。因此,在我们的 PsDocument 中,有 “WriteGraphicsSave()” 和 “WriteGraphicsRestore()” 方法。在这些方法之间,我们可以添加任何内容,包括嵌套的图形状态,并进行任何转换或剪切。这些转换不会影响外部图形状态,但会影响嵌套的图形状态。
如果我们不使用**“WriteGraphicsSave()”和“WriteGraphicsRestore()”**方法进行转换,则转换将基于上层的图形状态,并且 PsDocument 中的所有内容都将进行此转换。
一种从头开始对文档内容设置任何转换的算法包括以下步骤:
- 为生成的 PS 文件创建输出流。
- 创建 PsSaveOptions。
- 使用已创建的输出流和保存选项创建 PsDocument。
- 保存图形状态。这样,我们创建了一个新的图形状态,并将之前的图形状态放入图形状态堆栈中。
- 添加必要的转换:平移、缩放、旋转、剪切或其任意组合。在我们的代码中,我们分别展示了每个变换组件的影响,最后一次展示了三个变换组件的影响。
- 添加变换所需的必要内容。在我们的例子中,我们从该矩形创建了一个矩形 GraphicsPath,然后填充它。我们在进行任何变换之前创建一个矩形,并在当前图形状态下每次变换后填充它。
- 恢复图形状态,使其返回到之前应用的变换不受影响的状态。在我们的例子中,它是一个上层图形状态。
在这段代码中,我们从输出流和 PsSaveOptions 创建 PsDocument,将上层图形状态平移到点 100,100 以偏移第一个矩形,最后从该矩形创建第一个矩形 GraphicsPath。
1//Create an output stream for PostScript document
2using (Stream outPsStream = new FileStream(dataDir + "Transformations_outPS.ps", FileMode.Create))
3{
4 //Create save options with default values
5 PsSaveOptions options = new PsSaveOptions();
6
7 // Create new 1-paged PS Document
8 PsDocument document = new PsDocument(outPsStream, options, false);
9
10 document.Translate(100, 100);
11
12 //Create graphics path from the rectangle
13 GraphicsPath path = new GraphicsPath();
14 path.AddRectangle(new RectangleF(0, 0, 150, 100));
对于 Linux、MacOS 和其他非 Windows 操作系统,我们建议使用我们的 Aspose.Page.Drawing Nuget 包。它使用 Aspose.Drawing 后端,而不是 System.Drawing 系统库。
因此,请导入 Aspose.Page.Drawing 命名空间,而不是 System.Drawing 命名空间。在上面和下面的代码片段中,将使用 Aspose.Page.Drawing.RectangleF 代替 System.Drawing.RectangleF, 将使用 Aspose.Page.Drawing.Drawing2D.GraphicsPath 代替 System.Drawing.Drawing2D.GraphicsPath,等等。我们在 GitHub 上的代码示例包含所有必要的替换。
在这里,我们将橙色设置为上层图形状态的当前绘制颜色,并填充此矩形。
生成的 PS 文件将显示位于上层图形状态且未经过任何变换的第一个形状。
1////////////////////////////////////// No transformations ///////////////////////////////////////////////////////////////
2 //Set a paint in graphics state on upper level
3 document.SetPaint(new SolidBrush(Color.Orange));
4
5 //Fill the first rectangle that is on on the upper-level graphics state and that is without any transformations.
6 document.Fill(path);
7/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
现在,我们创建一个新的图形状态,该状态将相对于上一级图形状态沿 X 轴平移 250 个点,并向该新图形状态添加相同的矩形路径,该矩形路径以蓝色绘制。最后,我们从当前图形状态退出到上一级图形状态。
1////////////////////////////////////// Translation //////////////////////////////////////////////////////////////////////
2
3 //Save the graphics state in order to return back to this state after transformation
4 document.WriteGraphicsSave();
5
6 //Displace current graphics state on 250 to the right. So we add translation component to the current transformation.
7 document.Translate(250, 0);
8
9 //Set the paint in the current graphics state
10 document.SetPaint(new SolidBrush(Color.Blue));
11
12 //Fill the second rectangle in the current graphics state (has translation transformation)
13 document.Fill(path);
14
15 //Restore the graphics state to the previus (upper) level
16 document.WriteGraphicsRestore();
17/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
转换上层图形状态以便放置下一个矩形。
1 //Displace on 200 to the bottom.
2 document.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
3 document.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.
6 document.Scale(0.5f, 0.75f);
7
8 //Set the paint in the current graphics state
9 document.SetPaint(new SolidBrush(Color.Red));
10
11 //Fill the third rectangle in the current graphics state (has scale transformation)
12 document.Fill(path);
13
14 //Restore the graphics state to the previus (upper) level
15 document.WriteGraphicsRestore();
16//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
转换上层图形状态以便放置下一个矩形。
1 //Displace upper level graphics state on 250 to the right.
2 document.Translate(250, 0);
然后,我们创建一个新的图形状态,该状态将相对于上一级图形状态顺时针旋转 45 度,并向该新图形状态添加相同的矩形路径,该矩形路径以绿色绘制。最后,我们从当前图形状态退出到上一级图形状态。
1////////////////////////////////////// Rotation //////////////////////////////////////////////////////////////////////
2 //Save the graphics state in order to return back to this state after transformation
3 document.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.
6 document.Rotate(45));
7
8 //Set the paint in the current graphics state
9 document.SetPaint(new SolidBrush(Color.Green));
10
11 //Fill the fourth rectangle in the current graphics state (has rotation transformation)
12 document.Fill(path);
13
14 //Restore the graphics state to the previus (upper) level
15 document.WriteGraphicsRestore();
16//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
翻译上层图形状态以便将下一个矩形放置在页面的空白处。
1 //Returns upper level graphics state back to the left and displace on 200 to the bottom.
2 document.Translate(-250, 200);
然后,我们创建一个将被剪切的新图形状态,并将相同的矩形路径(用粉色绘制)添加到这个新的图形状态中。 最后,我们从当前图形状态退出到上一级图形状态。
1////////////////////////////////////// Shearing //////////////////////////////////////////////////////////////////////
2 //Save the graphics state in order to return back to this state after transformation
3 document.WriteGraphicsSave();
4
5 //Shear current graphics state. So we add shear component to the current transformation.
6 document.Shear(0.1f, 0.2f);
7
8 //Set the paint in the current graphics state
9 document.SetPaint(new SolidBrush(Color.Pink));
10
11 //Fill the fifth rectangle in the current graphics state (has shear transformation)
12 document.Fill(path);
13
14 //Restore the graphics state to the previus (upper) level
15 document.WriteGraphicsRestore();
16//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
翻译上层图形状态以便将下一个矩形放置在页面的空白处。
1 //Displace upper level graphics state on 250 to the right.
2 document.Translate(250, 0);
现在,我们创建最后一个图形状态,该状态将经历包含平移、缩放和旋转等复杂变换,并将相同的矩形路径(使用海蓝宝石颜色绘制)添加到此新的图形状态。 最后,我们从当前图形状态退出,并跳转到上一级图形状态。
1////////////////////////////////////// Complex transformation ////////////////////////////////////////////////////////
2 //Save the graphics state in order to return back to this state after transformation
3 document.WriteGraphicsSave();
4
5 //Transform current graphics state with complex transformation. So we add translation, scale and rotation components to the current transformation.
6 document.Transform(new Matrix(1.2f, -0.965925f, 0.258819f, 1.5f, 0f, 50));
7
8 //Set the paint in the current graphics state
9 document.SetPaint(new SolidBrush(Color.Aquamarine));
10
11 //Fill the sixth rectangle in the current graphics state (has complex transformation)
12 document.Fill(path);
13
14 //Restore the graphics state to the previus (upper) level
15 document.WriteGraphicsRestore();
16//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
翻译上层图形状态,以便将最后一个矩形放置在页面的空白处。
1 //Returns upper level graphics state back to the left and displace on 200 to the bottom.
2 document.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)
4 document.Fill(path);
5//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
最后我们关闭当前页面并保存文档。
1 //Close current page
2 document.ClosePage();
3
4 //Save the document
5 document.Save();
6}
请参阅 Java 中 PS 文档中的“Transformations 使用方法”。
运行此代码的结果如下:
您可以从 GitHub下载示例和数据文件。