การใช้ไฟล์ PSD เป็นเทมเพลตสำหรับออโตเมชัน - กรณีนามบัตรธุรกิจ

ภาพรวม

บทความนี้อธิบายกรณีที่ใช้บ่อยเมื่อคุณต้องการอัปเดตเลเยอร์บางส่วนใน ไฟล์ PSD โดยโปรแกรมอย่างสร้างเชิงลักษณะเทมเพลตที่ทราบ ซึ่งสามารถใช้สร้างนามบัตรขนาดใหญ่สำหรับผู้คนต่างๆ (กรณีนามบัตรธุรกิจ) หรือคุณอาจต้องการแปลงไฟล์ PSD เป็นภาษาต่างๆ พร้อมทำการแทนที่วัสดุกราฟิกบางส่วนในนั้น

หลังจากที่อ่านบทความนี้คุณจะทราบได้ว่าคุณสามารถทำดังนี้:

todo:image_alt_text

กรณีง่ายๆ

ตัวอย่างเช่น คุณมีเทมเพลต PSD บางชิ้นที่มีชื่อเลเยอร์ทราบเลย ดังนั้นคุณจำเป็นต้องเปลี่ยน อัปเดต หรือแทนที่เลเยอร์ PSD ผ่าน C# ก่อนอื่นคุณต้องเปิดไฟล์เทมเพลตด้วย Aspose.PSD

วิธีเปิดไฟล์ PSD ผ่าน C#?

// 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 ที่มีอยู่ เพื่อที่ข้อมูลเก่าจะหายไปในไฟล์ใหม่

แต่ถ้าเราต้องการอัปเดตข้อความอีกด้วย? กระบวนการจะคล้ายกัน ค้นหา Text Layer โดยชื่อของมันแล้วเราก็อัปเดตเลเยอร์ข้อความของไฟล์ Photoshop โดยโปรแกรมมาตามนี้

วิธีอัปเดตเลเยอร์ข้อความใน Photoshop โดยใช้ C#

// 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 สามารถมอบคุณสมบัติเพิ่มเติมที่ซับซ้อนมากขึ้น เช่น เพิ่มเลเยอร์ใหม่, ลบเลเยอร์เก่า และอัปเดตเลเยอร์ข้อความโดยใช้อัตราต่างๆในข้อความหลายบรรทัด

เราสามารถค้นหา Layer ที่ต้องการแทนที่ จากนั้นเราค้นพบดัชนีของมันในรายการเลเยอร์ ลบลง และแทนที่ด้วยเลเยอร์ใหม่หลังจากสร้างมันจาก ไฟล์ 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;
}

และสุดท้ายเราจำเป็นที่จะอัปเดตเลเยอร์ข้อความในภาพ PSD ที่มีโดย C# Aspose.PSD รองรับการอัปเดตของ TextLayer โดยต่อเนื่อง

การกำหนด Properties ของ PsdImage Layer

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 และข้อความหลายบรรทัด ซึ่งมีสไตล์ต่างๆ ในแต่ละแถว

todo:image_alt_text