We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

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

Hi, I got the below Null reference exception when adding special non-English characters such as ä to PDF. Because my application ran in RedHat machine, I loaded mscorefonts to the app. I use aspose.pdf in .NET code.

Code to load custom fonts before creating PDF document:
private static void AddCustomFonts()
{
//get ttf resources’ names
var fontNames = typeof(IncludedExcludedDocuments).Assembly.GetManifestResourceNames().Where(name => name.EndsWith(“ttf”));
Stream fontStream;
FontSource fontSource;
FontRepository.Sources.Clear();
var count = 0;
foreach (var fontName in fontNames)
{
count++;
//get stream object of resources
fontStream = typeof(IncludedExcludedDocuments).Assembly.GetManifestResourceStream(fontName);
//create font source off the stream and add it to fontrepository

            using (var ms = new MemoryStream())
            {
                fontStream.CopyTo(ms);
                fontSource = new MemoryFontSource(ms.ToArray());
                FontRepository.Sources.Add(fontSource);
            }
        }

    }

Exception stack trace:
System.NullReferenceException: Object reference not set to an instance of an object.
at Aspose.Pdf.Text.Font…ctor(#=z6I97n3Q5gfsJZVKXg0iiWmI= #=z3FKqCpY=)
at #=z7bI4GhXHbP5a$tKtZ8fm06BHoRC_OkCVFa6LJsOBCZxTOHyiI6BH0LE=.#=zSC3Wf7UWH3o9b8YzOu_EPNlXTTNI(String #=zBPdu0d8=, Font #=z3FKqCpY=, Font& #=zGOQ$bzu9227AxXZqyIlAUP4=)
at #=zqWBeXkAaLyA5AHcpSX2_Dkf5a5Yj.#=zn2QRuNL7kXPN(String #=zk8kaDOI=)
at Aspose.Pdf.Text.TextFragment.#=z3AZMS32VU1kV(String #=zk8kaDOI=)
at Aspose.Pdf.Text.TextFragment…ctor(String text)
at Aspose.Pdf.Cells.Add(String text)

Do you have suggestion what I can do to prevent the exception for non-special english characters?

@zhenzhen.su

Please try to assign font directly to the TextFragment like below and let us know if the issue still persists by sharing a sample code snippet to add text inside PDF:

textFragment.TextState.Font = FontRepository.FindFont("Arial Unicode MS");

Hi, @asad.ali
thank you for the tip. I did the following to assign font directly to the TextFragment, instead of using Arial Unicode MS, I used Times New Roman. It worked in my local windows machine , but got Null exception in Linux machine during deployment.

        PDF.Page page = pdfDocument.Pages[1];
        PDF.Text.TextFragment title = new PDF.Text.TextFragment("ريخلا ءاسم");          
        title.TextState.Font = Fonts["IHS.Apps.Specs4.CustomCollection.Business.Resources.NotoSans-Regular.ttf"];        
        page.Paragraphs.Add(title);

Error message:
System.NullReferenceException: Object reference not set to an instance of an object.
Stack Trace:
at Aspose.Pdf.Text.Font…ctor(#=z6I97n3Q5gfsJZVKXg0iiWmI= #=z3FKqCpY=)
at #=z7bI4GhXHbP5a$tKtZ8fm06BHoRC_OkCVFa6LJsOBCZxTOHyiI6BH0LE=.#=zSC3Wf7UWH3o9b8YzOu_EPNlXTTNI(String #=zBPdu0d8=, Font #=z3FKqCpY=, Font& #=zGOQ$bzu9227AxXZqyIlAUP4=)
at #=zqWBeXkAaLyA5AHcpSX2_Dkf5a5Yj.#=zn2QRuNL7kXPN(String #=zk8kaDOI=)
at Aspose.Pdf.Text.TextFragment.#=z3AZMS32VU1kV(String #=zk8kaDOI=)
at Aspose.Pdf.Text.TextFragment…ctor(String text)

@zhenzhen.su

The issue seems related to missing font or API is unable to find the required font in your system. Could you please try to get the font directories using FontRepository.Sources and see if the font is present in one of those directories where API is supposed to find it. These fonts should be placed in “/usr/share/fonts/truetype/msttcorefonts” directory as Aspose.PDF for .NET scans this folder on Linux like operating systems.

Hi, asad.ali
Thank you for the suggestion. I did the following as suggested and able to generate PDF containing special characters successfully except for east Asian and southeast Asian characters. These asian characters show up as tofu on the PDF.

  1. placed msttcorefonts in a font folder inside the app
  2. using FontRepository.Sources to add the font folder

I think msttcorefonts doesn’t support Asian characters (CJK). You suggested using Arial Unicode MS, however it is costly. I tried putting CODE2000 and Unifont in font folder but they didn’t work still showing tofu in the PDF, even though they say they support CJK. I will try explicitly textFragment.TextState.Font = FontRepository.FindFont(“CODE2000”). Do you know any other font besides Arial Unicode MS that will work for asian char (CJK). Thank you.

@zhenzhen.su

Usually MS Gothic and Serif family fonts support the CJK. Also, you can try to use SimSun font. In case issue still persists, please let us know.

Hi, I tried to use noto sans regular but got null exception. Is it because aspose pdf doesn’t support non-microsoft fonts? https://www.google.com/get/noto/

@zhenzhen.su

Aspose.PDF supports using Noto Sans Font and your issue seems related to specific environment. Would you please share the name and version of the OS that you are running along with the information of application type and .NET Core version. Also, please confirm if the Noto fonts are placed in the same font directory where other fonts are present and usable by the API?

Hi, asad.ali
thank you. My server is Red Hat Enterprise Linux Server 7.9 (Maipo). The application is net 5 and it is of type Console Application. The code to generate PDF resides in a library of .net standard 2.0. The application uses the library.
The noto fonts are placed in the directory /fonts/truetype/msttcorefonts/ of the application and they can be found by title_test1.TextState.Font = FontRepository.FindFont(“Noto Sans Regular”); . I removed all of mscorefonts because I don’t have license for them and leaving only noto sans fonts.

Here is screenshot for font folder (Stream output)Capture.JPG (18.1 KB)

my code snippet
var curDir = Directory.GetCurrentDirectory();
var rootDir = Directory.GetDirectoryRoot(curDir);
FolderFontSource fs = new FolderFontSource(curDir + @"/fonts/truetype/msttcorefonts/");
FolderFontSource fs1 = new FolderFontSource(rootDir + @"/usr/share/fonts/truetype/");
FolderFontSource fs2 = new FolderFontSource(rootDir + @"/usr/share/fonts/");
FontRepository.Sources.Add(fs);
FontRepository.Sources.Add(fs1);
FontRepository.Sources.Add(fs2);

PDF.Text.TextFragment title_test = new PDF.Text.TextFragment();
title_test.TextState.FontSize = PAGE_TITLE_SIZE;
title_test.TextState.Font = FontRepository.FindFont(“Noto Sans Regular”);
title_test.Margin = new PDF.MarginInfo(135, 10, 10, 10);
title_test.Text = “ờồệềä”;
page.Paragraphs.Add(title_test);

        PDF.Text.TextFragment title_test1 = new PDF.Text.TextFragment();
        title_test1.TextState.FontSize = PAGE_TITLE_SIZE;
        title_test1.TextState.Font = FontRepository.FindFont("Noto Sans Regular");
        title_test1.Margin = new PDF.MarginInfo(135, 10, 10, 10);
        title_test1.Text = "á, é, í, ó, ú, ü, ñ, ¿, ¡";
        page.Paragraphs.Add(title_test1);

error stack trace:
at Aspose.Pdf.Text.Font…ctor(#=z6I97n3Q5gfsJZVKXg0iiWmI= #=z3FKqCpY=)

at #=z7bI4GhXHbP5a$tKtZ8fm06BHoRC_OkCVFa6LJsOBCZxTOHyiI6BH0LE=.#=zSC3Wf7UWH3o9b8YzOu_EPNlXTTNI(String #=zBPdu0d8=, Font #=z3FKqCpY=, Font& #=zGOQ$bzu9227AxXZqyIlAUP4=)

at Aspose.Pdf.Text.TextSegment.set_Text(String value)

at Aspose.Pdf.Text.TextFragment.#=z7ve1zF7xTB2l(String #=zp3TWiYw=)

at Aspose.Pdf.Text.TextFragment.#=zJMGcUL4=(TextFragment #=zKfw1dxc=)

at Aspose.Pdf.Text.TextFragment.Clone()

at Aspose.Pdf.Text.TextParagraph.#=z9tY_kkPCFpp_4NC1v6PpB5o=()

at Aspose.Pdf.Text.TextParagraph.#=zgjL8vF5IrRDMW_ffu_5bO4g=(Boolean #=z3XLt0vM=)

at Aspose.Pdf.Text.TextParagraph.#=zgjL8vF5IrRDMW_ffu_5bO4g=()

at Aspose.Pdf.Text.TextParagraph.EndEdit()

at #=z6Gu3oEMf_fwueis7IsEom1s=.#=zLPyUIR$R2VAj(TextFragment #=zKfw1dxc=, Rectangle #=zB_ZHGFM=, Page #=zvY3BxxQ=, Boolean #=zaj80Ob2_AI_S, HorizontalAlignment #=zEK6jUGg=, VerticalAlignment #=zPY2tS0WFGYnF, Boolean #=z88DkAM9wsL1h)

at #=z6Gu3oEMf_fwueis7IsEom1s=.#=zLPyUIR$R2VAj(TextFragment #=zKfw1dxc=, Rectangle #=zB_ZHGFM=)

at #=z6Gu3oEMf_fwueis7IsEom1s=.#=zfxuzJsESKqX4(BaseParagraph #=zoume_6_V_MRxzxomfw==)

at #=z6Gu3oEMf_fwueis7IsEom1s=.#=zj89OGJo=()

at Aspose.Pdf.Page.#=zEMZFlFo=(Page #=zvY3BxxQ=)

at Aspose.Pdf.Page.#=zn5iMv7YSNSsprhuNKQ==()

at Aspose.Pdf.Document.ProcessParagraphs()

at Aspose.Pdf.Document.#=zUl0u9u6RLfUr(Stream #=zcLcVG9A=, SaveOptions #=z$JBS2IlSATdb)

at Aspose.Pdf.Document.Save

Hi,
Is there a way to do fallback to a different font when the specified font don’t cover the text? For example, aspose word has FontSetting object which allows us to put a list of fonts. If one font doesn’t work , another font will be used.

@zhenzhen.su

Current custom font substitution in Aspose.PDF (using FontRepository.Substitutions ) works
in a declarative manner. It changes the original font name with a new font name in a PDF document, so Adobe Acrobat or another PDF viewer will use another font to show text. Font encoding and other data related to font are not changed. This declarative font substitution possibly can cause problems, when the new font is not fully compatible with the original font.

Furthermore, we have logged an issue as PDFNET-49346 in our issue tracking system against your case. We will further look into its details and keep you posted with the status of its rectification. Please be patient and spare us some time.

We are sorry for the inconvenience.

Hi, asad.ali
Does the environment have to have mscorefonts installed in order for aspose pdf library to work? Is there any fonts that will work without mscorefonts installed?

Thank you.

@zhenzhen.su

Yes, it is recommended to use Microsoft Fonts while processing PDF files using Aspose.PDF in order prevent unwanted results. As far as fonts other than MS are concerned, they can be used by the API but you need to specify them in the code by using FontRepository methods.

The issues you have found earlier (filed as PDFNET-49346) have been fixed in Aspose.PDF for .NET 21.12.