NullReferenceException when saving PDF with special character and newline

Hi, we are using using Aspose.PDF.Drawing 23.12.0 with a license, but the issue was also reproduced with the same code with the latest release (trial).

The issue shows itself when running the .net core 8 application either on a linux container or directly on the WSL through Visual Studio. On Windows, the code does not fail.

Code to reproduce:

using Aspose.Pdf;
using Aspose.Pdf.Text;

FontRepository.LoadFonts();

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

// 1. text
page.Paragraphs.Add(new TextFragment("1.1 Some text"));

// 2. text with newline
page.Paragraphs.Add(new TextFragment("2.1 Some text" + Environment.NewLine));

// 3. text with norwegian characters
page.Paragraphs.Add(new TextFragment("3.1 Some text æ"));
page.Paragraphs.Add(new TextFragment("3.2 Some text ø"));
page.Paragraphs.Add(new TextFragment("3.3 Some text å"));
page.Paragraphs.Add(new TextFragment("3.4 Some text æøå"));

// 4. text with norwegian characters before newline
page.Paragraphs.Add(new TextFragment("4.1 Some text æ" + Environment.NewLine));
page.Paragraphs.Add(new TextFragment("4.2 Some text ø" + Environment.NewLine));
page.Paragraphs.Add(new TextFragment("4.3 Some text å" + Environment.NewLine));

var stream = new MemoryStream();
document.Save(stream, SaveFormat.Pdf);

File.WriteAllBytes("#output.pdf", stream.ToArray());

If the last line adding a paragraph is removed, the PDF is generated just fine. With the last line, the following error is thrown:

Object reference not set to an instance of an object.

at aspose.pdf.text.font..ctor(#=zjuhsxkwul6jhk3l0j8ft3xs= #=zsoy5qye=)
at aspose.pdf.text.textparagraph.#=zwajerq8eivvmmovdcunwhaa=(string #=zznhkzlc=, textstate #=zoucxtqm=)
at aspose.pdf.text.textparagraph.#=zwrhbpcqf5ipr(textfragment #=zznhkzlc=, textstate #=zoucxtqm=, single #=z3zaxe8jkf7ud)
at aspose.pdf.text.textparagraph.#=zxsebtcabhv_8cymk4omtlo8=(textfragment #=zos0pkwu=, textstate #=zoucxtqm=, single #=z3zaxe8jkf7ud)
at aspose.pdf.text.textparagraph.appendline(textfragment line, textstate textstate, single linespacing)
at aspose.pdf.text.textparagraph.appendline(textfragment line)
at #=znr6d2gv41z6ha5orklpp5ig=.#=zyl16smkqkul_(textfragment #=zos0pkwu=, rectangle #=zc$effj0=, page #=zipbugsq=, boolean #=zwkeu2r7i1rs9, horizontalalignment #=zas7gkso=, verticalalignment #=z_ohmfqjina5q, boolean #=zx$cji1jssgjk)
at #=znr6d2gv41z6ha5orklpp5ig=.#=zyl16smkqkul_(textfragment #=zos0pkwu=, rectangle #=zc$effj0=)
at #=znr6d2gv41z6ha5orklpp5ig=.#=zfamhzisqkbrx(textfragment #=zeaf2wvd0ittgkl9lmg==, textbuilder #=zp5suv4w=, textparagraph& #=zyv2axxdduypc)
at #=znr6d2gv41z6ha5orklpp5ig=.#=zii7wbnpeln7u(textfragment #=zeaf2wvd0ittgkl9lmg==)
at #=znr6d2gv41z6ha5orklpp5ig=.#=zd6clvug=()
at aspose.pdf.page.#=zdippncgqxgis(page #=zipbugsq=, list`1 #=zgnow9w8=, double #=zwewyrao=, double #=zrafgwu4=)
at aspose.pdf.page.#=zzppvjzw=(page #=zipbugsq=)
at aspose.pdf.page.#=zxowrqq3id1x9z0wygq==()
at aspose.pdf.document.processparagraphs()
at aspose.pdf.document.#=zbwdxfkdcnsil(stream #=z9ru6u4u=, saveoptions #=zjpioeihlzoct)
at aspose.pdf.document.#=zsrvdau0yswub(stream #=zl7sc4wlehaqh, saveoptions #=zjtjhevc=)
at aspose.pdf.document.#=zsrvdau0yswub(stream #=zl7sc4wlehaqh, saveformat #=zicsq9rm=)
at aspose.pdf.document.save(stream outputstream, saveformat format)

@ingej

Instead of Aspose.PDF for .NET, have you tried installing and using Aspose.Pdf.Drawing for .NET in Linux environment? Also, please check and verify that msttcorefonts package is installed or not. In case issue still persists, please let us know.

Hi, thanks for your quick response.

We are using Aspose.PDF.Drawing, so that should be ok.

Is msttcorefonts a requirement? Because we do no load any fonts. If loading a font from disk and using it, the problem disappears. But should it not work with the ‘default’ font? The ‘special’ norwegian characters seem to work fine, as long as they are not combined with newlines.

But thanks, I will look into loading the msttcorefonts .

Hi, I tried installing msttcorefonts in the container, which results in the error not happening. But if I try setting the default font of the document when saving, the font is only used for some lines.

var saveOptions = new PdfSaveOptions
{
    DefaultFontName = "Arial",
};

document.Save(stream, saveOptions);

pdf_output_arial.png (18.1 KB)

I did a bit more testing. Even when loading fonts from disk, the font is not used for all text.

var assembly = Assembly.GetExecutingAssembly();
var fontStream = assembly.GetManifestResourceStream("DemoApp.Roboto-Regular.ttf");
var theRegularFont = FontRepository.OpenFont(fontStream, FontTypes.TTF);
theRegularFont.IsEmbedded = true;

 var document = new Document
 {
     PageInfo =
     {
         DefaultTextState =
         {
             Font = theRegularFont,
         }
     }
 };

Even when doing this, other fonts are embedded into the document, and some lines does not use the Roboto fonts (the ones with the å character).

If I load the font as above and use it for the text fragments where the default document font is not working, the correct font is used:

var problematicFragment = new TextFragment("4.3 Some æøåÅØLÆ text å" + Environment.NewLine)
{
    TextState =
    {
        Font = theRegularFont
    }
};

@ingej

Yes, the presence of Windows Fonts are necessary for the API to produce expected results. Along with that, the API will need to be supplied with fonts that support special characters or foreign language characters for that matter. If you set default font, the API will use it to render characters and it will not work for the characters that are not supported by it. Therefore, you need to specify a font directory where all necessary fonts are present so that API can access them during PDF processing and pick all suitable fonts to render the characters.

FontRepository.Sources.Add(new FolderFontSource("Directory where Fonts are present"));

In case you still notice any issues, please share your docker file and other information to replicate the issue with minimal code. We will further proceed accordingly.

Arial should (and does) support all Norwegian characters. Some of the characters (øæ) work as expected. For now, just å causes the issue. But if we put the same texts into a html file and use Arial there too, converting the html file to PDF works as expected with all ‘special’ characters in the selected font. So this is only happening when using the approach shown above. But thanks for your help, we will probably use html files + conversion moving forward.

@ingej

In Linux based systems, a specific font can be used differently as system operates the character set differently as compared to Windows. Nevertheless, please keep using the API and feel free to share details in case you face any other issues. We will proceed further to assist you accordingly.