Insert image into PS files
C++ API solution to work with images of PS document
Before starting to work with images in PostScript files you need to know some peculiarities that may make it easier for you to do this work. Here are a few key points:
- PostScript doesn’t support transparency, therefore translucent images added to the PS document will be converted to 24 bits-per-pixel RGB image and rendered as opaque. The only exception is 1 bits-per-pixel image masks, that is images that have every pixel either fully opaque or fully transparent. For such cases Aspose.Page’s PsDocument offers a separate method DrawTransparentImage() with a transparency threshold parameter (TT). This parameter helps to convert a translucent image to a 1-bit-per-pixel image mask. If the alpha channel of the pixel is more or equal to TT, the pixel becomes fully transparent. Otherwise, it will be fully opaque. For example, if TT is 255, only fully transparent pixels will be fully transparent. The rest of the pixels will become fully opaque.
- PostScript supports various image compression techniques, such as JPEG and LZW and the compression level can affect the image quality and file size.
- When you resize images in a PostScript file, the computer needs to figure out the color of the new pixels. It uses different techniques (called interpolation methods) to do this. The method you choose can affect how good the image looks, especially if you’re making it bigger or smaller. So, it’s important to pick the right method to get the best quality.
- Files in PS format can become significantly large when including high-resolution images or multiple images. This can impact file transfer and processing times, so optimize image data size by reducing the resolution if needed.
Keep in mind these aspects to ensure proper image integration and accurate rendering in PostScript-based workflows. But no matter what, - Aspose.Page provides you with the functionality to add images 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 images in PS files in particular follow the documentation.
To add images to PS documents we need:
Aspose.Page for C++ API which is a feature-rich, powerful, and easy-to-use document manipulation and conversion C++ API.
Open the NuGet package manager, and search for Aspose.Page.Cpp and install. You may also use the following command from the Package Manager Console.
Package Manager Console Command
PM> Install-Package Aspose.Page.Cpp
Steps to add images to a PS file.
This code below creates a PS document, adds an image to it, applies a transformation to the image, and saves the document with the specified settings. To see more examples go to Aspose.Page for C++ GitHub project .
- Create an output stream for the resulting PS file.
- Create a PsSaveOptions object with default options. Change the background color if needed..
- Create a new instance of PsDocument with the output stream, options, and false as parameters. This initializes a new PostScript document with the specified options.
- Create a new graphics state.
- Create System.Drawing.Bitmap from the image file.
- Create the necessary transformation for the image.
- Add the image to PsDocument as a fully opaque image (using the AddImage() method) if we are sure that the image is opaque or add one as a transparent image (using AddTransparentImage() method) if we are not sure that the image is opaque.
- Exit from the current graphics state to upper level one.
- Call the ClosePage() to indicate that the current page is completed.
- Save the changes using the Save() method.
Add images to PS
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.