Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active March 11, 2022 09:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/2a8c10d2eeb5bcfa4e122a9d0bd969e3 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/2a8c10d2eeb5bcfa4e122a9d0bd969e3 to your computer and use it in GitHub Desktop.
Manipulate PSD Files via Java | Aspose.PSD for Java

Manipulate PSD Files using Java Photoshop API

Java codes listed below demonstrating how to convert Adobe® Photoshop® PSD to JPG, PNG, BMP, GIF, TIFF Images and PDF using Java

More details at https://products.aspose.com/psd/java/conversion/

Here is the list of all supported image formats https://docs.aspose.com/psd/java/supported-file-formats/

For code integration there few prerequisite listed in installation section below.

Installation

Download its latest version directly from Maven and install it within your Maven-based project by adding the configurations to the pom.xml.

More About Java Photoshop API

Home | Product Page | Docs | Demos | API Reference | Examples | Blog | Search | Free Support | Temporary License

// Load a PSD file as an image and cast it into PsdImage
PsdImage psdImage = (PsdImage)Image.load(dataDir + "layers.psd");
// Create graphics object to perform draw operations.
Graphics graphics = new Graphics(psdImage);
// Create font to draw watermark with.
Font font = new Font("Arial", 20.0f);
// Create a solid brush with color alpha set near to 0 to use watermarking effect.
SolidBrush brush = new SolidBrush(Color.fromArgb(50, 128, 128, 128));
// Specify string alignment to put watermark at the image center.
StringFormat sf = new StringFormat();
sf.setAlignment(StringAlignment.Center);
sf.setLineAlignment(StringAlignment.Center);
// Draw watermark using font, partly-transparent brush and rotation matrix at the image center.
graphics.drawString("Some watermark text", font, brush, new RectangleF(0, 0, psdImage.getWidth(), psdImage.getHeight()), sf);
// Export the image into PNG file format.
psdImage.save(dataDir+"AddWatermark_output.png", new PngOptions());
String destName = dataDir + "output";
// load an existing document
Image image = Image.load(dataDir + "sample.psd");
image.save(destName + ".bmp", new BmpOptions());
image.save(destName + ".gif", new GifOptions());
image.save(destName + ".jpeg", new JpegOptions());
// Load an existing PSD image as Image
Image psdToImage = Image.load("sample.psd");
// Create an instance of PngOptions class
PngOptions pngOptions = new PngOptions();
// BmpOptions, GifOptions, JpegOptions, Jpeg2000Options other relevant image options
// Call the save method, provide output path and export options to convert PSD file to relevant image.
psdToImage.save("psd-to-png.png", pngOptions);
// In case to convert specific layers in the PSD file to the raster image
pngOptions.setColorType(PngColorType.TruecolorWithAlpha);
// Loop through the list of layers
for (int i = 0; i < psdToImage.getLayers().length; i++) {
// Convert and save the layer to PNG file format.
psdToImage.getLayers()[i].save(String.format("layer_out{0}.png", i + 1), pngOptions);
}
// Load image
Image psdtopdf = Image.load("sample.psd");
// Create PDF options
PdfOptions options = new PdfOptions();
// Convert PSD to PDF
psdtopdf.save("psd-to-pdf.pdf", options );
String destName = dataDir + "output.jpg";
// Load an existing image into an instance of RasterImage class
Image image = Image.load(dataDir + "sample.psd");
image.rotateFlip(RotateFlipType.Rotate270FlipNone);
image.save(destName, new JpegOptions());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment