Add Non-English characters in PDF in C# using Aspose.PDF for .NET - System.NullReferenceException

Hi,

We are using Aspose.Pdf for .NET version 20.3 which is getting deployed in Pivotal Cloud Foundry (PCF) cloud environment on window stack/os.

Below line is throwing exception when we try to set Non-English(Fr) content like “Date d’arrivée prévue”

new TextFragment { Text = “Date d’arrivée prévue” }

Exception:
TextFragment had exception : System.NullReferenceException: Object reference not set to
an instance of an object.
at Aspose.Pdf.Text.Font…ctor(#=zaCT$kc$RUo7Q4ubUn4tXiRE= #=zs3EEYEc=)
at
#=zKzeu6yEv9nebofO9$xNNnxVk59hmUl$dcwCk4i$5qbfH8yuPRlVnjLw=.#=z_6504JuWeG_
2TMuRGTlg1WgQliBv(String #=zgDhrh1s=, Font #=zs3EEYEc=, Font&
#=zbMM2MfSEpOCkp8xkvxZxf4M=)
at Aspose.Pdf.Text.TextSegment.set_Text(String value)
at Aspose.Pdf.Text.TextFragment.set_Text(String value)
at
Dell.SupportOnline.Orders.Export.Pdf.PackingSlip.PackingSlipPdfGenerater.GeneratePdfPoc

Sample Code:
public MemoryStream GeneratePdfPoc()
{
var table = new Table();
var hdrRow = table.Rows.Add();
var hdrRowCell = new Cell();

    try
    {
        hdrRowCell.Paragraphs.Add(new TextFragment { Text = "Date d’arrivée prévue" });
    }
    catch (Exception ex)
    {
        table.ColumnWidths = "100%";
        hdrRowCell.Paragraphs.Add(new TextFragment { Text = $"TextFragment had exception : {ex.ToString()}" });
    }

    hdrRow.Cells.Add(hdrRowCell);
    SetAsposePdfLicense();

    var document = new Document();
    var page = document.Pages.Add();
    page.Paragraphs.Add(table);

    var output = new MemoryStream { Position = 0 };
    document.Save(output);

    return output;
}

Note: same code works on my Laptop with window OS, but when deployed on PCF cloud environment it is not working.

Now since it is PCF cloud environment we can’t install the fonts on the container, how to resolve this Non-English issue?

PDF ran from PCF Could environment on Window operating system image.png (203.7 KB)

PDF ran from Local Visual studio on Window operating system : From_Local_Visualstudio.pdf (31.8 KB)

Regards
-Nirmal

@nirmalkumarcmAspose,

I have worked with sample code shared by you and it seems that issue is with fonts. Can you please check and make sure that required fonts are available in your deployment version.

Hi Ahmad, thanks for reply.

Since we are deploying our app in PCF cloud environment we don’t have option to install/deploy the fonts in PCF cloud container.

Is there a option to include all fonts files in our .Net project folder and SetFontsSources like one we have in Aspose.Words?

Regards
-Nirmal

@nirmalkumarcmAspose,

I have observed your comments. Can you please visit this thread. This will help you to set fonts folder. Please share feedback with us if there is still an issue.

Below addLocalFontPath function is not there in .Net version, it is there in Java

Also went through API FontRepository | Aspose.PDF for .NET API Reference
But could not find alternative to addLocalFontPath in .Net

FontRepository.addLocalFontPath(“path_to_fonts”);

@nirmalkumarcmAspose

Please use following sample code on your end for font path.

// connect custom font directory
FolderFontSource fs = new FolderFontSource(@“path\to\my\folder”);
FontRepository.Sources.Add(fs);

Thanks Ahmad, i am able to solve the Non-Eng issue by above mentioned solution.

@nirmalkumarcmAspose,

You are welcome.