Aspose pdf fails to save a PDF when options are defined

using .net 9 latest apose.pdf (25.10.0).

1.pdf (221.1 KB)

when trying to save attached PDF with:

SaveOptions so = new PdfSaveOptions()
{
    DefaultFontName = "Arial",
};

pdfDocument.Save(outputPdfPath, so);

save fails with the following nullRef exception:
Error: Object reference not set to an instance of an object.
Stack trace: at #=zqCWMPX8WWRMwgQXJZKHVULVQvN9hbbKOwxuyiRmRcTAl.#=zgO6CVw5eW5VxOeivBw==()
at #=zqCWMPX8WWRMwgQXJZKHVULVQvN9hbbKOwxuyiRmRcTAl.#=zimr5kVDAp2Yu()
at Aspose.Pdf.Document.#=zdhv8BSZdNRMogWlylg==(Document #=zccs6ZfY=, String #=zf0bheg8Ykozi)
at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr)
— End of stack trace from previous location —
at dje_qLFF8XAWXAJWVVSX66GNXAF97SGY6JTS9KZH3T6FSTEGCPSMS8JYA_ejd.#=zGO$NTH822a7quOPAnzWcuTX3RhN6DDlcOa0IHh8=(Exception #=zlanGNwI=)
at dje_qLFF8XAWXAJWVVSX66GNXAF97SGY6JTS9KZH3T6FSTEGCPSMS8JYA_ejd.#=z1kNn3DA4POzSShLh$E$367M$DRKG(Object #=zlanGNwI=)
at dje_qLFF8XAWXAJWVVSX66GNXAF97SGY6JTS9KZH3T6FSTEGCPSMS8JYA_ejd.#=zXx0EC_dCAv9dRC0$mpSJDWg=(MethodBase #=zlanGNwI=, Boolean #=zUIBQWD4=)
at dje_qLFF8XAWXAJWVVSX66GNXAF97SGY6JTS9KZH3T6FSTEGCPSMS8JYA_ejd.#=zgA6oAFn0k17ULD3V9NWa6JAs5E51OjJceA==(dje_qLFF8XAWXAJWVVSX66GNXAF97SGY6JTS9KZH3T6FSTEGCPSMS8JYA_ejd #=zlanGNwI=, #=qZ6YJboq_0HB1JJjWQJfFO81Mq$CLAuNsNCC485aJ69o= #=zUIBQWD4=)
at dje_qLFF8XAWXAJWVVSX66GNXAF97SGY6JTS9KZH3T6FSTEGCPSMS8JYA_ejd.#=zJAt7uWngplGXQUh9gc5G3NzEvU26()
at dje_qLFF8XAWXAJWVVSX66GNXAF97SGY6JTS9KZH3T6FSTEGCPSMS8JYA_ejd.#=zUJQPov4XlwZU5w3oua1mq1a$iK5k(Boolean #=zlanGNwI=)
— End of stack trace from previous location —
at dje_qLFF8XAWXAJWVVSX66GNXAF97SGY6JTS9KZH3T6FSTEGCPSMS8JYA_ejd.#=zGO$NTH822a7quOPAnzWcuTX3RhN6DDlcOa0IHh8=(Exception #=zlanGNwI=)
at dje_qLFF8XAWXAJWVVSX66GNXAF97SGY6JTS9KZH3T6FSTEGCPSMS8JYA_ejd.#=z1kNn3DA4POzSShLh$E$367M$DRKG(Object #=zlanGNwI=)
at dje_qLFF8XAWXAJWVVSX66GNXAF97SGY6JTS9KZH3T6FSTEGCPSMS8JYA_ejd.#=zAD0tb9eTLbRbkMdmdqKBDsNDTeqA()
at dje_qLFF8XAWXAJWVVSX66GNXAF97SGY6JTS9KZH3T6FSTEGCPSMS8JYA_ejd.#=zpVOfhnDB2_E1HKY_zTzRbFC1xFW9fMX5_U88Q4Y=(Object #=zlanGNwI=, UInt32 #=zUIBQWD4=)
at dje_qLFF8XAWXAJWVVSX66GNXAF97SGY6JTS9KZH3T6FSTEGCPSMS8JYA_ejd.#=zUJQPov4XlwZU5w3oua1mq1a$iK5k(Boolean #=zlanGNwI=)
at dje_qLFF8XAWXAJWVVSX66GNXAF97SGY6JTS9KZH3T6FSTEGCPSMS8JYA_ejd.#=zVrq9vnBipanssS_k5rj6R919tV4$HArV7yrPX3SihrHT(Object[] #=zlanGNwI=, Type[] #=zUIBQWD4=, Type[] #=zwpAkrcg=, Object[] #=zcJMfVMI=)
at dje_qLFF8XAWXAJWVVSX66GNXAF97SGY6JTS9KZH3T6FSTEGCPSMS8JYA_ejd.#=zCPdtD8mHdWGGgn9BdPffG7ncmK16K94pfKUQgII=(Stream #=zlanGNwI=, String #=zUIBQWD4=, Object[] #=zwpAkrcg=, Type[] #=zcJMfVMI=, Type[] #=zj0ZLhm4=, Object[] #=zynBCpNs=)
at dje_qLFF8XAWXAJWVVSX66GNXAF97SGY6JTS9KZH3T6FSTEGCPSMS8JYA_ejd.#=z_tjsD0vKQCY7fwWtDHaJngE=(Stream #=zlanGNwI=, String #=zUIBQWD4=, Object[] #=zwpAkrcg=)
at dje_qLFF8XAWXAJWVVSX66GNXAF97SGY6JTS9KZH3T6FSTEGCPSMS8JYA_ejd.#=zutjfBNbBUzPIT75ZTkW5xhqGkfox(Stream #=zlanGNwI=, String #=zUIBQWD4=, Object[] #=zwpAkrcg=)
at Aspose.Pdf.Document.#=zU50KS4smIEQs(Stream #=zPkgGkfg=, SaveOptions #=zg0M2Os1MzU7P)
at Aspose.Pdf.Document.#=zba8CQpJ8TrZo(Stream #=zPkgGkfg=, SaveOptions #=zg0M2Os1MzU7P)
at Aspose.Pdf.Document.#=zcATkl1VJKUYG(Stream #=zRUWhNQhlfHvA, SaveOptions #=z2cJfcRw=)
at Aspose.Pdf.Document.Save(Stream outputStream, SaveOptions options)
at Aspose.Pdf.Document.#=zcATkl1VJKUYG(String #=zEY9OrAWsLrG5, SaveOptions #=z2cJfcRw=)
at Aspose.Pdf.Document.Save(String outputFileName, SaveOptions options)
at AsposePdfReader.Program.Main(String[] args) in D:\AsposePdfReader\Program.cs:line 72

