Rendering text to PNG via Aspose.Words API in Unity project

Hi,
I want to use this feature “Convert text to PNG in C#” in my unity project. Can you please suggest a way “how to use” this in Unity. What dependencies and plugins do I need to integrate this in my Unity project. What are the steps gonna be. Please help. I have been stuck for a week to achieve this.

I have the package installed still getting the error:
error CS0246: The type or namespace name ‘Aspose’ could not be found (are you missing a using directive or an assembly reference?)

Thanks in advance.Screenshot 2023-06-30 at 12.50.15 PM.jpg (313.5 KB)

@aadi3124,

Thanks for the screenshot.

It seems you want to use Aspose.Words API in Unity project for Text to PNG feature, please confirm? After your confirmation, we will move your query to respective forum where you will be assisted accordingly.

Hi @amjad.sahi Thanks for the reply.
Yes. Correct. I want this feature (Text to PNG) in my Unity project.

@aadi3124 I am not quite familiar with Unity. But you can create a class library in Visual Studio targeting the same .NET version as your Unity project, add NuGet reference to Aspose.Words and publish the project. After this the publish folder will contain all the required dlls.

hi @alexey.noskov
Thanks a lot for the reply. I did as you suggested. Now I am able to get the dlls.
As per the Aspose documentation says, I need to upload a file as input which gonna be converted into an image. But I want to convert a string (Text) in to an Image. Something like, suppose I have a string “ASPOSE”, and I want to convert this into an image. Just using the string as an input instead of a file.
Can I do that? Please help me with this.
Thanks a lot!

@aadi3124 You can use DocumentBuilder to achieve this:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Write("This is some string");
doc.Save(@"C:\Temp\out.png");

Or load TXT document from stream:

string test = "This is some string";
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(test)))
{
    Document doc = new Document(ms);
    doc.Save(@"C:\Temp\out.png");
}

hi @alexey.noskov
Thanks for the help.
I have tried the first way using DocumentBuilder. Image has been created but without text. Due to that I am unable to open that out put file. After debugging the issue, I found this error but I had created the dll and using the same one. As you can see ion the attached screenshots.Screenshot 2023-07-04 at 11.40.58 AM.jpg (123.0 KB)
Screenshot 2023-07-04 at 11.47.53 AM.jpg (46.5 KB)

DllNotFoundException: libSkiaSharp assembly:<unknown assembly> type:<unknown type> member:(null)
SkiaSharp.SKImageInfo..cctor () (at <380c754179c141f1918f725747c3aa56>:0)
Rethrow as TypeInitializationException: The type initializer for 'SkiaSharp.SKImageInfo' threw an exception.
SkiaSharp.SKBitmap..ctor (System.Int32 width, System.Int32 height, System.Boolean isOpaque) (at <380c754179c141f1918f725747c3aa56>:0)
Yo..cctor () (at <808682690cb241a0d80e7f20b39c5c6c>:0)
Rethrow as TypeInitializationException: The type initializer for 'Yo' threw an exception.
eTa.l () (at <808682690cb241a0d80e7f20b39c5c6c>:0)
eTa.h (System.IO.Stream a) (at <808682690cb241a0d80e7f20b39c5c6c>:0)
eTa.e (Et a, System.Drawing.SizeF b, System.IO.Stream c, Aspose.Words.Saving.ImageSaveOptions d, Wo e) (at <808682690cb241a0d80e7f20b39c5c6c>:0)
dTa.g (System.IO.Stream a) (at <808682690cb241a0d80e7f20b39c5c6c>:0)
dTa.dTauQRpe (Eab a) (at <808682690cb241a0d80e7f20b39c5c6c>:0)
nua.nuauQRpb (Eab a) (at <808682690cb241a0d80e7f20b39c5c6c>:0)
Aspose.Words.Document.gb (Eab a) (at <808682690cb241a0d80e7f20b39c5c6c>:0)
Aspose.Words.Document.Sa (System.IO.Stream a, System.String b, Aspose.Words.Saving.SaveOptions c) (at <808682690cb241a0d80e7f20b39c5c6c>:0)
Aspose.Words.Document.Save (System.String fileName, Aspose.Words.Saving.SaveOptions saveOptions) (at <808682690cb241a0d80e7f20b39c5c6c>:0)
Aspose.Words.Document.Save (System.String fileName) (at <808682690cb241a0d80e7f20b39c5c6c>:0)
TextScript.TextToImage () (at Assets/TextScript.cs:23)

