Error while instantiating Object of class "Aspose.Pdf.TextStamp" .NET C#

Hi Team,
I am not able to instantiate an object of class TextStamp when I debug locally. However this works sometimes (Doesn’t work all of the time) in the deployed dev environment which is a Linux based image.

Please find the below code snippet and the exception.

try
{
    TextStamp textStamp = new TextStamp("Sample Stamp");
}
catch (Exception ex)
{
throw;
}

Stack trace -
at #=q4IEUrftjYU$par8Q9LJ00tW0gvtn27t33MOXq5jf7M0=.#=zm2cl7e0sXMFI6CAz8GMWXa_CMUfb(Byte[] #=zk9pPDeEfDHy22YjFMAlVx0cxiTST)
.
.
More encrypted # hash unreadable stacktrace.
.
.
at System.Runtime.Loader.AssemblyLoadContext.InvokeResolveEvent(ResolveEventHandler eventHandler, RuntimeAssembly assembly, String name)
at System.Reflection.RuntimeAssembly.GetManifestResourceStream(String name)
.
.
More encrypted # hash unreadable stacktrace.
.
.
at Aspose.Pdf.Text.FontRepository.FindFont(String fontName)
at Aspose.Pdf.TextStamp.#=z6LG_W723YVs7()
at Aspose.Pdf.TextStamp…ctor(String value)
at Program.$(String[] args) in C:\Users\MNagathane\source\repos\ConsoleApp2AsposeTest\ConsoleApp2AsposeTest\Program.cs:line 9

Solutions tried -

  1. Installed all necessary fonts in local windows fonts - Still same issue persists

OS : Windows
Aspose Total v21.9.0 Licensed
.NET 5

It seems you are encountering an issue when trying to instantiate the TextStamp class in your .NET application, particularly in a Linux-based environment. The stack trace indicates that the problem may be related to font resolution, as the TextStamp constructor relies on the availability of the specified font.

Here are a few suggestions to help you troubleshoot and resolve the issue:

  1. Font Availability: Ensure that the font you are trying to use (“Sample Stamp” in your case) is installed and accessible in the environment where your application is running. On Linux, you may need to install the necessary fonts manually, as the default fonts available on Windows may not be present.

  2. Font Repository: You can use the FontRepository class to check if the font is available in your application. Here’s a code snippet to help you verify the available fonts:

    var availableFonts = FontRepository.Families;
    foreach (var font in availableFonts)
    {
        Console.WriteLine(font.Name);
    }
    
  3. Fallback Font: If the specific font is not available, consider using a fallback font that is guaranteed to be present in both your local and deployed environments. You can set the font for the TextStamp explicitly if needed.

  4. Debugging: Since the issue occurs intermittently, try to log additional information when the exception is thrown. This can help you identify if there are specific conditions under which the error occurs.

  5. Environment Consistency: Ensure that your local development environment closely matches the deployed environment in terms of configuration and installed libraries. Differences in the environment can lead to inconsistent behavior.

If the problem persists after trying these suggestions, please provide additional details about the fonts installed in your Linux environment and any other relevant configurations. This information can help in diagnosing the issue further.

Feel free to refer to the Aspose documentation for more details on adding text stamps and managing fonts [1][2].

Sources:
[1]: Add Text stamps in PDF C#|Aspose.PDF for .NET
[2]: Add Text Stamp In PDF File | Aspose.PDF for .NET API Reference

  1. I have already tried using the exact image on local system using same docker file - Issue persists.

  2. If I am not able to create instance of a Texstamp class itself , how can I try anything else ?

@maharudra7
I tested your issue on current latest version of Aspose PDF which is 24.7 , issue seems to be solved
if there’s no reason to keep version you’re using I recommend to update version of Aspose PDF and check if it’s still present

Update to latest Version resolves the issue, Thanks . However this comes at a renewal cost which would require a proper justification for this purchase. What I am not able to understand is inconsistent behavior. Sometimes it works and sometimes it doesn’t.

@maharudra7
Do you mean behaviour that you mentioned initially ?
I can check stack trace - seemingly it has to do something with fonts
But, as product progresses, most of this issues are resolved and it’s hard for development team to recreate state of Aspose PDF library 3 years ago as it constantly changes and improves