How to Load, Inspect and Save a PSD Document in Java
View as Markdown

How to Load, Inspect and Save a PSD Document in Java

This guide shows how to open a PSD or PSB file, read its header, handle a file that cannot be loaded, and write the document back. PsdImage is the entry point; Image is its base class.

Step-by-Step Guide

Step 1: Add the Library

<dependency>
  <groupId>org.aspose</groupId>
  <artifactId>aspose-psd-foss</artifactId>
  <version>26.11.0</version>
</dependency>

The library needs Java 11 or later.


Step 2: Load the File

Call PsdImage.load(String) with the path of the file. PSD and PSB files load through the same call. To read from a stream, call PsdImage.load(InputStream); it declares the checked PsdLoadException and does not close your stream. Image.load returns an Image that is a PsdImage for these files.


Step 3: Read the Header

getWidth() and getHeight() give the size in pixels. getBitsPerChannel(), getColorMode(), getCompression() and getChannelsCount() describe how the document is stored. RGB, CMYK and indexed-color PSD files load, and RGB PSB files load.


Step 4: Handle Load Errors

Input with an invalid signature or header, an empty stream, or data that ends before the end of the image resources or layer sections throws PsdLoadException from load(InputStream). load(String) reports the same problems as a RuntimeException that wraps it. A missing file is a FileNotFoundException and a null path is an IllegalArgumentException. PsdLoadException is not an IOException, so a catch (IOException) does not catch it.


Step 5: Save the Document

Call save(String) to write a file or save(OutputStream) to write a stream. save(OutputStream) closes the stream that you pass in. Close the image when you are done, for example with try-with-resources.

Things to Know

  • The library does not render or export images and does not decode pixels.
  • getVersion() returns 6 for PSD and PSB documents alike, and PsdImage does not report which of the two a file is.
  • The loader does not check the merged image data at the end of the file, so a file that is cut off or damaged there still loads.

See Also