Support of Fill Layers
Support of Fill Layers With Color Fill
This article demonstrates how to fill PSD layer with Color. Please use the Aspose.PSD.FileFormats.Psd.Layers.FillLayer to add Color in PSD layer. The following code snippets loads a PSD file, access the Filllayer class and sets the color using the FillLayer.FillSettings property.
String dataDir = Utils.getDataDir(ColorFillLayer.class) + "PSD/"; | |
String sourceFileName = dataDir + "ColorFillLayer.psd"; | |
String exportPath = dataDir + "ColorFillLayer_output.psd"; | |
try (PsdImage im = (PsdImage) Image.load(sourceFileName)) { | |
for (int i = 0; i < im.getLayers().length; i++) { | |
if (im.getLayers()[i] instanceof FillLayer) { | |
FillLayer fillLayer = (FillLayer) im.getLayers()[i]; | |
if (fillLayer.getFillSettings().getFillType() != FillType.Color) { | |
throw new Exception("Wrong Fill Layer"); | |
} | |
IColorFillSettings settings = (IColorFillSettings) fillLayer.getFillSettings(); | |
settings.setColor(Color.getRed()); | |
fillLayer.update(); | |
im.save(exportPath); | |
break; | |
} | |
} | |
} |
Support of Fill Layers With Gradient Fill
This article demonstrates the usage of Gradient fill to fill PSD layer. Aspose.PSD APIs have exposed efficient & easy to use methods to achieve this goal. Aspose.PSD has exposed the GradientColorPoint and GradientTransparencyPoint classes to add Gradient effect into a layer.
The steps to fill PSD layer with Gradient fill are as simple as below:
- Load a PSD file as an image using the factory method Load exposed by Image class.
- Set settings properties of FillLayer object.
- Create list of ColorPoints with required colors and position of color.
- Create list of transparencyPoints with required opacity and position of transparency point.
- Call the FillLayer.Update method
- Save the results.
The following code snippet shows you how to add Gradient fill to fill PSD layer.
String dataDir = Utils.getDataDir(GradientFillLayer.class) + "PSD/"; | |
String sourceFileName = dataDir + "ComplexGradientFillLayer.psd"; | |
String outputFile = dataDir + "ComplexGradientFillLayer_output.psd"; | |
try (PsdImage image = (PsdImage) Image.load(sourceFileName)) { | |
for (int i = 0; i < image.getLayers().length; i++) { | |
if (image.getLayers()[i] instanceof FillLayer) { | |
FillLayer fillLayer = (FillLayer) image.getLayers()[i]; | |
if (fillLayer.getFillSettings().getFillType() != FillType.Gradient) { | |
throw new Exception("Wrong Fill Layer"); | |
} | |
IGradientFillSettings settings = (IGradientFillSettings) fillLayer.getFillSettings(); | |
if ( | |
Math.abs(settings.getAngle() - 45) > 0.25 || | |
settings.getDither() != true || | |
settings.getAlignWithLayer() != false || | |
settings.getReverse() != false || | |
Math.abs(settings.getHorizontalOffset() - (-39)) > 0.25 || | |
Math.abs(settings.getVerticalOffset() - (-5)) > 0.25 || | |
settings.getTransparencyPoints().length != 3 || | |
settings.getColorPoints().length != 2 || | |
Math.abs(100.0 - settings.getTransparencyPoints()[0].getOpacity()) > 0.25 || | |
settings.getTransparencyPoints()[0].getLocation() != 0 || | |
settings.getTransparencyPoints()[0].getMedianPointLocation() != 50 || | |
settings.getColorPoints()[0].getColor() != Color.fromArgb(203, 64, 140) || | |
settings.getColorPoints()[0].getLocation() != 0 || | |
settings.getColorPoints()[0].getMedianPointLocation() != 50) { | |
throw new Exception("Gradient Fill was not read correctly"); | |
} | |
settings.setAngle(0.0); | |
settings.setDither(false); | |
settings.setAlignWithLayer(true); | |
settings.setReverse(true); | |
settings.setHorizontalOffset(25); | |
settings.setVerticalOffset(-15); | |
List<IGradientColorPoint> colorPoints = new ArrayList<IGradientColorPoint>(); | |
Collections.addAll(colorPoints, settings.getColorPoints()); | |
List<IGradientTransparencyPoint> transparencyPoints = new ArrayList<IGradientTransparencyPoint>(); | |
Collections.addAll(transparencyPoints, settings.getTransparencyPoints()); | |
GradientColorPoint gr1 = new GradientColorPoint(); | |
gr1.setColor(Color.getViolet()); | |
gr1.setLocation(4096); | |
gr1.setMedianPointLocation(75); | |
colorPoints.add(gr1); | |
colorPoints.get(1).setLocation(3000); | |
GradientTransparencyPoint gr2 = new GradientTransparencyPoint(); | |
gr2.setOpacity(80.0); | |
gr2.setLocation(4096); | |
gr2.setMedianPointLocation(25); | |
transparencyPoints.add(gr2); | |
transparencyPoints.get(2).setLocation(3000); | |
settings.setColorPoints(colorPoints.toArray(new IGradientColorPoint[0])); | |
settings.setTransparencyPoints(transparencyPoints.toArray(new IGradientTransparencyPoint[0])); | |
fillLayer.update(); | |
image.save(outputFile, new PsdOptions(image)); | |
break; | |
} | |
} | |
} |
Stroke Effect With Color Fill
This article demonstrates how to render stroke effect with color fill. The Stroke effect is used to add strokes and borders to layers and shapes. It can be used to create solid-color lines, colorful gradients, as well as patterned borders.
The steps to render Stroke effect with Color fill are as simple as below:
- Set LoadEffectsResource property.
- Load a PSD file as an image using the factory method Load exposed by Image class and define PsdLoadOptions.
- Set settings properties of ColorFillSetting.
- Save the results.
The following code snippet shows you how to render Stroke effect with Color fill.
String dataDir = Utils.getDataDir(StrokeEffectWithColorFill.class) + "PSD/"; | |
// Implement rendering of Stroke effect with Color Fill for export | |
String sourceFileName = dataDir + "StrokeComplex.psd"; | |
String exportPath = dataDir + "StrokeComplexRendering.psd"; | |
String exportPathPng = dataDir + "StrokeComplexRendering.png"; | |
PsdLoadOptions loadOptions = new PsdLoadOptions(); | |
loadOptions.setLoadEffectsResource(true); | |
try (PsdImage im = (PsdImage) Image.load(sourceFileName, loadOptions)) { | |
for (int i = 0; i < im.getLayers().length; i++) { | |
StrokeEffect effect = (StrokeEffect) im.getLayers()[i].getBlendingOptions().getEffects()[0]; | |
ColorFillSettings settings = (ColorFillSettings) effect.getFillSettings(); | |
settings.setColor(Color.getDeepPink()); | |
} | |
// Save psd | |
im.save(exportPath, new PsdOptions()); | |
PngOptions option = new PngOptions(); | |
option.setColorType(PngColorType.TruecolorWithAlpha); | |
// Save png | |
im.save(exportPathPng, option); | |
} |