Add New Layer to PSD in Java

Adobe’s popular Photoshop application uses the PSD (Photoshop Document) as a native image file format. A PSD file may contain several layers for creating logos, brochures, and other images. In certain cases, we may need to manipulate the PSD files programmatically. In this article, we will learn how to add a new layer to a PSD in Java.

The following topics shall be covered in this article:

  1. Java PSD API to Add New Layers
  2. Add a New Layer to PSD
  3. Add a New Text Layer to PSD

Java PSD API to Add New Layers

For creating new layers in PSD files, we will be using the Aspose.PSD for Java API. It supports creating, editing, or manipulating PSD and several other supported file formats.

The PsdImage class of the API enables loading, editing, and saving PSD files. It also provides functionality to update properties, add watermarks, perform graphics operations or convert one file format to another.

The PsdImage.AddRegularLayer method allows adding a new regular layer. The AddTextLayer(string, Rectangle) method of the PsdImage class adds a new Text layer to the PSD.

Please either download the JAR of the API or add the following pom.xml configuration in a Maven-based Java application.

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>https://repository.aspose.com/repo/</url>
</repository> 
<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-psd</artifactId>
    <version>21.7</version>
</dependency>   

Add New Layer to PSD using Java

We can add a new layer to a PSD file by following the steps given below:

  1. Firstly, load a PSD file as PsdImage using the Image class.
  2. Next, prepare an object to add such as Rectangle.
  3. Then, call the addRegularLayer() method.
  4. Optionally, set the layer position such as Top, Left, Right, and Bottom.
  5. After that, assign the object to the layer using the saveArgb32Pixels() method.
  6. Finally, save the output file using the save() method.

The following code sample shows how to add a new layer to a PSD file in Java.

Add-New-Layer-to-PSD-Programmatically-using-Java

Add New Layer to PSD Programmatically using Java

Add New Text Layer to PSD using Java

We can add a new text layer to a PSD file by following the steps given below:

  1. Firstly, load the PSD file as PsdImage using the Image class.
  2. Next, define a Rectangle.
  3. Then, call the addTexLayer() method. It takes text input and a rectangle object as arguments.
  4. After that, set the layer position such as Top, Left, Right, and Bottom.
  5. Finally, save the output file using the save() method.

The following code sample shows how to add a new text layer to a PSD file in Java.

Add-New-Text-Layer-to-PSD-using-Java

Add New Text Layer to PSD Programmatically using Java

Get Free Temporary License

You can get a free temporary license to try the library without evaluation limitations.

Conclusion

In this article, we have learned how to:

  • create a new PSD image;
  • create a new regular layer;
  • insert a text layer in a PSD using Java.

Besides adding a new layer to PSD in Java, you can learn more about Aspose.PSD for Java using documentation. In case of any ambiguity, please feel free to contact us on our free support forum.

See Also