Working with Images in PS file | Java

Contents
[ Hide Show ]

Add Image in PS Document

Aspose.Page for Java library offers two methods for adding images to PS document:

It was made because PostScript doesn’t support transparency, but translucent images, however, can be rendered as a set of fully transparent and fully opaque pixels. Such images are called masks.

If we want to see the translucent image in the PS document as mask, that will better reflect the transparency of the image, we should do some checking and preprocessing of such an image. The check and the preprocessing require time. Therefore, if someone’s sure that the image is fully opaque, it is better to use the first method, because it saves execution time.

The second method recognizes whether the image is fully opaque or fully transparent or translucent. If it is fully opaque it is added as the opaque image in the first method, if it is fully transparent it is not added to the document at all, if it is the translucent image it is added as a PostScript image mask.

In the example below we demonstrate how to add a fully opaque image. Adding a transparent image will be demonstrated in the “Working with Transparency” article.

In order to add an image to a new PsDocument with Aspose.Page for Java library in this example we take the following steps:

  1. Create an output stream for the resulting PS file.
  2. Create PsSaveOptions object with default options.
  3. Create a 1-paged PsDocument with an already created output stream and save options.
  4. Create a new graphics state.
  5. Create java.awt.image.BufferedImage from image file.
  6. Create the necessary transformation for the image.
  7. Add the image to the PsDocument object.
  8. Exit from the current graphics state to upper level one.
  9. Close the page.
  10. Save the document.
 1//Create an output stream for PostScript document
 2FileOutputStream outPsStream = new FileOutputStream(dataDir + "AddImage_outPS.ps");
 3//Create 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
 9
10document.writeGraphicsSave();
11document.translate(100, 100);
12
13//Create a BufferedImage object from image file
14BufferedImage image = ImageIO.read(new File(dataDir + "TestImage Format24bppRgb.jpg"));
15
16//Create image transform
17AffineTransform transform = new AffineTransform();
18transform.translate(35, 300);
19transform.scale(3, 3);
20transform.rotate(-45);
21
22//Add image to document
23document.drawImage(image, transform, null);
24
25document.writeGraphicsRestore();
26
27//Close current page
28document.closePage();
29//Save the document
30document.save();

See working with images in PS documents in .NET.

The result of running this code is appeared as

Add Image

You can download examples and data files from GitHub.

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.