自動化のためのPSDファイルをテンプレートとして使用する - ビジネスカードケース

概要

この記事では、PSD/PSBファイルがいくつかの既知のテンプレート構造を持つ場合に、PSDファイル内のいくつかのレイヤーをプログラムで更新する必要がある場合によく使用されるケースについて説明しています。これは、異なる人々のために多くの名刺を作成するために使用できます(ビジネスカードケース)。または、PSDファイルを異なる言語に翻訳し、その中のいくつかのグラフィック素材を置き換える必要がある場合にも使用できます。

この記事を読んだ後、以下のことができるようになります:

todo:image_alt_text

シンプルなケース

たとえば、既知のレイヤー名を持ついくつかのPSDテンプレートがあるとします。したがって、C#を使用してPSDレイヤーを変更、更新、または置換する必要があります。まず、Aspose.PSDでテンプレートファイルを開く必要があります。

C#を介してPSDファイルを開く方法は?

// Let's load Image with Picture Frame
using (PsdImage image = (PsdImage)Image.Load(sourceFile))

todo:image_alt_text

その後、名前でレイヤーを見つける必要があります。これのためのシンプルな実装がここにあります。

PSDファイル内のレイヤーを名前で見つける方法

// Using Layer Display Name you can find it in the PSD Image Layers List
private Layer FindLayerByName(PsdImage image, string name)
{
var layers = image.Layers;
foreach (var layer in layers)
{
if (string.Equals(layer.DisplayName, name, StringComparison.InvariantCultureIgnoreCase))
{
return layer;
}
}
return null;
}

レイヤーが見つかったら、Graphicsを使用して通常の方法で更新できます。

PSDレイヤーのGraphicsに描画する方法

// Let's find layer that we want to replace
var layerToReplace = FindLayerByName(image, "LayerToReplace");
var layers = new List<Layer>(image.Layers);
using (Stream stream = new FileStream(newLayerFile, FileMode.Open))
{
var newLayer = new Layer(stream);
// Drawing of new layer on the old
var graphic = new Graphics(layerToReplace);
graphic.Clear(Color.Empty);
graphic.DrawImage(newLayer, new Rectangle(new Point(), new Size(layerToReplace.Width, layerToReplace.Height)));
}

この場合、新たに読み込んだPNG画像を既存のPSDレイヤーに描画して、古いデータは新しいファイルで失われます。

しかし、テキストも更新する必要がある場合はどうでしょうか?プロセスは似ています。テキストレイヤーを名前で見つけ、それからプログラムでPhotoshopファイルのテキストレイヤーを更新します。

C#を使用してPhotoshopでテキストレイヤーを更新する方法

// We also can update text layers in PSD file. This can be used for business card create automation
var layerToUpdateText = (TextLayer)FindLayerByName(image, "Place the name of the picture here");
// Simple way to update text
layerToUpdateText.UpdateText("Louis le Grand Dauphin");

最後に、変更内容を保存する必要があります:

変更されたPSDファイルを保存する方法 - Aspose.PSD

// Save PSD file
image.Save(outputFile);

結果の画像:

todo:image_alt_text

追加機能を備えた複雑なケース

先ほどは、PSDファイルのレイヤー内の画像を置換する最も簡単な方法を示しました。

しかし、Aspose.PSDには新しいレイヤーを追加したり、古いレイヤーを削除したり、異なるスタイルを使用してテキストレイヤーを更新するなど、さらに複雑な追加機能があります。

置換したいレイヤーを見つけ、それからLayersリスト内でそのインデックスを見つけ、その後削除してから、それをJPEGファイル(https://wiki.fileformat.com/image/jpeg/)から新しいレイヤーを作成し、同じ場所に挿入できます。

ファイルから新しいレイヤーを作成し、Aspose.PSDを使用してPSDイメージに挿入する方法

// Finding layer that we want to replace
var layerToReplace = FindLayerByName(image, "LayerToReplace");
var layers = new List<Layer>(image.Layers);
var indexOfLayer = layers.IndexOf(layerToReplace);
layers.Remove(layerToReplace);
using (Stream stream = new FileStream(newLayerFile, FileMode.Open))
{
var newLayer = new Layer(stream);
// We put new layer on the same coordinate and position in Layers as the removed layer
CopyLayerPosition(layerToReplace, newLayer);
layers.Insert(indexOfLayer, newLayer);
// Save list of changed layers
image.Layers = layers.ToArray();
}

このファイルのコードスニペットの最後では、レイヤーの位置を修正し、新しいレイヤーの配列をPsdイメージに保存します。

PsdImageレイヤーのプロパティをコピーする方法

// Way to Copy Layer Coordinates in Aspose.PSD
void CopyLayerPosition(Layer from, Layer to)
{
to.Left = from.Left;
to.Top = from.Top;
to.Right = from.Right;
to.Bottom = from.Bottom;
}

そして最後に、既存のPSDイメージ内のテキストレイヤーをC#で更新する必要があります。Aspose.PSDは[TextLayer by Portionsの更新]をサポートしています。(テキストポーション)は、スタイルと段落のプロパティのユニークな組み合わせを持っています。

PsdImage レイヤーのプロパティをコピーする方法

void UpdateTextLayer(PsdImage image)
{
// We also can update text layers in PSD file. This can be used for business card create automation
var layerToUpdateText = (TextLayer)FindLayerByName(image, "Place the name of the picture here");
// You can create comples text layers with different styles.
var textData = layerToUpdateText.TextData;
// We will use existing style of Text Layer to create new
var textPortion = textData.Items[0];
var defaultStyle = textPortion.Style;
var defaultParagraph = textPortion.Paragraph;
ITextPortion[] newPortions = textData.ProducePortions(
new string[] { "Louis XIV\r", "of France" },
defaultStyle,
defaultParagraph);
// Updating of default styles
newPortions[0].Style.FontSize = 24;
newPortions[0].Style.FillColor = Color.RoyalBlue;
newPortions[1].Style.Leading = 20;
// Removing old text
textData.RemovePortion(0);
// Addint new text portions
foreach (var newPortion in newPortions)
{
textData.AddPortion(newPortion);
}
// Applying text update
textData.UpdateLayerData();
// Fixes of the Layer position, because new text takes 2 rows
layerToUpdateText.Top = layerToUpdateText.Top - 10;
layerToUpdateText.Bottom = layerToUpdateText.Bottom - 10;
}

その結果、Jpeg、Png、J2k、Bmp、Gif、またはTiffファイルから新しいレイヤーを持つPSDテンプレートをコードで変更し、各行で異なるスタイルの複数行テキストを持つtextを更新しました。

todo:image_alt_text