Unabel to parse the font stream error when using a font from Assets folder android

Hi,


In a net8.0-android project i have added a ttf font in Assets folder and i get the stream using Assets.Open and pass it to PdfTrueTypeFont constructor. When doing so i get the error unable to parse the font stream error. I am not sure why we are getting this error. I can work around by writing that stream to a new file in cache folder and create a stream using System.IO.File.OpenRead with that file and passing it to PdfTrueTypeFont solves the problem. But it would be nice if i am not doing this extra step.


Thanks


4 Replies

RB Ravikumar Baladhandapani Syncfusion Team December 1, 2023 01:26 PM UTC

We suspect that the reported issue is caused by the stream being in a different reading position. To resolve this, you can set the stream position to 0 using the code snippet provided below:


stream.Position = 0;


If you are still experiencing the issue, kindly share the complete code snippet or a simple sample that can help us reproduce the issue. This will be beneficial for our further investigation.



BR Balasubramanian Ramanathan December 5, 2023 10:26 AM UTC

when i set the stream.position = 0. it says not supported. Please find the attached project to reproduce the problem.


Attachment: AndroidApp1_b91c0e50.zip


RB Ravikumar Baladhandapani Syncfusion Team December 8, 2023 04:31 PM UTC

Thanks for the details.

Currently, we are analyzing the reported behavior with the provided details on our end, and we will update with further details on December 12th, 2023.



KS Karmegam Seerangan Syncfusion Team December 13, 2023 12:20 PM UTC

We have analyzed and modified the sample as per your requirement. Please try this below code snippet on your end and let us know if you have any further assistance in this.

protected override void OnCreate(Bundle? savedInstanceState)

{

    base.OnCreate(savedInstanceState);

    SetContentView(Resource.Layout.activity_main);

   var assets = this.Assets;

   using (StreamReader reader = new StreamReader(assets.Open("Fonts/NotoSans-Regular.ttf")))

   {

       Stream underlyingStream = reader.BaseStream;

       MemoryStream memoryStream = new MemoryStream();

        underlyingStream.CopyTo(memoryStream);

       memoryStream.Position = 0;

       PdfTrueTypeFont font = new PdfTrueTypeFont(memoryStream, 14);

   }

}

Before that you can changes the Build Action as Android Assets. Please refer following snapshot,

image


Loader.
Up arrow icon