Add geometrical figures to PS files

C# .NET API solution to work with shapes of PS files

 

Drawing geometrical figures in PostScript (PS) files has a few peculiarities worth noting:

  • Though PostScript uses a coordinate system where the origin (0, 0) is located at the bottom left corner of the page, Aspose.Page PsDocument transforms PS graphics state so that the origin is located at the upper left corner. It is more convenient because we usually place content of the page from the top to the bottom and from the left to the right.
  • The units of measurement in PostScript are points, where 1 point is equal to 1/72 inch. Therefore, when specifying coordinates or dimensions of geometrical figures, consider this point-based measurement.

  • In PostScript, geometrical figures are constructed by defining paths that are a series of connected line segments, curves, or both. The path is defined using a sequence of commands such as newpath, moveto, lineto, curveto, arc and closepath. They are used to control the movement of the current point and define the shape of the path. There is also a command that draws or fills rectangle (rectfill and rectstroke). Aspose.Page’s PsDocument simplifies drawing and filling geometrical shapes accepting only System.Drawing.Drawing2D.GraphicsPath object that can contain one or more subpathes, composed from connected and/or disconnected straight and curve segments. However, PsDocument contains methods also for low-level drawing, such as DrawLine(), DrawPolyLine(), Draw(Fill)Arc(), Draw(Fill)Oval(), Draw(Fill)Rect(), Draw(Fill)RoundRect(), Draw(Fill)Polygon().
  • The order in which you call the drawing commands is important. If two figures overlap, the one that is drawn later will appear on top of the previous one.

  • PostScript allows you to set separate paint for filling and outlining the figures. Thought PostScript supports many color spaces, Aspose.Page offers to use only following paints: RGB solid color, texture and hatch patterns, linear and path gradient fill as most popular.

  • As PostScript doesn’t support transparency, a translucent shape overlapping another shape hides it in the place of overlapping. Aspose.Page’s PsDocument performs pseudo-transparency for colored figures that don’t overlap other shapes but lie on a white background. In this case, the final RGB color will be calculated by taking into account the alpha value of the figure’s color.

  • It also supports various transformation operations such as scaling, rotation, translation, and shearing so you can modify the size, orientation, and position of the shapes.

  • PS provides features for clipping regions and masks, allowing you to limit the drawing to specific areas of shapes.

Knowing these aspects will let you ensure proper shape integration and accurate rendering in PostScript-based workflows. But no matter what, - Aspose.Page provides you with the functionality to draw geometrical shapes in PS files easily. With this API you can add different images using C#. To learn more about how to deal with PS files and how to work with shapes in PS files in particular follow the documentation.

To add shapes to PS documents we need:

  • Aspose.Page for .NET API which is a feature-rich, powerful, and easy-to-use document manipulation and conversion API for the C# platform.

  • Open the NuGet package manager, and search for Aspose.Page and install. You may also use the following command from the Package Manager Console.

Package Manager Console Command


    PM> Install-Package Aspose.Page

Steps to add figures to a PS file C#.

This code below creates a PS document, adds a rectangle to it, and paints it with an orange color. To see more examples go to Aspose.Page-for-.NET GitHub project .

  1. Create an output stream for the PostScript document using the FileStream class.
  2. Create a PsSaveOptions object to specify the save options for the PostScript document.
  3. Create a graphics path using the System.Drawing.Drawing2D.GraphicsPath class and specify the coordinates and dimensions of the rectangle using the System.Drawing.RectangleF class.
  4. Use SetPaint() Method to specify the color of the rectangle.
  5. Call the Fill() Method to add the specified color to the rectangle.
  6. Call the ClosePage() to indicate that the current page is completed.
  7. Save the changes using the Save() Method.

C# Code to insert a a geometrical shape to a PS file

    using Aspose.Page.EPS;
    using Aspose.Page.EPS.Device;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.IO;
    //Create an output stream for PostScript document
    using (Stream outPsStream = new FileStream(dataDir + "AddRectangle_outPS.ps", FileMode.Create))
    {
        //Create save options with A4 size
        PsSaveOptions options = new PsSaveOptions();

        // Create a new 1-paged PS Document
        PsDocument document = new PsDocument(outPsStream, options, false);

        //Create a graphics path from the first rectangle
        System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
        path.AddRectangle(new System.Drawing.RectangleF(250, 100, 150, 100));
        //Set the paint
        document.SetPaint(new System.Drawing.SolidBrush(Color.Orange));
        //Fill the rectangle
        document.Fill(path);

        //Close the current page
        document.ClosePage();

        //Save the document
        document.Save();
    }



FAQ

1. Is it possible to add geometrical figures or shapes to PostScript files?

Yes, you can add geometrical figures or shapes to PostScript files using various drawing operators provided by the PostScript language. These operators allow you to draw basic shapes such as rectangles, circles, lines, and polygons, as well as more complex geometric constructions.

2. How can I add geometrical figures or shapes to a PS file?

To add geometrical figures or shapes to a PostScript file using Aspose.Page you need to create a graphics path using the System.Drawing.Drawing2D.GraphicsPath Class and specify the coordinates and dimensions of the rectangle using the System.Drawing.Rectangle Class. Then use the SetPaint() and Fill() Methods to manage the figure color.

3. Are there predefined shapes or templates available for use in PostScript (PS) files?

While PostScript itself doesn’t provide predefined shapes or templates like some other graphics formats, you can create your own library of reusable shapes by defining them in separate PostScript files or incorporating them into your workflow as reusable snippets of code.

PS What is PS File Format

PS format is one of the page description language (PDL) formats. It is capable to contain graphic as well as text information on the page. That is why the format was supported by most of the programs for image editing. The postscript file itself is a kind of instruction for printers. It contains information on what and how to print from its page.