saving the same PDF without the options succeeds:

pdfDocument.Save(outputPdfPath);

(internal ref NCP-5964)

@oren10280

Summary
Saving the PDF works when you call pdfDocument.Save(path). Adding a PdfSaveOptions that only sets DefaultFontName = "Arial" causes a NullReferenceException during the save.

Why it happens
DefaultFontName tells Aspose.PDF which font to use when the source PDF references a font that is not embedded. When this property is set, Aspose.PDF tries to resolve the font through its internal FontSettings. If the FontSettings instance is not initialized or the specified font cannot be found, the library hits a null reference while looking up the font and throws the exception. This behaviour is observed with the 25.10.0 build on .NET 9.

What to do next

  1. Make sure the fallback font is available
  • Verify that Arial (or the font you want to use) is installed on the machine.
  • If you cannot rely on a system‑installed font, copy the .ttf file (e.g., arial.ttf) into your project and reference it at runtime.
  1. Create and configure a FontSettings object
using Aspose.Pdf;
using Aspose.Pdf.Text;

// Load the source PDF
Document pdfDocument = new Document("input.pdf");

// Prepare FontSettings
FontSettings fontSettings = new FontSettings();
// Point to a folder that contains the fallback font (e.g., a "fonts" folder next to the exe)
fontSettings.SetFontsFolder(@"./fonts", true); // true → scan sub‑folders
// Set the default font name to use when a font is missing
fontSettings.DefaultFontName = "Arial";
// Optional: always embed the fallback font in the output PDF
fontSettings.FontEmbeddingMode = FontEmbeddingMode.Always;

Alternative – load the font from memory:

byte[] arialBytes = File.ReadAllBytes(@"C:\Windows\Fonts\arial.ttf");
fontSettings.SetFontSources(new FontSourceBase[]
{
new MemoryFontSource(arialBytes, 0, arialBytes.Length)
});
  1. Attach the FontSettings to PdfSaveOptions and save
PdfSaveOptions saveOptions = new PdfSaveOptions
{
FontSettings = fontSettings
// you can add other options here, e.g. CompressionLevel, PreserveFormFields, etc.
};

pdfDocument.Save("output.pdf", saveOptions);
  1. Validate the fix
  • Run the code with the FontSettings attached but without setting DefaultFontName. The PDF should save.
  • Then set DefaultFontName and save again. If it now succeeds, the issue was the missing/incorrect font configuration.
  1. If the problem persists
  • Double‑check the path to the font folder/file and ensure the application has read permissions.
  • Try a different fallback font (e.g., “Times New Roman”) to rule out a corrupted font file.
  • Update to the latest Aspose.PDF build; occasional runtime‑specific bugs are fixed in newer patches.
  • If the exception still occurs, open a support ticket with Aspose, attaching the PDF, the minimal reproducible code, and the full stack trace.

Quick checklist

  • [ ] Confirm the fallback font (Arial) is installed or provide the .ttf file.
  • [ ] Create a FontSettings instance, set DefaultFontName, and point to the folder/file containing the font.
  • [ ] Assign the FontSettings to PdfSaveOptions.FontSettings.
  • [ ] (Optional) Set FontEmbeddingMode = FontEmbeddingMode.Always to embed the fallback font.
  • [ ] Save the document using the configured PdfSaveOptions.

Following these steps should prevent the NullReferenceException and allow the PDF to be saved with the desired font substitution behavior.

Note: The specific stack trace you posted does not appear in the retrieved documentation, so the explanation above is based on typical Aspose.PDF behavior with DefaultFontName. If the issue is still unresolved after applying the steps, further investigation with Aspose support is recommended.

:warning: I could not fully verify this explanation against the retrieved sources. Please double-check the behaviour in your environment and confirm the exact SDK/version.

the fix doesn’t work. FontSettings isn’t part of latest aspose. the issue happens only on some PDFs, where I would expect, based on your explanation, that it would happen on any PDf.

@oren10280

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-61227

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.