Document Save returns NullReferenceException due to issues with umlauts and ttf-mscorefonts-installer fonts

Hi,

we experience some issues similar to Add Non-English characters in PDF in C# using Aspose.PDF - System.NullReferenceException - #11 by asad.ali

Is the ttf-mscorefonts-installer really required for usage of Aspose.Pdf? The problem is that these fonts pose a license challenge for us and we dont want to use them. We bought the big Aspose package and are now required to maybe even buy/license these font for proper usage? This seems excessive. Is there a way to substitute these fonts in the context of Documents and umlauts (ü,ä,ö,…)?

We found out the following variants:

  1. No umlauts, no msfonts => No Problem
  2. With Umlauts, With msfonts => No Problem
  3. With Umlauts, no msfonts => NRE
  4. Deleting all fonts in /usr/share/fonts/truetype/msttcorefonts => NRE
  5. Deleting all fonts except arial => No Problem
  6. “renaming” e.g. the JetBrains font in Arial and overwriting in “/usr/share/fonts/truetype/msttcorefonts/” => No Problem
    Where Renameing is not just set a file name, it means setting font information in the .ttf-File
    Font name: “ArialMT” Family-name “Arial”, Name: “Arial” (here FontForge was used)

System specs:
Aspose.Pdf: 21.6.0
Linux (Alpine)
DotNet Core 5.0
The Liberation Mono fonts is installed and can be loaded by Aspose.

Example code:

Document document = new();
var page = AddNewPage(document);
string pureTextLine = “asdfghjkl ä”;
TextFragment textFragment = new();
// Set text properties
textFragment.TextState.Font = FontRepository.FindFont(“Liberation Mono”, FontStyles.Regular, true);
textFragment.TextState.FontSize =12;
//Add text
textFragment.Text = pureTextLine;
page.Paragraphs.Add(textFragment);
var targetStream = new MemoryStream();
document.Save(targetStream, SaveFormat.Pdf);
return targetStream;

Error:

System.NullReferenceException HResult=0x80004003 Message=Object reference not set to an instance of an object. Source=Aspose.PDF StackTrace: at Aspose.Pdf.Text.Font…ctor(#=zzAdlzqBLBBM$ix0YtLsrQlI= #=zZSFEfbA=) at #=z3NJn0rgKZdfKwcCHUdmeHHRQ8$eVipejZ9FEpyF4rqRrpnUVUwQt7Ms=.#=zrx9TPK6SqTkjNDxSoJniVD5cLAlb(String #=zzrtNc8o=, Font #=zZSFEfbA=, Font& #=zW79ZnQuj9gQsPx4rIHpXbBc=) at Aspose.Pdf.Text.TextSegment.set_Text(String value) at Aspose.Pdf.Text.TextFragment.#=zt4EkmOB5ZkkA(String #=z24s_ez0=) at Aspose.Pdf.Text.TextFragment.#=zIE1AgFg=(TextFragment #=z3e1d2lk=) at Aspose.Pdf.Text.TextFragment.Clone() at Aspose.Pdf.Text.TextParagraph.#=zDBGD9u50iB6qu5c1Zfq8G34=() at Aspose.Pdf.Text.TextParagraph.#=zu3Bu$kdadD10Ff3xyvlHhu8=(Boolean #=zU2r_DXg=) at Aspose.Pdf.Text.TextParagraph.#=zu3Bu$kdadD10Ff3xyvlHhu8=() at Aspose.Pdf.Text.TextParagraph.EndEdit() at #=zJLgYjJ542J$N43G9JAlCcAI=.#=zkLcmE3lnceF_(TextFragment #=z3e1d2lk=, Rectangle #=zlvYFYAs=,

@Lockhead

The MS Fonts are required to be installed in the system where you are using the API because, Aspose.PDF uses these fonts to generate and process the PDF documents. Indeed, some fonts offer license limitations and they can only be used under certain conditions. For example, they can be embedded only into non-editable document. PDF/A document is a case of non-editable document.

Aspose.Pdf.Text.Font Class has a property ‘FontOptions’ of type Aspose.Pdf.Text.IFontOptions, which also has property ‘NotifyAboutFontEmbeddingError’. You can use this property if you want to get exception notification if font embedding was failed. Also Aspose.Pdf.Text.Font Class has a method GetLastFontEmbeddingError() - to return description of error if an attempt to embed font was failed, if there are no error cases it returns empty string.

So to get notification about font license restrictions you can use code like this:

font.FontOptions.NotifyAboutFontEmbeddingError = true;
try
{
  font.IsEmbedded = true;
}
catch(Exception ex)
{
}

OR

font.IsEmbedded = true;
string errorMessage = font.GetLastFontEmbeddingError();
if (!string.IsNullOrEmpty(errorMessage))
{
}

Furthermore, we have logged an investigation ticket as PDFNET-50136 in our issue tracking system to investigate this whole case. We will look into its details and let you know as soon as we have some feedback after analysis of the issue. Please be patient and spare us some time.

We are sorry for the inconvenience.

Thanks for the reply!

I just wanted to emphasize that our problem is in the context of creating a new PDF (via Document) and not loading of existing PDFs.

Regards

@Lockhead

We will surely investigate the ticket from this perspective and let you know once we have some updates regarding its resolution. Please give us some time.

PS: Meanwhile, please try using Document.DisableFontLicenseVerifications = true; property and share your feedback with us in case it helps to resolve the issue at your end.

We tested it out and nothing changed.

@Lockhead

Thanks for the feedback.

The ticket information has been updated accordingly and we will let you know once we have additional updates regarding its resolution.