@aadi3124 Aspose.Words for .NET Standard and .NET6 uses SkiaSharp to deal with graphics. The symptoms looks like .NET Standard version of Aspose.Words cannot properly use SkiaSharp to work with graphics. Please make sure libSkiaSharp native assets are properly published in your application. Usually it is in runtimes folder. Could you please try put the libSkiaSharp for the appropriate target platform next to SkiaSharp.dll?

hi @alexey.noskov
Thanks a lot again.
Sure! I checked that libSkiaSharp has been properly published as you can see here Screenshot 2023-07-04 at 1.38.47 PM.png (65.0 KB)
There are 4 different platform folder and all contains libSkiaSharp. I have tried the all one by one to put along with Skia Sharp.dll as you can see Screenshot 2023-07-04 at 1.39.00 PM.png (66.9 KB)

but still getting the same error
Screenshot 2023-07-04 at 1.43.56 PM.png (152.5 KB)

@aadi3124 I have put libSkiaSharp.dll from skiasharp.2.80.1\runtimes\win-x64\native folder and image is rendered properly in my test Unity project. You can download 2.80.1 version of SkiaSharp from here:
https://www.nuget.org/packages/SkiaSharp/2.80.1

Hi @alexey.noskov
I am already the same version of skiasharp i.e 2.80.1
you can see here Screenshot 2023-07-04 at 4.36.53 PM.jpg (282.0 KB)

You said you have tested in Unity test project.
Can you please give me a brief steps, how did you used this in Unity. That will help me little more. May be I am missing some of the steps. (Just for info I am using Unity 2022.2.1f1 editor version.)
Thanks!

@aadi3124 As I have mentioned, I am not quite familiar with Unity. I have created a simple application with C# script. Here are steps:

  1. Create a new 3D Unity project:

    The project creation process takes quite a while.
  2. Right click on Assets and create a new C# script:
  3. In GameObject menu select create and add some object on scene, foe example Cube:
  4. Drag and drop the script on the created cube object:
  5. Add some Aspose.Words code to the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using Aspose.Words;

public class HelloWorld : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        builder.Write("Hello, World!");
        doc.Save(@"C:\Temp\out.png");
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
  1. In project settings check the target .NET version:
  2. Open Visual Studio and create Class Library project targeting the same version of .NET:
  3. Add reference to Aspose.Words NuGet package and rebuild the project:
  4. Right click on the project in Visual Studio and click Open in Terminal:
  5. Run the following command
dotnet publish -r win-x64
  1. *ClassLibraryProjectPath*\bin\Debug\netstandard2.1\win-x64\publish folder contains all the required dlls. Drag and drop them into the Unity project Assets folder. Skip *.json, *.pdb and ClassLibrary1.dll, System.Runtime.dll, System.Threading.Tasks.dll, System.Text.Encoding.dll, System.Reflection.Primitives.dll, System.Reflection.Emit.ILGeneration.dll, System.Reflection.Emit.dll, System.Text.Encoding.CodePages.dll files.

Run the project and that is it, the image is generated.

Here is the final list of dlls in the Assets folder:

Aspose.Words.dll
Aspose.Words.Pdf2Word.dll
libSkiaSharp.dll
Microsoft.Win32.Registry.dll
SkiaSharp.dll
System.Buffers.dll
System.IO.dll
System.Memory.dll
System.Numerics.Vectors.dll
System.Reflection.dll
System.Runtime.CompilerServices.Unsafe.dll
System.Security.AccessControl.dll
System.Security.Principal.Windows.dll

Please note, this was my first experience with Unity.

hi @alexey.noskov Thanks a lot for the detail explanation and for the example. Sorry for the delay. Actually I was out of work for sometimes due to some reasons.
When I try to create class library in visual studio, I am seeing this type of screen

not as yours and got confused here. What should I select. Can you please guide me here. Please see the screenshot attached.
Thanks a lot again!

@aadi3124 The difference is that you are using Visual Studio for Mac, and I am using Visual Studio for Windows. On Mac you should Create .NET Standard Library and then select the required target framework on the next screen.

@alexey.noskov
Thanks a lot. I am able to create these dlls after following the steps.

but still getting this exception.

Thanks!

@aadi3124 As I can see libskiasharp is not published in the output folder:

Looks like a bug in Visual Studio for Mac. Could you please try copying the appropriate dll from SkiaSharp nuget package.