Using PSD files as templates for automation - Business Cards Case
Overview
This article describes often used cases when you need to update some layers In PSD File programmatically, where the PSD/PSB file has some known template-like structure. This can be used to create a big amount of Business Cards for different people (Business Cards Case). Or you need to make a translation of the PSD file to different languages with the replacement of some graphic material in it.
After reading this article you will know how you can make this:
Simple case
For example, you have some PSD Template with the known Layer Names. So you need to change, update or replace PSD Layer via C#. Firstly you need to open the template file with Aspose.PSD.
How to open PSD File via C#?
Then we new need to find a layer that we want to replace by its name. Here is a simple implementation for this.
How to find the layer in PSD file by its name
When the layer is found, we can update it in a common way, using Graphics:
How to Draw on the PSD layer Graphics
In this case, we draw a newly loaded PNG image on the existing PSD layer, so the old data will be lost in the new file.
But what if we also need to update text? The process will be similar. Find Text Layer by its name then we programmatically update the Text Layer of Photoshop File.
How to update Text Layer in Photoshop using C#
At the end we need to save our changes:
How to save changed PSD file using Aspose.PSD
The resulting image:
A complex case with additional features
Above we showed the most simple way to replace image in a layer of PSD File.
But Aspose.PSD can offer more complex additional features like adding a new layer, removing old layers, and update of text layer using different styles in multi-line text.
We can find Layer that we want to replace, then we find its index in Layers List, remove him and insert a new Layer after the creation of it from Jpeg File to the same place.
Create a new layer from file and insert it to PSD Image using Aspose.PSD
At the end of this file code snippet, we correct the layer position and save new Layers’ array to Psd Image.
How to copy PsdImage Layer properties
And after all, we need to update text layers in the existing PSD image by C#. Aspose.PSD supportsupdating of TextLayer by Portions. Each text portion has a unique combination of Style and Paragraph properties.
How to copy PsdImage Layer properties
As a result, we have changed the PSD template via code with a new Layer from Jpeg, Png, J2k, Bmp, Gif, or Tiff file and multi-line text with different styles in each row.