隐藏

在C#中更新psd文件中的文本图层

发布:2024/1/24 18:04:03作者:管理员 来源:本站 浏览次数:159



我想在C#中更新psd文件的特定文本层(总是相同的(用他的名字查找))。 我搜索并测试了很多没有工作的图书馆。 最近,我在GitHub上找到了这个库:https://github.com/bizzehdee/System.Drawing.PSD


我下载了源代码,尝试了它,在C#中,我可以访问我的特定层,但是,我无法更新它。 在Layer类中,有不同的属性,但我无法控制它们。


我不知道是否有人可以测试它并帮助我理解库。 我问作者,但他的最后一次行动是去年......


我希望你能帮助我!


非常感谢。

2 个答案:


答案 0 :(得分:1)


您可以尝试Aspose.PSD。它支持简单的文本层编辑和按部分编辑文本层:https://docs.aspose.com/display/psdnet/Working+With+Text+Layers


// Load a PSD file as an image and cast it into PsdImage

using (PsdImage psdImage = (PsdImage)Image.Load(dataDir + "layers.psd"))

{

   foreach (var layer in psdImage.Layers)

   {

       if (layer is TextLayer)

       {

           TextLayer textLayer = layer as TextLayer;

           textLayer.UpdateText("test update", new Point(0, 0), 15.0f, Color.Purple);

       }

   }


   psdImage.Save(dataDir + "UpdateTextLayerInPSDFile_out.psd");

}


带有文本部分的示例:


string sourceFile = dataDir + "text212.psd";

string outputFile = dataDir + "Output_text212.psd";


using (var img = (PsdImage)Image.Load(sourceFile))

{

   TextLayer textLayer = (TextLayer)img.Layers[1];

   IText textData = textLayer.TextData;

   ITextStyle defaultStyle = textData.ProducePortion().Style;

   ITextParagraph defaultParagraph = textData.ProducePortion().Paragraph;

   defaultStyle.FillColor = Color.DimGray;

   defaultStyle.FontSize = 51;


   textData.Items[1].Style.Strikethrough = true;


   ITextPortion[] newPortions = textData.ProducePortions(new string[]

   {

       "E=mc", "2\r", "Bold", "Italic\r",

       "Lowercasetext"

   },

   defaultStyle,

   defaultParagraph);


   newPortions[0].Style.Underline = true; // edit text style "E=mc"

   newPortions[1].Style.FontBaseline = FontBaseline.Superscript; // edit text style "2\r"

   newPortions[2].Style.FauxBold = true; // edit text style "Bold"

   newPortions[3].Style.FauxItalic = true; // edit text style "Italic\r"

   newPortions[3].Style.BaselineShift = -25; // edit text style "Italic\r"

   newPortions[4].Style.FontCaps = FontCaps.SmallCaps; // edit text style      "Lowercasetext"


   foreach (var newPortion in newPortions)

   {

        textData.AddPortion(newPortion);

   }


   textData.UpdateLayerData();

   img.Save(outputFile);

}


答案 1 :(得分:0)


我不使用 Aspose,因为它不是免费使用的。


您只需要 AdobeStandard.COM 组件。


Photoshop.Application app = new Photoshop.Application();

Photoshop.Document doc = app.Open(@"test.psd");


for(int i = 1; i <= doc.ArtLayers.Count; i++)

{

   string name = doc.ArtLayers[i].Name;

   

   if (name.Equals("Title1"))

   {

       doc.ArtLayers[i].TextItem.Contents = "Hello World";

   }

}