Can we change Font of text in text Layer of a psd file in java?

I wanted to change the font from Arial to Calibri of my text in textlayer. can we do that?

@mail9deep

Please try using following sample on your end to change the font family for the text in layer.

//Creates an instance of Image
com.aspose.psd.Image image = new com.aspose.psd.fileformats.psd.PsdImage(500, 500);
//Creates and initialize an instance of Graphics class
com.aspose.psd.Graphics graphics = new com.aspose.psd.Graphics(image);

//Clears Graphics surface
graphics.clear(com.aspose.psd.Color.getWheat());

//Creates an instance of Font
com.aspose.psd.Font font = new com.aspose.psd.Font("Times New Roman", 16);

//Create an instance of SolidBrush having Red Color
com.aspose.psd.brushes.SolidBrush brush = new com.aspose.psd.brushes.SolidBrush(com.aspose.psd.Color.getRed());

//Draw a String
graphics.drawString("Created by Aspose.PSD for .Net", font, brush, new PointF(100, 100));

// create export options.
com.aspose.psd.imageoptions.GifOptions options = new com.aspose.psd.imageoptions.GifOptions();

// save all changes
image.save("C:\\temp\\output.gif", options);

Hi @mudassir.fayyaz,

I don’t want to draw string as i am updating text of TextLayer .

Have tried draw string also but it’s not working.

Layer textLayer = findLayer(“AHORRA HOY”,psdImage);
TextLayer txtlyr = (TextLayer) textLayer;
txtlyr.updateText(“Some text”);

Here findLayer will just return a layer with the name “AHORRA HOY”.

In the above code, I just want to change the font of this “Some text”.
I am able to change font size and color but unable to change Font.

@mail9deep

We regret to share that at present we don’t provide API to change Font for layer at this moment. We have already added a ticket for this with ID PSDNET-806 in our issue tracking system for providing requested support. This thread has been linked with the issue so that you may be notified once the support will be included.

Hi @mudassir.fayyaz,

Is there any update on this ticket?

@mail9deep

At present the requested feature is unavailable in API and I request for your patience. We will share the good news with you as soon as the feature will be available.

The issues you have found earlier (filed as PSDNET-806) have been fixed in this update.

@mudassir.fayyaz do you by any chance have a piece of code showing how to supply the font to a text layer? :grinning:

@mortenma,

The following code example shows how to change the font in TextLayer and how to add support of not installed fonts:

        // The path to the folder with the fonts that you want to use in PsdImage, but which are not installed on your PC.
        String fontsFolder = "FontsFolder";

        // Add support for my, not installed, fonts.
        FontSettings.setFontsFolders(new String[] { fontsFolder }, true);

        PsdImage image = (PsdImage)Image.load("file.psd");
        try
        {
            TextLayer textLayer = (TextLayer)image.getLayers()[1];

            // Sets the font name for each portion in TextLayer.
            ITextPortion[] portions = textLayer.getTextData().getItems();
            for (int i = 0; i < portions.length; i++)
            {
                ITextPortion textPortion = portions[i];
                // You can use the name of the fonts installed on your PC or the fonts added using the "FontSettings.SetFontsFolders" method.
                textPortion.getStyle().setFontName(FontSettings.getAdobeFontName("Times New Roman"));
            }

            // Update preview of text layer.
            textLayer.getTextData().updateLayerData();

            image.save("output.psd");
        }
        finally
        {
            image.dispose();
        }

Thanks, Yaroslav.
I got it working in LinqPad 7. However, when I move the code to an asp.net core / .NET5 application I’m getting the exception below when I call “FontSettings.GetAdobeFontName(source_font_name);” (and later when I call textLayer.TextData.UpdateLayerData() without setting the font.

“BinaryFormatter serialization and deserialization are disabled within this application. See Deserialization risks in use of BinaryFormatter and related types - .NET | Microsoft Learn for more information.”

The Deserialization risks in use of BinaryFormatter and related types - .NET | Microsoft Learn link redirects to this article “BinaryFormatter security guide” Deserialization risks in use of BinaryFormatter and related types - .NET | Microsoft Learn

It appears that .NET 5 introduced breaking changes around “BinaryFormatter”. See Breaking changes in .NET 5 - .NET | Microsoft Learn

If I move all code to a .NET console app it works (seems to be a asp.net issue)

I’m not sure I got it right, however, if I did, are there any alternatives to setting the layer font when working in a asp.net core / .NET 5 application?

Thanks for your help.

Best regards
Morten

My work-a-round was to use aspose.pdf with text-replacing instead

1 Like