使用PSD文件作为模板进行自动化处理 - 商业名片案例
概述
本文描述了在需要通过编程方式更新PSD文件中的一些图层时经常使用的情况,其中PSD/PSB文件具有某种已知的类似模板的结构。这可用于为不同人群创建大量的名片(商业名片案例)。或者您需要将PSD文件翻译成不同语言,并用其中的一些图形素材替换之。
阅读本文后,您将了解如何实现以下操作:
简单情况
例如,您有一个包含已知图层名称的PSD模板。因此,您需要通过C#更改、更新或替换PSD图层。首先,您需要使用Aspose.PSD打开模板文件。
如何通过C#打开PSD文件?
// Let's load Image with Picture Frame | |
using (PsdImage image = (PsdImage)Image.Load(sourceFile)) |
然后,我们需要根据名称查找要替换的图层。以下是此功能的简单实现。
如何通过其名称在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图层上绘制
// 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))); | |
} |
在这种情况下,我们在现有PSD图层上绘制了新加载的PNG图像,因此旧数据将在新文件中丢失。
但如果我们还需要更新文本怎么办?过程类似。通过其名称查找文本图层,然后我们以编程方式更新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"); |
最后,我们需要保存更改:
如何使用Aspose.PSD保存更改的PSD文件
// Save PSD file | |
image.Save(outputFile); |
最终的图像是:
具有附加功能的复杂情况
以上我们展示了在PSD文件的图层中替换图像的最简单方式。
但是Aspose.PSD可以提供更复杂的附加功能,如添加新图层、移除旧图层,并使用不同的样式在多行文本中更新文本图层。
我们可以找到要替换的图层,然后在图层列表中找到其索引,将其删除,并在从Jpeg文件创建新图层后插入到相同位置。
从文件创建新图层并将其插入到PSD图像中使用Aspose.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; | |
} |
最后,我们需要通过C#更新现有PSD图像中的文本图层。Aspose.PSD支持按部分更新文本图层。每个文本部分具有唯一的样式和段落属性组合。
如何复制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; | |
} |
结果,我们通过代码更改了PSD模板,添加了来自Jpeg、Png、J2k、Bmp、Gif或Tiff文件的新图层和不同风格的多行文本。